Class: TripSpark::Model
- Inherits:
-
Object
- Object
- TripSpark::Model
- Extended by:
- Forwardable
- Defined in:
- lib/tripspark_api/models.rb
Class Attribute Summary collapse
-
.identifier ⇒ Object
The attribute of the model that can be used to uniquely identify an instance from any other.
Class Method Summary collapse
-
.attribute(name, type: nil, array: false) ⇒ Object
Define a new attribute of the model.
-
.attributes ⇒ Object
The list of attributes defined on the model.
-
.delegate(*names, to:) ⇒ Object
Define one or more delegated methods on the model, passing them to the given attribute.
- .primary_attribute(name) ⇒ Object
Instance Method Summary collapse
-
#==(o) ⇒ Object
Assume that two Model objects are the same if their primary attributes have the same value.
-
#assign(args = {}) ⇒ Object
Mass assign a group of attributes.
-
#identifier ⇒ Object
The value of the primary attribute on this model.
-
#initialize(args = {}) ⇒ Model
constructor
Initialize a model instance with any given attributes assigned.
Constructor Details
#initialize(args = {}) ⇒ Model
Initialize a model instance with any given attributes assigned
60 61 62 |
# File 'lib/tripspark_api/models.rb', line 60 def initialize args={} assign(args) end |
Class Attribute Details
.identifier ⇒ Object
The attribute of the model that can be used to uniquely identify an instance from any other. The primary attribute should also be set with ‘attribute <name>`.
45 46 47 |
# File 'lib/tripspark_api/models.rb', line 45 def identifier @identifier end |
Class Method Details
.attribute(name, type: nil, array: false) ⇒ Object
Define a new attribute of the model. If ‘type` is given, a new instance of `type` will be created whenever this attribute is assigned a value. This allows creation of nested objects from a simple Hash. If `type` is given and `array` is true, the value given to this attribute will be interpreted as an array and a new instance of `type` will be created for each entry in the array It `type` is given, and the value given to this attribute is nil, no new instance of `type` will be created. Instead, the value will remain nil, as an instance of NilClass.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tripspark_api/models.rb', line 18 def attribute name, type: nil, array: false attributes << [name, type] attr_reader name # Use a custom writer method to allow typed attributes to be # instantiated properly. define_method "#{name}=" do |value| # Only do type conversion if the type is specified and the value is # not nil. if type and !value.nil? # Lookup is done on TripSpark to ensure that Model subclasses are # searched first, falling back to other types (Numeric, Hash, etc.) # if no Model subclass is found. klass = TripSpark.const_get(type.to_s.constantize) value = array ? value.map{ |v| klass.new(v) } : klass.new(value) end instance_variable_set("@#{name}", value) end end |
.attributes ⇒ Object
The list of attributes defined on the model
38 39 40 |
# File 'lib/tripspark_api/models.rb', line 38 def attributes @attributes ||= Set.new end |
.delegate(*names, to:) ⇒ Object
Define one or more delegated methods on the model, passing them to the given attribute.
52 53 54 55 56 |
# File 'lib/tripspark_api/models.rb', line 52 def delegate *names, to: names.each do |name| def_delegator to, name end end |
.primary_attribute(name) ⇒ Object
46 47 48 |
# File 'lib/tripspark_api/models.rb', line 46 def primary_attribute name @identifier = name end |
Instance Method Details
#==(o) ⇒ Object
Assume that two Model objects are the same if their primary attributes have the same value
79 80 81 |
# File 'lib/tripspark_api/models.rb', line 79 def == o identifier == o.identifier end |
#assign(args = {}) ⇒ Object
Mass assign a group of attributes. Attribute names will be automatically be converted to snake_case for consistency.
66 67 68 69 70 |
# File 'lib/tripspark_api/models.rb', line 66 def assign args={} args.each do |name, value| public_send("#{name.underscore}=", value) end end |
#identifier ⇒ Object
The value of the primary attribute on this model
73 74 75 |
# File 'lib/tripspark_api/models.rb', line 73 def identifier send(self.class.identifier) end |