Class: SimpleRepresenter::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_representer/field.rb

Direct Known Subclasses

Computed, Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, options) ⇒ Field

Returns a new instance of Field.



7
8
9
10
# File 'lib/simple_representer/field.rb', line 7

def initialize(field, options)
  @field = field.to_sym
  @options = options
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



5
6
7
# File 'lib/simple_representer/field.rb', line 5

def field
  @field
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/simple_representer/field.rb', line 5

def options
  @options
end

Instance Method Details

#call(representer) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/simple_representer/field.rb', line 12

def call(representer)
  return if options[:if] && !representer.instance_exec(&options[:if])

  value = process(representer)
  value = options[:default] if value.nil?
  value = nested_representer(value) if options[:representer] && !value.nil?

  build_field(value)
end