Class: ActiveResource::Schema
- Inherits:
-
Object
- Object
- ActiveResource::Schema
- Defined in:
- lib/active_resource/schema.rb
Overview
:nodoc:
Constant Summary collapse
- KNOWN_ATTRIBUTE_TYPES =
attributes can be known to be one of these types. They are easy to cast to/from.
%w( string text integer float decimal datetime timestamp time date binary boolean )
Instance Attribute Summary collapse
-
#attrs ⇒ Object
An array of attribute definitions, representing the attributes that have been defined.
Instance Method Summary collapse
- #attribute(name, type, options = {}) ⇒ Object
-
#initialize ⇒ Schema
constructor
The internals of an Active Resource Schema are very simple - unlike an Active Record TableDefinition (on which it is based).
Constructor Details
#initialize ⇒ Schema
The internals of an Active Resource Schema are very simple - unlike an Active Record TableDefinition (on which it is based). It provides a set of convenience methods for people to define their schema using the syntax:
schema do
string :foo
integer :bar
end
The schema stores the name and type of each attribute. That is then
read out by the schema method to populate the schema of the actual
resource.
25 26 27 |
# File 'lib/active_resource/schema.rb', line 25 def initialize @attrs = {} end |
Instance Attribute Details
#attrs ⇒ Object
An array of attribute definitions, representing the attributes that have been defined.
11 12 13 |
# File 'lib/active_resource/schema.rb', line 11 def attrs @attrs end |
Instance Method Details
#attribute(name, type, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/active_resource/schema.rb', line 29 def attribute(name, type, = {}) raise ArgumentError, "Unknown Attribute type: #{type.inspect} for key: #{name.inspect}" unless type.nil? || Schema::KNOWN_ATTRIBUTE_TYPES.include?(type.to_s) the_type = type.to_s # TODO: add defaults #the_attr = [type.to_s] #the_attr << options[:default] if options.has_key? :default @attrs[name.to_s] = the_type self end |