Flowdock
method

schema

Importance_2
v3.0.5 - Show latest stable - 0 notes - Class: ActiveResource::Base
schema(&block) public

Creates a schema for this resource - setting the attributes that are known prior to fetching an instance from the remote system.

The schema helps define the set of known_attributes of the current resource.

There is no need to specify a schema for your Active Resource. If you do not, the known_attributes will be guessed from the instance attributes returned when an instance is fetched from the remote system.

example: class Person < ActiveResource::Base

  schema do
    # define each attribute separately
    attribute 'name', :string

    # or use the convenience methods and pass >=1 attribute names
    string  'eye_colour', 'hair_colour'
    integer 'age'
    float   'height', 'weight'

    # unsupported types should be left as strings
    # overload the accessor methods if you need to convert them
    attribute 'created_at', 'string'
  end

end

p = Person.new p.respond_to? :name # => true p.respond_to? :age # => true p.name # => nil p.age # => nil

j = Person.find_by_name(‘John’) # <person><name>John</name><age>34</age><num_children>3</num_children></person> j.respond_to? :name # => true j.respond_to? :age # => true j.name # => ‘John’ j.age # => ‘34’ # note this is a string! j.num_children # => ‘3’ # note this is a string!

p.num_children # => NoMethodError

Attribute-types must be one of:

 string, integer, float

Note: at present the attribute-type doesn’t do anything, but stay tuned… Shortly it will also cast the value of the returned attribute. ie: j.age # => 34 # cast to an integer j.weight # => ‘65’ # still a string!

Show source
Register or log in to add new notes.