Class: Enolib::ElementBase

Inherits:
Object
  • Object
show all
Defined in:
lib/enolib/elements/element_base.rb

Direct Known Subclasses

Empty, Fieldset, List, Section, SectionElement, ValueElementBase

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ElementBase.



5
6
7
8
9
# File 'lib/enolib/elements/element_base.rb', line 5

def initialize(context, instruction, parent = nil)
  @context = context
  @instruction = instruction
  @parent = parent
end

Instance Method Details

#comment_error(message = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/enolib/elements/element_base.rb', line 11

def comment_error(message = nil)
  if block_given?
    message = yield(@context.comment(@instruction))
  elsif message.is_a?(Proc)
    message = message.call(@context.comment(@instruction))
  end

  unless message
    raise ArgumentError, 'A message or message function must be provided'
  end

  Errors::Validation.comment_error(@context, message, @instruction)
end

#error(message = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/enolib/elements/element_base.rb', line 25

def error(message = nil)
  if block_given?
    message = yield(self)  # Revisit self in this context - problematic
  elsif message.is_a?(Proc)
    message = message.call(self)  # Revisit self in this context - problematic
  end

  unless message
    raise ArgumentError, 'A message or message function must be provided'
  end

  Errors::Validation.element_error(@context, message, @instruction)
end

#key(loader = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/enolib/elements/element_base.rb', line 39

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

  @touched = true

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

  begin
    loader.call(_key)
  rescue => message
    raise Errors::Validation.key_error(@context, message, @instruction)
  end
end

#key_error(message = nil) ⇒ Object

TODO: All implementations - this could be called on the document (?), via both element and section, most likely triggering erratic behavior because the error->selection chain does not handle the missing key range? (Also being able to call it and doing so does not make sense in the first place)



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/enolib/elements/element_base.rb', line 59

def key_error(message = nil)
  if block_given?
    message = yield(_key)
  elsif message.is_a?(Proc)
    message = message.call(_key)
  end

  unless message
    raise ArgumentError, 'A message or message function must be provided'
  end

  Errors::Validation.key_error(@context, message, @instruction)
end

#optional_comment(loader = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/enolib/elements/element_base.rb', line 73

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

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

  _comment(loader, required: false)
end

#optional_string_commentObject



83
84
85
# File 'lib/enolib/elements/element_base.rb', line 83

def optional_string_comment
  _comment(required: false)
end

#rawObject



87
88
89
# File 'lib/enolib/elements/element_base.rb', line 87

def raw
  @context.raw(@instruction)
end

#required_comment(loader = nil) ⇒ Object



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

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

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

  _comment(loader, required: true)
end

#required_string_commentObject



101
102
103
# File 'lib/enolib/elements/element_base.rb', line 101

def required_string_comment
  _comment(required: true)
end

#string_keyObject



105
106
107
108
109
# File 'lib/enolib/elements/element_base.rb', line 105

def string_key
  @touched = true

  _key
end

#touchObject



111
112
113
# File 'lib/enolib/elements/element_base.rb', line 111

def touch
  @touched = true
end