Class: Enolib::Element

Inherits:
SectionElement show all
Defined in:
lib/enolib/elements/element.rb

Instance Attribute Summary

Attributes inherited from SectionElement

#instruction

Instance Method Summary collapse

Methods inherited from SectionElement

#_untouched, #to_empty, #to_field, #to_fieldset, #to_list, #touch, #yields_empty?, #yields_field?, #yields_fieldset?, #yields_list?

Methods inherited from ElementBase

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

Constructor Details

This class inherits a constructor from Enolib::ElementBase

Instance Method Details

#to_documentObject



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

def to_document
  unless @instruction[:type] == :document
    raise Errors::Validation.unexpected_element_type(@context, nil, @instruction, 'expected_document')
  end

  unless instance_variable_defined?(:@section)
    @section = Section.new(@context, @instruction)
    @yielded = :section
  end

  @section
end

#to_fieldset_entryObject



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

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

    @fieldset_entry = FieldsetEntry.new(@context, @instruction)
  end

  @fieldset_entry
end

#to_list_itemObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/enolib/elements/element.rb', line 31

def to_list_item
  unless instance_variable_defined?(:@list_item)
    unless @instruction[:type] == :list_item
      raise Errors::Validation.unexpected_element_type(@context, nil, @instruction, 'expected_list_item')
    end

    @list_item = FieldsetEntry.new(@context, @instruction)
  end

  @list_item
end

#to_sObject



43
44
45
# File 'lib/enolib/elements/element.rb', line 43

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

#to_sectionObject



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

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

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

  @section
end

#yields_fieldset_entry?Boolean



60
61
62
# File 'lib/enolib/elements/element.rb', line 60

def yields_fieldset_entry?
  @instruction[:type] == :fieldset_entry
end

#yields_list_item?Boolean



64
65
66
# File 'lib/enolib/elements/element.rb', line 64

def yields_list_item?
  @instruction[:type] == :list_item
end

#yields_section?Boolean



68
69
70
71
# File 'lib/enolib/elements/element.rb', line 68

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