Module: FleetAPI::Attributes

Defined in:
lib/fleet_api/attributes.rb

Instance Method Summary collapse

Instance Method Details

#assoc_accessor(name, options = {}) ⇒ Object



24
25
26
27
# File 'lib/fleet_api/attributes.rb', line 24

def assoc_accessor(name, options={})
  assoc_reader(name, options)
  assoc_writer(name, options)
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, options={})
  assoc_key  = options[:key] || "#{name}_id"
  collection = options[: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, options={})
  assoc_key = options[: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