Class: Enolib::List

Inherits:
ElementBase show all
Defined in:
lib/enolib/elements/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ElementBase

#comment_error, #error, #initialize, #key, #key_error, #optional_string_comment, #raw, #required_comment, #required_string_comment, #string_key

Constructor Details

This class inherits a constructor from Enolib::ElementBase

Instance Attribute Details

#instructionObject (readonly)

Returns the value of attribute instruction.



5
6
7
# File 'lib/enolib/elements/list.rb', line 5

def instruction
  @instruction
end

Instance Method Details

#_instantiate_items(list) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/enolib/elements/list.rb', line 7

def _instantiate_items(list)
  if list.has_key?(:mirror)
    _instantiate_items(list[:mirror])
  elsif list.has_key?(:extend)
    _instantiate_items(list[:extend]) + list[:items].map { |item| ListItem.new(@context, item, self) }
  elsif list.has_key?(:items)
    list[:items].map { |item| ListItem.new(@context, item, self) }
  else
    []
  end
end

#_itemsObject



19
20
21
22
23
24
25
# File 'lib/enolib/elements/list.rb', line 19

def _items
  unless instance_variable_defined?(:@instantiated_items)
    @instantiated_items = _instantiate_items(@instruction)
  end

  @instantiated_items
end

#_untouchedObject



27
28
29
30
31
32
33
# File 'lib/enolib/elements/list.rb', line 27

def _untouched
  return @instruction unless instance_variable_defined?(:@touched)

  untouched_item = _items.find { |item| !item.instance_variable_defined?(:@touched) }

  untouched_item ? untouched_item.instruction : false
end

#itemsObject



35
36
37
38
39
# File 'lib/enolib/elements/list.rb', line 35

def items
  @touched = true

  _items
end

#lengthObject



41
42
43
44
45
# File 'lib/enolib/elements/list.rb', line 41

def length
  @touched = true

  _items.length
end

#optional_comment(loader = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/enolib/elements/list.rb', line 47

def optional_comment(loader = nil)
  loader = Proc.new if block_given?

  if loader
    _comment(loader, required: false)
  else
    raise ArgumentError, 'A loader block or Proc must be provided'
  end
end

#optional_string_valuesObject



57
58
59
60
61
# File 'lib/enolib/elements/list.rb', line 57

def optional_string_values
  @touched = true

  _items.map(&:optional_string_value)
end

#optional_values(loader = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/enolib/elements/list.rb', line 63

def optional_values(loader = nil)
  loader = Proc.new if block_given?

  @touched = true

  unless loader
    raise ArgumentError, 'A loader function must be provided'
  end

  _items.map { |item| item.optional_value(loader) }
end

#parentObject



75
76
77
# File 'lib/enolib/elements/list.rb', line 75

def parent
  @parent || Section.new(@context, @instruction[:parent])
end

#required_string_valuesObject



79
80
81
82
83
# File 'lib/enolib/elements/list.rb', line 79

def required_string_values
  @touched = true

  _items.map(&:required_string_value)
end

#required_values(loader = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/enolib/elements/list.rb', line 85

def required_values(loader = nil)
  loader = Proc.new if block_given?

  @touched = true

  unless loader
    raise ArgumentError, 'A loader function must be provided'
  end

  _items.map { |item| item.required_value(loader) }
end

#to_sObject



97
98
99
# File 'lib/enolib/elements/list.rb', line 97

def to_s
  "#<Enolib::List key=#{@instruction[:key]} items=#{_items.length}>"
end

#touchObject



101
102
103
104
105
# File 'lib/enolib/elements/list.rb', line 101

def touch
  @touched = true

  _items.each(&:touch)
end