Class: Enolib::SectionElement

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

Direct Known Subclasses

Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ElementBase

#comment_error, #error, #initialize, #key, #key_error, #optional_comment, #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)

TODO: Revisit this hacky exposition



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

def instruction
  @instruction
end

Instance Method Details

#_untouchedObject



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

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

  return @instruction if instance_variable_defined?(:@empty) && !@empty.instance_variable_defined?(:@touched)
  return @instruction if instance_variable_defined?(:@field) && !@field.instance_variable_defined?(:@touched)
  return @fieldset._untouched if instance_variable_defined?(:@fieldset)
  return @list._untouched if instance_variable_defined?(:@list)
  return @section._untouched if instance_variable_defined?(:@section)
end

#to_emptyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/enolib/elements/section_element.rb', line 19

def to_empty
  unless instance_variable_defined?(:@empty)
    unless @instruction[:type] == :empty
      # TODO: Below and in all implementations - why nil for key as second parameter?
      raise Errors::Validation.unexpected_element_type(@context, nil, @instruction, 'expected_empty')
    end

    @empty = Empty.new(@context, @instruction, @parent)
    @yielded = :empty
  end

  @empty
end

#to_fieldObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/enolib/elements/section_element.rb', line 33

def to_field
  unless instance_variable_defined?(:@field)
    if instance_variable_defined?(:@yielded)
      raise TypeError, "This element was already yielded as #{PRETTY_TYPES[@yielded]} and can't be yielded again as a field."
    end

    unless @instruction[:type] == :field ||
           @instruction[:type] == :multiline_field_begin ||
           @instruction[:type] == :field_or_fieldset_or_list
      raise Errors::Validation.unexpected_element_type(@context, nil, @instruction, 'expected_field')
    end

    @field = Field.new(@context, @instruction, @parent)
    @yielded = :field
  end

  @field
end

#to_fieldsetObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/enolib/elements/section_element.rb', line 52

def to_fieldset
  unless instance_variable_defined?(:@fieldset)
    if instance_variable_defined?(:@yielded)
      raise TypeError, "This element was already yielded as #{PRETTY_TYPES[@yielded]} and can't be yielded again as a fieldset."
    end

    unless @instruction[:type] == :fieldset || @instruction[:type] == :field_or_fieldset_or_list
      raise Errors::Validation.unexpected_element_type(@context, nil, @instruction, 'expected_fieldset')
    end

    @fieldset = Fieldset.new(@context, @instruction, @parent)
    @yielded = :fieldset
  end

  @fieldset
end

#to_listObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/enolib/elements/section_element.rb', line 69

def to_list
  unless instance_variable_defined?(:@list)
    if instance_variable_defined?(:@yielded)
      raise TypeError, "This element was already yielded as #{PRETTY_TYPES[@yielded]} and can't be yielded again as a list."
    end

    unless @instruction[:type] == :list || @instruction[:type] == :field_or_fieldset_or_list
      raise Errors::Validation.unexpected_element_type(@context, nil, @instruction, 'expected_list')
    end

    @list = List.new(@context, @instruction, @parent)
    @yielded = :list
  end

  @list
end

#to_sObject



86
87
88
# File 'lib/enolib/elements/section_element.rb', line 86

def to_s
  "#<Enolib::SectionElement key=#{_key} yields=#{_yields}>"
end

#to_sectionObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/enolib/elements/section_element.rb', line 90

def to_section
  unless instance_variable_defined?(:@section)
    unless @instruction[:type] == :section
      raise Errors::Validation.unexpected_element_type(@context, nil, @instruction, 'expected_section')
    end

    @section = Section.new(@context, @instruction, @parent)
    @yielded = :section
  end

  @section
end

#touchObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/enolib/elements/section_element.rb', line 103

def touch
  # TODO: Revisit setting touched on foreign instances
  if !instance_variable_defined?(:@yielded)
    @touched = true
  elsif instance_variable_defined?(:@empty)
    @empty.touched = true
  elsif instance_variable_defined?(:@field)
    @field.touched = true
  elsif instance_variable_defined?(:@fieldset)
    @fieldset.touch
  elsif instance_variable_defined?(:@list)
    @list.touch
  elsif instance_variable_defined?(:@section)
    @section.touch
  end
end

#yields_empty?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/enolib/elements/section_element.rb', line 120

def yields_empty?
  @instruction[:type] == :empty
end

#yields_field?Boolean

Returns:

  • (Boolean)


124
125
126
127
128
# File 'lib/enolib/elements/section_element.rb', line 124

def yields_field?
  @instruction[:type] == :field ||
  @instruction[:type] == :multiline_field_begin ||
  @instruction[:type] == :field_or_fieldset_or_list
end

#yields_fieldset?Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/enolib/elements/section_element.rb', line 130

def yields_fieldset?
  @instruction[:type] == :fieldset ||
  @instruction[:type] == :field_or_fieldset_or_list
end

#yields_list?Boolean

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/enolib/elements/section_element.rb', line 135

def yields_list?
  @instruction[:type] == :list ||
  @instruction[:type] == :field_or_fieldset_or_list
end

#yields_section?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/enolib/elements/section_element.rb', line 140

def yields_section?
  @instruction[:type] == :section
end