Class: JsonApiClient::Schema
- Inherits:
-
Object
- Object
- JsonApiClient::Schema
- Defined in:
- lib/json_api_client/schema.rb
Defined Under Namespace
Classes: Property
Constant Summary collapse
- DEFAULT_PROPERTY_TYPES =
{ int: lambda {|value| value.to_i }, integer: lambda {|value| value.to_i }, string: lambda {|value| value.to_s }, float: lambda {|value| value.to_f }, boolean: lambda {|value| value.is_a?(String) ? (value != "false") : !!value }, timestamp: lambda {|value| value.is_a?(DateTime) ? value : Time.at(value.to_f).to_datetime }, timestamp_ms: lambda {|value| value.is_a?(DateTime) ? value : Time.at(value.to_f/1000).to_datetime }, datetime: lambda {|value| value.is_a?(DateTime) ? value : DateTime.parse(value.to_s) }, date: lambda {|value| value.is_a?(Date) ? value : Date.parse(value.to_s) } }
Class Method Summary collapse
- .find_property_type(name) ⇒ Object
- .property_types ⇒ Object
- .register_property_type(name, caster) ⇒ Object
Instance Method Summary collapse
-
#add(name, options) ⇒ void
Add a property to the schema.
- #each_property(&block) ⇒ Object (also: #each)
-
#find(property_name) ⇒ Property?
(also: #[])
Look up a property by name.
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
-
#size ⇒ Fixnum
(also: #length)
How many properties are defined.
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
44 45 46 |
# File 'lib/json_api_client/schema.rb', line 44 def initialize @properties = {} end |
Class Method Details
.find_property_type(name) ⇒ Object
26 27 28 |
# File 'lib/json_api_client/schema.rb', line 26 def find_property_type(name) property_types[name.to_sym] end |
.property_types ⇒ Object
18 19 20 |
# File 'lib/json_api_client/schema.rb', line 18 def property_types @property_types ||= DEFAULT_PROPERTY_TYPES end |
.register_property_type(name, caster) ⇒ Object
22 23 24 |
# File 'lib/json_api_client/schema.rb', line 22 def register_property_type(name, caster) property_types[name.to_sym] = caster end |
Instance Method Details
#add(name, options) ⇒ void
This method returns an undefined value.
Add a property to the schema
55 56 57 |
# File 'lib/json_api_client/schema.rb', line 55 def add(name, ) @properties[name.to_sym] = Property.new(name.to_sym, [:type], [:default]) end |
#each_property(&block) ⇒ Object Also known as: each
67 68 69 |
# File 'lib/json_api_client/schema.rb', line 67 def each_property(&block) @properties.values.each(&block) end |
#find(property_name) ⇒ Property? Also known as: []
Look up a property by name
76 77 78 |
# File 'lib/json_api_client/schema.rb', line 76 def find(property_name) @properties[property_name.to_sym] end |
#size ⇒ Fixnum Also known as: length
How many properties are defined
62 63 64 |
# File 'lib/json_api_client/schema.rb', line 62 def size @properties.size end |