Class: Hexp::Node
- Inherits:
-
Object
- Object
- Hexp::Node
- Extended by:
- Forwardable
- Includes:
- Attributes, Children
- Defined in:
- lib/hexp/node.rb,
lib/hexp-rails.rb,
lib/hexp/node/pp.rb,
lib/hexp/node/domize.rb,
lib/hexp/node/children.rb,
lib/hexp/node/rewriter.rb,
lib/hexp/node/normalize.rb,
lib/hexp/node/selection.rb,
lib/hexp/node/attributes.rb,
lib/hexp/node/css_selection.rb
Overview
Transparently use Hexp nodes in templates
Defined Under Namespace
Modules: Attributes, Children Classes: CssSelection, Domize, Normalize, PP, Rewriter, Selection
Class Method Summary collapse
-
.[](*args) ⇒ Hexp::Node
Main entry point for creating literal hexps.
-
.inspect_name ⇒ String
private
Returns the class name for use in creating inspection strings.
Instance Method Summary collapse
- #concord_init ⇒ Object
-
#initialize(*args) ⇒ Hexp::Node
constructor
Normalize the arguments.
-
#inspect ⇒ String
Return a string representation that is close to the literal form.
-
#pp ⇒ String
Pretty print, a multiline representation with indentation.
-
#process(*processors) ⇒ Hexp::Node
Run a number of processors on this node.
-
#rewrite(css_selector = nil) {|The| ... } ⇒ Hexp::Node
(also: #replace)
Replace nodes in a tree.
-
#select(css_selector = nil) {|| ... } ⇒ Hexp::Selector, Hexp::CssSelector
Select nodes based on a css selector.
-
#set_tag(tag) ⇒ Hexp::Node
Return a new node, with a different tag.
- #tag?(tag) ⇒ Boolean
-
#text? ⇒ FalseClass
Is this a text node? Returns false.
-
#to_dom(options = {}) ⇒ Nokogiri::HTML::Document
private
Convert this node into a Nokogiri Document.
-
#to_hexp ⇒ Hexp::Node
Standard hexp coercion protocol, return self.
-
#to_html(options = {}) ⇒ String
Serialize this node to HTML.
- #to_s ⇒ Object
Methods included from Children
#add_child, #append, #content, #empty?, #map_children, #text
Methods included from Attributes
#[], #add_class, #attr, #class?, #class_list, #has_attr?, #merge_attrs, #remove_attr, #remove_class, #set_attrs
Constructor Details
#initialize(*args) ⇒ Hexp::Node
Normalize the arguments
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/hexp/node.rb', line 127 def initialize(*args) tag_ok = args[0].instance_of?(Symbol) raise "The tag of node should be a Symbol" unless tag_ok attrs_ok = args[1].instance_of?(Hash) && args[1].all? {|k,v| k.instance_of?(String) && v.instance_of?(String) } if attrs_ok && args[2].instance_of?(List) concord_init(args[0], args[1], args[2]) elsif attrs_ok && args[2].instance_of?(Array) concord_init(args[0], args[1], List.new(args[2])) else concord_init(*Normalize.new(args).call) end.freeze end |
Class Method Details
.[](*args) ⇒ Hexp::Node
Main entry point for creating literal hexps
At the moment this just redirects to #new, and since Hexp::Node is aliased to H this provides a shorthand for the contructor,
Note that while the H[] form is part of the public API and expected to remain, it’s implementation might change. In particular H might become a class or module in its own right, so it is recommended to only use this method in its H[] form.
111 112 113 |
# File 'lib/hexp/node.rb', line 111 def self.[](*args) new(*args) end |
.inspect_name ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the class name for use in creating inspection strings
This will return “H” if H == Hexp::Node, or “Hexp::Node” otherwise.
340 341 342 343 344 345 346 |
# File 'lib/hexp/node.rb', line 340 def inspect_name if defined?(H) 'H' else self.name end end |
Instance Method Details
#concord_init ⇒ Object
115 |
# File 'lib/hexp/node.rb', line 115 alias concord_init initialize |
#inspect ⇒ String
Return a string representation that is close to the literal form
191 192 193 |
# File 'lib/hexp/node.rb', line 191 def inspect self.class.inspect_name + [tag, attributes, children].compact.reject(&:empty?).inspect end |
#pp ⇒ String
Pretty print, a multiline representation with indentation
203 204 205 |
# File 'lib/hexp/node.rb', line 203 def pp self.class::PP.new(self).call end |
#process(*processors) ⇒ Hexp::Node
Run a number of processors on this node
This is pure convenience, but it helps to conceptualize the “processor” idea of a component (be it a lambda or other object), that responds to call, and transform a Hexp::Node tree.
303 304 305 |
# File 'lib/hexp/node.rb', line 303 def process(*processors) processors.empty? ? self : processors.first.(self).process(*processors.drop(1)) end |
#rewrite(css_selector = nil) {|The| ... } ⇒ Hexp::Node Also known as: replace
Replace nodes in a tree
With a CSS selector string like “form.checkout” you specify the nodes you want to operate on. These will be passed one by one into the block. The block returns the Hexp::Node that will replace the old node, or it can replace an Array
of nodes to fill the place of the old node.
Because of this you can add one or more nodes, or remove nodes by returning an empty array.
264 265 266 267 |
# File 'lib/hexp/node.rb', line 264 def rewrite(css_selector = nil, &block) return Rewriter.new(self, block) if css_selector.nil? CssSelection.new(self, css_selector).rewrite(&block) end |
#select(css_selector = nil) {|| ... } ⇒ Hexp::Selector, Hexp::CssSelector
Select nodes based on a css selector
278 279 280 281 282 283 284 |
# File 'lib/hexp/node.rb', line 278 def select(css_selector = nil, &block) if css_selector CssSelection.new(self, css_selector).each(&block) else Selection.new(self, block) end end |
#set_tag(tag) ⇒ Hexp::Node
Return a new node, with a different tag
233 234 235 |
# File 'lib/hexp/node.rb', line 233 def set_tag(tag) H[tag.to_sym, attributes, children] end |
#tag?(tag) ⇒ Boolean
219 220 221 |
# File 'lib/hexp/node.rb', line 219 def tag?(tag) self.tag == tag end |
#text? ⇒ FalseClass
Is this a text node? Returns false
215 216 217 |
# File 'lib/hexp/node.rb', line 215 def text? false end |
#to_dom(options = {}) ⇒ Nokogiri::HTML::Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert this node into a Nokogiri Document
179 180 181 |
# File 'lib/hexp/node.rb', line 179 def to_dom( = {}) Domize.new(self, ).call end |
#to_hexp ⇒ Hexp::Node
Standard hexp coercion protocol, return self
151 152 153 |
# File 'lib/hexp/node.rb', line 151 def to_hexp self end |
#to_html(options = {}) ⇒ String
Serialize this node to HTML
164 165 166 |
# File 'lib/hexp/node.rb', line 164 def to_html( = {}) Unparser.new().call(self) end |
#to_s ⇒ Object
7 8 9 |
# File 'lib/hexp-rails.rb', line 7 def to_s to_html.html_safe end |