Class: Motion::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Element

Returns a new instance of Element.



13
14
15
# File 'lib/motion/element.rb', line 13

def initialize(raw)
  @raw = raw.freeze
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



11
12
13
# File 'lib/motion/element.rb', line 11

def raw
  @raw
end

Class Method Details

.from_raw(raw) ⇒ Object



7
8
9
# File 'lib/motion/element.rb', line 7

def self.from_raw(raw)
  new(raw) if raw
end

Instance Method Details

#[](key) ⇒ Object



33
34
35
36
37
# File 'lib/motion/element.rb', line 33

def [](key)
  key = key.to_s

  attributes[key] || attributes[key.tr("_", "-")]
end

#attributesObject



29
30
31
# File 'lib/motion/element.rb', line 29

def attributes
  raw.fetch("attributes", {})
end

#checked?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/motion/element.rb', line 25

def checked?
  raw["checked"]
end

#dataObject



57
58
59
60
61
# File 'lib/motion/element.rb', line 57

def data
  return @data if defined?(@data)

  @data = DataAttributes.new(self)
end

#form_dataObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/motion/element.rb', line 63

def form_data
  return @form_data if defined?(@form_data)

  @form_data =
    ActionController::Parameters.new(
      Rack::Utils.parse_nested_query(
        raw.fetch("formData", "")
      )
    )
end

#idObject



39
40
41
# File 'lib/motion/element.rb', line 39

def id
  self[:id]
end

#tag_nameObject



17
18
19
# File 'lib/motion/element.rb', line 17

def tag_name
  raw["tagName"]
end

#valueObject



21
22
23
# File 'lib/motion/element.rb', line 21

def value
  raw["value"]
end