Sunday, September 12, 2010

Convention over configuration

This is actually coding by conventions.This software design paradigm which ensures the following things
  • This decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility
  • The developer only needs to specify unconventional aspects of the application for example if there is a class called user in the model then its curresponting table will be Users
General-purpose frameworks usually require one or more configuration files in order to set up the framework. A configuration file provides a mapping between a class and a resource (a database) or an event (a URL request). As the size and complexity of applications grow, so do the configuration files, making them harder to maintain.

A Valid example in rails is as below

An example should show you how the conventions work together: You have a database table called users with the primary key id. The matching model is called user and the controller, that handles all the logic is named users_controller. The view is split in different actions: if the controller has a new and edit action, there is also a new- and edit-view.

class NamesValidator

# Checks that first_name and last_name are within certain length
def self.valid_length?(name)
 name.first_name.length < 20 and name.last_name.length < 10
end

# Checks that first_name and last_name have the first character capitalized
# capitalize turns HELLO into Hello; hello into Hello; etc
def self.valid_case?(name)
 name.first_name == name.first_name.capitalize and
 name.last_name == name.last_name.capitalize
end

def self.non_conforming_method
 # This method will not be called during validation
end

end

class Name < Validatable

attr_accessor :first_name, :last_name # create getters and setters for instance variable name

def initialize(first_name, last_name)
 @first_name, @last_name = first_name, last_name
end

end

Name is just a simple class that has two fields: first_name and last_name. The NamesValidator has two class methods that check if the name is of valid length and if it has the right case. The method non_conforming_method is left there to show that our validation system does not call that method since it does not conform to the naming convention we agreed upon.

Saturday, September 11, 2010

The MVC Architecture (MVC)

I am happy to know that you all get a good knowledge about the rails Introduction post.So lets move to the working principle action in rails application Model–View–Controller (MVC) is a software architecture,currently considered an architectural pattern used in software engineering. The pattern isolates "domain logic" (the application logic for the user) from input and presentation (UI), permitting independent development, testing and maintenance of each.For instance, as depicted in the figure below, you may refer to "controller" as "input", "model" as "processor" or "processing" and "view" as "output". The MVC Architecture (MVC) So in other words, controller receives the input, passes it to the model for processing, or to the view for output. So MVC benefits include:
  • Isolation of business logic from the user interface
  • Ease of keeping code DRY
  • Making it clear where different types of code belong for easier maintenance
Let Us explain each in detail mentioning each functionalities

1) Model

A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.

2) View

The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.

3) Controller

The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

The MVC Architecture (MVC)

So the steps involved in the working of MVC is as follows

  • The browser makes a request, such as http://rubyonrailslink.blogspot.com/2010/09/what-is-rails.html
  • web server (mongrel, WEBrick, etc.) receives the request. It uses routes to find out which controller to use: the default route pattern is “/controller/action/id” as defined in config/routes.rb. In our case, it’s the “video” controller, method “show”, id “15″. The web server then uses the dispatcher to create a new controller, call the action and pass the parameters.
  • Controllers do the work of parsing user requests, data submissions, cookies, sessions and the “browser stuff”. They’re the pointy-haired manager that orders employees around. It gives orders without knowing (or caring) how it gets done. In our case, the show method in the video controller knows it needs to lookup a video. It asks the model to get video 15, and will eventually display it to the user.
  • Models are Ruby classes. They talk to the database, store and validate data, perform the business logic and otherwise do the heavy lifting. In this case, the model retrieves video 15 from the database.
  • Views are what the user sees: HTML, CSS, XML, Javascript, JSON. They’re the sales rep putting up flyers and collecting surveys, at the manager’s direction. Views are merely puppets reading what the controller gives them. They don’t know what exactly happening in the back room. In our example, the controller gives video 15 to the “show” view. The show view generates the HTML: divs, tables, text, descriptions, footers, etc.
  • The controller returns the response body (HTML, XML, etc.) & metadata (caching headers, redirects) to the server. The server combines the raw data into a proper HTTP response and sends it to the user.

