Class: Phlexi::Field::Structure::DOM
- Inherits:
-
Object
- Object
- Phlexi::Field::Structure::DOM
- Defined in:
- lib/phlexi/field/structure/dom.rb
Overview
Generates DOM IDs, names, etc. for a Field, Namespace, or Node based on norms that were established by Rails. These can be used outside of or Rails in other Ruby web frameworks since it has no dependencies on Rails.
Direct Known Subclasses
Instance Method Summary collapse
-
#id ⇒ Object
Walks from the current node to the parent node, grabs the names, and separates them with a ‘_` for a DOM ID.
-
#initialize(field:) ⇒ DOM
constructor
A new instance of DOM.
-
#inspect ⇒ Object
Emit the id, name, and value in an HTML tag-ish that doesnt have an element.
-
#lineage ⇒ Object
One-liner way of walking from the current node all the way up to the parent.
-
#name ⇒ Object
The ‘name` attribute of a node, which is influenced by Rails.
-
#value ⇒ Object
Converts the value of the field to a String, which is required to work with Phlex.
Constructor Details
#initialize(field:) ⇒ DOM
Returns a new instance of DOM.
10 11 12 |
# File 'lib/phlexi/field/structure/dom.rb', line 10 def initialize(field:) @field = field end |
Instance Method Details
#id ⇒ Object
Walks from the current node to the parent node, grabs the names, and separates them with a ‘_` for a DOM ID.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/phlexi/field/structure/dom.rb', line 22 def id @id ||= begin root, *rest = lineage root_key = if root.respond_to?(:dom_id) root.dom_id else root.key end rest.map(&:key).unshift(root_key).join("_") end end |
#inspect ⇒ Object
Emit the id, name, and value in an HTML tag-ish that doesnt have an element.
51 52 53 |
# File 'lib/phlexi/field/structure/dom.rb', line 51 def inspect "<#{self.class.name} id=#{id.inspect} name=#{name.inspect} value=#{value.inspect}/>" end |
#lineage ⇒ Object
One-liner way of walking from the current node all the way up to the parent.
46 47 48 |
# File 'lib/phlexi/field/structure/dom.rb', line 46 def lineage @lineage ||= Enumerator.produce(@field, &:parent).take_while(&:itself).reverse end |
#name ⇒ Object
The ‘name` attribute of a node, which is influenced by Rails. All node names, except the parent node, are wrapped in a `[]` and collections are left empty. For example, `user[][street]` would be created for a form with data shaped like `{addresses: [{street: “Sesame Street”]}}`.
38 39 40 41 42 43 |
# File 'lib/phlexi/field/structure/dom.rb', line 38 def name @name ||= begin root, *names = keys names.map { |name| "[#{name}]" }.unshift(root).join end end |
#value ⇒ Object
Converts the value of the field to a String, which is required to work with Phlex. Assumes that ‘Object#to_s` emits a format suitable for display.
16 17 18 |
# File 'lib/phlexi/field/structure/dom.rb', line 16 def value @field.value.to_s end |