schema
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
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_color', 'hair_color' 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!