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.
23 24 25 |
# File 'lib/active_resource/schema.rb', line 23 def initialize @attrs = {} end |
Instance Attribute Details
#attrs ⇒ Object
An array of attribute definitions, representing the attributes that have been defined.
9 10 11 |
# File 'lib/active_resource/schema.rb', line 9 def attrs @attrs end |
Instance Method Details
#attribute(name, type, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/active_resource/schema.rb', line 27 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 |