Class: Proforma::Modeling::GenericContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/proforma/modeling/generic_container.rb

Overview

This can be used as a super-class for models which are basically containers for simple arrays of elements

Direct Known Subclasses

Table::Row, Table::Section

Instance Method Summary collapse

Constructor Details

#initialize(child_key, child_class, children = []) ⇒ GenericContainer

Returns a new instance of GenericContainer.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/proforma/modeling/generic_container.rb', line 15

def initialize(child_key, child_class, children = [])
  raise ArgumentError, 'child_key is required'   unless child_key
  raise ArgumentError, 'child_class is required' unless child_class

  @child_class = child_class
  @child_key   = child_key.to_s.to_sym
  @children    = child_class.array(children)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/proforma/modeling/generic_container.rb', line 30

def method_missing(method_name, *args, &block)
  if method_name.to_s == child_key.to_s
    Array(children)
  elsif method_name.to_s == child_setter_name
    @children = args
  else
    super
  end
end

Instance Method Details

#compile(data, evaluator) ⇒ Object



40
41
42
43
44
# File 'lib/proforma/modeling/generic_container.rb', line 40

def compile(data, evaluator)
  compiled_children = children.map { |child| child.compile(data, evaluator) }

  self.class.new(child_key => compiled_children)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/proforma/modeling/generic_container.rb', line 24

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s == child_key.to_s ||
    method_name.to_s == child_setter_name ||
    super
end