Class: ApiTwister::ApiAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/api-twister/api_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, association) ⇒ ApiAssociation

Returns a new instance of ApiAssociation.



5
6
7
8
# File 'lib/api-twister/api_association.rb', line 5

def initialize(model, association)
  @model = model
  @association = association
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



3
4
5
# File 'lib/api-twister/api_association.rb', line 3

def association
  @association
end

Instance Method Details

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



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/api-twister/api_association.rb', line 10

def api_hash(name, options={})
  hash = {}
  assoc = @model.reflect_on_association(@association)
  raise "There is no association named #{@association}" if assoc.nil?
  klass = eval(assoc.class_name)
  if klass.respond_to? :api_hash
    hash[@association] = klass.api_hash(name, options)
  else
    hash[@association] = {}
  end
end