Class: Enolib::Fieldset

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ElementBase

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

Constructor Details

#initialize(context, instruction, parent = nil) ⇒ Fieldset

Returns a new instance of Fieldset.



7
8
9
10
11
# File 'lib/enolib/elements/fieldset.rb', line 7

def initialize(context, instruction, parent = nil)
  super(context, instruction, parent)

  @all_entries_required = parent ? parent.all_elements_required? : false
end

Instance Attribute Details

#instructionObject (readonly)

Returns the value of attribute instruction.



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

def instruction
  @instruction
end

#touchedObject (readonly)

Returns the value of attribute touched.



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

def touched
  @touched
end

Instance Method Details

#_missing_error(entry) ⇒ Object



13
14
15
# File 'lib/enolib/elements/fieldset.rb', line 13

def _missing_error(entry)
  raise Errors::Validation.missing_element(@context, entry.key, @instruction, 'missing_fieldset_entry')
end

#_untouchedObject



17
18
19
20
21
22
23
# File 'lib/enolib/elements/fieldset.rb', line 17

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

  untouched_entry = _entries.find { |entry| !entry.instance_variable_defined?(:@touched) }

  untouched_entry ? untouched_entry.instruction : false
end

#all_entries_required(required = true) ⇒ Object



25
26
27
# File 'lib/enolib/elements/fieldset.rb', line 25

def all_entries_required(required = true)
  @all_entries_required = required
end

#assert_all_touched(message = nil, except: nil, only: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/enolib/elements/fieldset.rb', line 29

def assert_all_touched(message = nil, except: nil, only: nil)
  message = Proc.new if block_given?

  _entries(map: true).each do |key, entries|
    next if except && except.include?(key) || only && !only.include?(key)

    entries.each do |entry|
      next if entry.touched  # TODO: Revisit instance var existance question in ruby

      if message.is_a?(Proc)
        message = message.call(entry)
      end

      raise Errors::Validation.unexpected_element(@context, message, entry[:instruction])
    end
  end
end

#entries(key = nil) ⇒ Object



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

def entries(key = nil)
  @touched = true

  if key
    entries_map = _entries(map: true)
    entries_map.has_key?(key) ? entries_map[key] : []
  else
    _entries
  end
end

#entry(key = nil) ⇒ Object



58
59
60
# File 'lib/enolib/elements/fieldset.rb', line 58

def entry(key = nil)
  _entry(key)
end

#optional_entry(key) ⇒ Object



62
63
64
# File 'lib/enolib/elements/fieldset.rb', line 62

def optional_entry(key)
  _entry(key, required: false)
end

#parentObject



66
67
68
# File 'lib/enolib/elements/fieldset.rb', line 66

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

#required_entry(key = nil) ⇒ Object



70
71
72
# File 'lib/enolib/elements/fieldset.rb', line 70

def required_entry(key = nil)
  _entry(key, required: true)
end

#to_sObject



74
75
76
# File 'lib/enolib/elements/fieldset.rb', line 74

def to_s
  "#<Enolib::Fieldset key=#{@instruction[:key]} entries=#{_entries.length}>"
end

#touchObject



78
79
80
81
82
# File 'lib/enolib/elements/fieldset.rb', line 78

def touch
  @touched = true

  _entries.each(&:touch)
end