Class: NaranyaEcm::Rest::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/naranya_ecm/rest/associations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options = {}) ⇒ Association

Returns a new instance of Association.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/naranya_ecm/rest/associations.rb', line 14

def initialize(klass, name, options = {})
  @klass, @name = klass, name

  @associated_class = if (given_class_name = options.delete(:class_name))
    resolve_class_name(given_class_name)
  else
    resolve_class_name(name.to_s.classify.to_sym)
  end

  @options = options.with_indifferent_access

  klass.send :attr_accessor, name

end

Instance Attribute Details

#associated_classObject (readonly)

Returns the value of attribute associated_class.



7
8
9
# File 'lib/naranya_ecm/rest/associations.rb', line 7

def associated_class
  @associated_class
end

#klassObject (readonly)

Returns the value of attribute klass.



7
8
9
# File 'lib/naranya_ecm/rest/associations.rb', line 7

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/naranya_ecm/rest/associations.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/naranya_ecm/rest/associations.rb', line 7

def options
  @options
end

Instance Method Details

#resolve_class_name(given_class_name) ⇒ Object



9
10
11
12
# File 'lib/naranya_ecm/rest/associations.rb', line 9

def resolve_class_name(given_class_name)
  given_class_name = "#{@klass.name.deconstantize}::#{given_class_name}" if given_class_name.is_a?(Symbol)
  given_class_name.constantize
end