Class: GenericViewMapper::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/generic_view_mapper/view/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Attribute

Returns a new instance of Attribute.



7
8
9
10
# File 'lib/generic_view_mapper/view/attribute.rb', line 7

def initialize(name, options)
  @name = name
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/generic_view_mapper/view/attribute.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/generic_view_mapper/view/attribute.rb', line 5

def options
  @options
end

Instance Method Details

#field_nameObject



12
13
14
15
16
17
18
# File 'lib/generic_view_mapper/view/attribute.rb', line 12

def field_name
  options
    .fetch(:as) { name }
    .to_s
    .camelize(:lower)
    .to_sym
end

#render(target, view_context) ⇒ Object



30
31
32
# File 'lib/generic_view_mapper/view/attribute.rb', line 30

def render(target, view_context)
  { field_name => send_to(target, view_context) }
end

#send_to(target, view_context) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/generic_view_mapper/view/attribute.rb', line 20

def send_to(target, view_context)
  name.to_s.split('.').inject(target) do |obj, method|
    if view_context.respond_to?(method)
      view_context.public_send(method)
    else
      obj.public_send(method)
    end
  end
end