Class: Shark::FormService::Form::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/shark/form_service/form/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, parent = nil) ⇒ Element

Returns a new instance of Element.



9
10
11
12
13
# File 'lib/shark/form_service/form/element.rb', line 9

def initialize(element, parent = nil)
  @element = element.except('elements') || {}
  @parent = parent
  @children = parse_children(element['elements'] || [])
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/shark/form_service/form/element.rb', line 7

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/shark/form_service/form/element.rb', line 7

def parent
  @parent
end

Instance Method Details

#ancestorsObject



50
51
52
53
54
# File 'lib/shark/form_service/form/element.rb', line 50

def ancestors
  return parent.ancestors << self if parent.present?

  [self]
end

#attribute_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/shark/form_service/form/element.rb', line 46

def attribute_defined?(name)
  attribute_definition(name).present?
end

#attribute_definition(name) ⇒ Object



42
43
44
# File 'lib/shark/form_service/form/element.rb', line 42

def attribute_definition(name)
  attribute_definitions.detect { |a| a['name'] == name }
end

#attribute_definitionsObject



38
39
40
# File 'lib/shark/form_service/form/element.rb', line 38

def attribute_definitions
  @element['attribute_definitions'] || []
end

#idObject



15
16
17
# File 'lib/shark/form_service/form/element.rb', line 15

def id
  @element['id']
end

#labelObject



19
20
21
22
23
24
25
26
# File 'lib/shark/form_service/form/element.rb', line 19

def label
  %w[label legend].each do |attr|
    next unless attribute_defined?(attr)

    return attribute_definition(attr)['value']
  end
  ''
end

#textObject



32
33
34
35
36
# File 'lib/shark/form_service/form/element.rb', line 32

def text
  return '' unless attribute_defined?('text')

  attribute_definition('text')['value']
end

#typeObject



28
29
30
# File 'lib/shark/form_service/form/element.rb', line 28

def type
  @element['type']
end