Friday, May 30, 2014

Notes - Rails Tutorial Chapter 5 - REST

REST architecture
wiki defines REST as:
Representational state transfer (REST) is a software architectural style consisting of a coordinated set of architectural constraints applied to components, connectors, and data elements, within a distributed hypermedia system.

What???
As condescending as this is it is also a helpful explanation:
http://katgleason.tumblr.com/post/37836552900/how-i-explained-rest-to-my-wife

https://www.servage.net/blog/2013/04/08/rest-principles-explained/

http://rest.elkstein.org/

http://www.railstutorial.org/book/demo_app#table-demo_RESTful_users
HTTP requestURLActionPurpose
GET/usersindexpage to list all users
GET/users/1showpage to show user with id 1
GET/users/newnewpage to make a new user
POST/userscreatecreate a new user
GET/users/1/editeditpage to edit user with id 1
PATCH/users/1updateupdate user with id 1
DELETE/users/1destroydelete user with id 1

Vim vs Sublime

I  decided to try out Sublime finally. I am a gvim user right now and am very happy using it. Because I am going to be pair programming this summer with 2 people who use Sublime I figured that I should get a head start on learning it.  I downloaded the version for Linux Ubuntu and am working on setting it up. I found this tutorial to help me get it rolling.
How to install Sublime Text on Ubuntu tutorial.

Thursday, May 29, 2014

Notes - Rails Tutorial Chapter 7

== === eq? equal? 
http://stackoverflow.com/questions/7156955/whats-the-difference-between-equal-eql-and

Db:rest
Josh found:

http://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaloadhttp://rake.rubyforge.org/

Rails comes equipped with three environments: test, development, and production. The default environment for the Rails console is development:

If you ever need to run a console in a different environment (to debug a test, for example), you can pass the environment as a parameter to the console script:

  $ rails console test

As with the console, development is the default environment for the local Rails server, but you can also run it in a different environment:

  $ rails server --environment production
If you view your app running in production, it won’t work without a production database, which we can create by running rake db:migrate in production:

  $ bundle exec rake db:migrate RAILS_ENV=production

REST!!!!!
POST, GET, PATCH, and DELETE
When following REST principles, resources are typically referenced using the resource name and a unique identifier.

show when Rails’ REST features are activated, GET requests are automatically handled by the show action.
HTTP requestURLActionNamed routePurpose
GET/usersindexusers_pathpage to list all users
GET/users/1showuser_path(user)page to show user
GET/users/newnewnew_user_pathpage to make a new user (signup)
POST/userscreateusers_pathcreate a new user
GET/users/1/editeditedit_user_path(user)page to edit user with id1
PATCH/users/1updateuser_path(user)update user
DELETE/users/1destroyuser_path(user)delete user
______________________________________________________________________________________________________________________________________________________________________

Here we’ve used params to retrieve the user id. When we make the appropriate request to the Users controller, params[:id] will be the user id 1, so the effect is the same as the find method
@user = User.find(params[:id])
User.find(1)
Technically, params[:id] is the string "1", but find is smart enough to convert this to an integer.

Factory Girl is a DSL for defining Active Record objects.

Monday, May 26, 2014

Notes - Rails Tutorial Chapter 4

This is a great "Intro to Ruby" chapter. It is really helpful to follow along with the Rails Console just to get used to using it.

The map method returns the result of applying the given block to each element in the array or range.

   I wish this was worded better
4.3.3 Hashes and symbols
Hashes are essentially arrays that aren’t limited to integer indices. (In fact, some languages, especially Perl, sometimes call hashes associative arrays for this reason.) Instead, hash indices, or keys, can be almost any object.
Very cool
 flash = { success: "It worked!", error: "It failed." }
 flash.each do |key, value|
   puts "Key #{key.inspect} has value #{value.inspect}"
 end
methods
  inspect
  p - p returns the object being printed while puts always returns nil
  map
  second - a rails method
  Hash merge http://www.ruby-doc.org/core-2.1.2/Hash.html#method-i-merge

strings vs. hashes
strings need to be compared character by character, while symbols can be compared all in one go.

When hashes are the last argument in a function call, the curly braces are optional
Exercises



Create three hashes called person1, person2, and person3, with first and last names under the keys :first and :last. Then create a params hash so that params[:father] is person1, params[:mother] is person2, and params[:child] is person3. Verify that, for example, params[:father][:first] has the right value.

instance method
A method called on an instance, such as length, is called an instance method.

class method
When a method gets called on the class itself, as in the case of new, it’s called a class method

Wednesday, May 21, 2014

Wednesday, May 14, 2014

struct

http://www.ruby-doc.org/core-2.1.1/Struct.html
A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class. 
The Struct class generates new subclasses that hold a set of members and their values. For each member a reader and writer method is created similar to Module#attr_accessor.