Class: FlexiView::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, &data_block) ⇒ Attribute

Returns a new instance of Attribute.



7
8
9
10
11
12
13
# File 'lib/flexi_view/attribute.rb', line 7

def initialize(name, options, &data_block)
  @name = name
  @options = options || {}
  @serialized_name = (@options[:as] || name).to_sym
  @formatter = FlexiView.formatters[@options[:format]]
  @data_block = data_block if block_given?
end

Instance Attribute Details

#data_blockObject

Returns the value of attribute data_block.



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

def data_block
  @data_block
end

#formatterObject

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#serialized_nameObject

Returns the value of attribute serialized_name.



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

def serialized_name
  @serialized_name
end

Instance Method Details

#condition?(serializer) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
# File 'lib/flexi_view/attribute.rb', line 15

def condition?(serializer)
  return true if options[:if].nil?

  if options[:if].is_a?(Proc)
    serializer.instance_exec(&options[:if])
  else
    serializer.public_send(options[:if])
  end
end

#to_data(serializer) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/flexi_view/attribute.rb', line 25

def to_data(serializer)
  value = if data_block
            serializer.instance_exec(serializer.object, &data_block)
          else
            serializer.object.public_send(name)
          end

  formatter ? formatter.format(value) : value
end