Module: FleetAPI::Attributes
- Defined in:
- lib/fleet_api/attributes.rb
Instance Method Summary collapse
- #assoc_accessor(name, options = {}) ⇒ Object
- #assoc_reader(name, options = {}) ⇒ Object
- #assoc_writer(name, options = {}) ⇒ Object
Instance Method Details
#assoc_accessor(name, options = {}) ⇒ Object
24 25 26 27 |
# File 'lib/fleet_api/attributes.rb', line 24 def assoc_accessor(name, ={}) assoc_reader(name, ) assoc_writer(name, ) end |
#assoc_reader(name, options = {}) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/fleet_api/attributes.rb', line 2 def assoc_reader(name, ={}) assoc_key = [:key] || "#{name}_id" collection = [:collection] || "#{name}s" define_method(name) do if assoc_id = self.send(assoc_key) self.connection.send(collection).get(assoc_id) else self.instance_variable_get("@#{name}") end end end |
#assoc_writer(name, options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fleet_api/attributes.rb', line 13 def assoc_writer(name, ={}) assoc_key = [:key] || "#{name}_id" define_method("#{name}=") do |assoc| if assoc.is_a?(Cistern::Model) self.send("#{assoc_key}=", assoc.identity) else self.instance_variable_set("@#{name}", assoc) end end end |