Thursday, September 9, 2010

Ruby Introduction

  • Ruby was introduced by Yukihiro Matsumoto (known widely as “Matz”) in 1993
  • Ruby is a dynamic interpreted language which has many strong features of various languages
  • It is a strong Object oriented programming language which also has single inheritance as in Java
  • It also provides us with the feature called as mixins. Using mixins we can easily import methods from multiple classes using modules. Ruby also has the scripting feature similar to the Python and Perl
  • The Object oriented concept from C++ and Java also maintains the reliability of programming in addition to maintaining the security of code
  • Ruby is open source which means that it is free to be used; one does not need to pay anything to use it. Because of this feature of Ruby it is used worldwide by everyone
Ruby Introduction

Why learn Ruby

  • Ruby can easily recognize variable types all by itself. Due to this feature coders are not required to define variable types as we have to do in several other programming languages. Ruby's dynamic typing saves a lot of time for programmers
  • Ruby has been provided to programmers with various class libraries which are bundled together. These class libraries start from the basic data types to advanced level thread and network programming. These class libraries have not been saturated yet and Ruby still is in the process of getting more libraries with time to make programming more simple and efficient
  • Ruby also comes with an effective garbage collector which avoids the problem of memory leaks to a great extent and takes care of the misuse and unnecessary occupancy of the memory
  • Ruby provides us with familiar syntaxes which are known to C++,Eiffel, Perl, and Python programmers. These syntaxes are composed of all the common features available for the programming like the comments, identifiers, reserved words, literals, arrays, Regular expressions etc

Wednesday, September 8, 2010

What is Rails?

Rails is a web application development framework written in the Ruby language. It allows you to write less code while accomplishing more than many other languages and frameworks.Rails is opinionated software. It makes the assumption that there is a “best” way to do things, and it’s designed to encourage that way.

Some key points regarding rails is below

  • An extremely productive web-application framework.
  • Written in Ruby by David Heinemeier Hansson
  • You could develop a web application at least ten times faster with Rails than you could with a typical Java framework
  • An open source Ruby framework for developing database-backed web applications
  • Your code and database schema are the configuration
  • No compilation phase required

The Rails philosophy includes several guiding principles:

  • DRY – “Don’t Repeat Yourself” – suggests that writing the same code over and over again is a bad thing.
  • Convention Over Configuration – means that Rails makes assumptions about what you want to do and how you’re going to do it, rather than requiring you to specify every little thing through endless configuration files.
  • REST is the best pattern for web applications – organizing your application around resources and standard HTTP verbs is the fastest way to go.
This will be discussed in depth later.

Rails Strengths

Rails is packed with features that make you more productive, with many of the following features building on one other.

1) Metaprogramming

Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.

2) Active Record

Rails introduces the Active Record framework, which saves objects to the database. The Rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.

3) Convention over configuration

Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow suggested naming conventions, Rails doesn't need much configuration.

4) Scaffolding

You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.

5) Built-in testing

Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.

6) Three environments

Rails gives you three default environments: development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.

Monday, August 30, 2010

First post for Programmers

HI Friends, last day my father asks me to do some works on web on the basis of my software experience in the platform i am working.He told me that the benifits are three
  • You will learn that topic thouroughly
  • Makes the others to learn if somebody are inneed of that resources
  • You can earn from your blog by the contents
  • Your Laziness factor will be less to large extend also, if you starts writing
I know why he suggest me to that,because he knows very well about me that i am a lazy person(not in a large scale).I straight away told him that its possible but many things are to be done before doing that like collecting the theory part of thattopic and with simple steps i had to well explain that in each post by giving some screen casts and also its time consuming and i am lacking that too.Listening to my speech he just give me a silent smile and shake his head and went away.Even though it is a good smile but the way and the time of smile make me to think little bit more. So i started thinking of writing a blog which well describes the various concepts in my platform like Ruby on Rails and dot net.So i decided to write one blog which is well explains the ruby on rails applications and dot net basics.