Class: Craft
- Inherits:
-
Object
- Object
- Craft
- Defined in:
- lib/craft.rb,
lib/craft/version.rb
Overview
Craft objects out of HTML and XML.
Examples
module Transformations
IntegerTransform = lambda { |n| Integer n.text }
Timestamp = lambda { Time.now }
end
class Person < Craft
include Transformations
one :name, 'div.name'
one :age, 'div.age', IntegerTransform
many :friends, 'li.friend', Person
stub :created_at, Timestamp
end
Constant Summary collapse
- VERSION =
'0.1.0'
Class Attribute Summary collapse
-
.attribute_names ⇒ Object
readonly
Returns an Array of names for the attributes defined in the class.
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
-
.many(name, *paths) ⇒ Object
Define an attribute that extracts a collection of values from a parsed document.
-
.one(name, *paths) ⇒ Object
Define an attribute that extracts a single value from a parsed document.
-
.parse(body) ⇒ Object
Parse a document.
-
.stub(name, value = nil) ⇒ Object
Define an attribute that returns a value without parsing the document.
- .to_proc ⇒ Object
Instance Method Summary collapse
-
#attribute_names ⇒ Object
Returns an Array of names for the attributes on this object.
-
#attributes ⇒ Object
Returns the Hash attributes.
-
#initialize(node, parent = nil) ⇒ Craft
constructor
Craft a new object.
Constructor Details
#initialize(node, parent = nil) ⇒ Craft
Craft a new object.
node - A Nokogiri::XML::Node.
113 114 115 116 |
# File 'lib/craft.rb', line 113 def initialize(node, parent = nil) @node = node @parent = parent end |
Class Attribute Details
.attribute_names ⇒ Object (readonly)
Returns an Array of names for the attributes defined in the class.
25 26 27 |
# File 'lib/craft.rb', line 25 def attribute_names @attribute_names end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
108 109 110 |
# File 'lib/craft.rb', line 108 def parent @parent end |
Class Method Details
.many(name, *paths) ⇒ Object
Define an attribute that extracts a collection of values from a parsed document.
name - The Symbol name of the attribute. paths - One or more String XPath of CSS queries. An optional Proc
transformation on the extracted value may be appended. If none is
appended, the default transformation returns the stripped String
value of the node.
Returns nothing.
37 38 39 40 41 42 43 44 |
# File 'lib/craft.rb', line 37 def many(name, *paths) transform = pop_transform_from_paths paths @attribute_names << name define_method name do @node.search(*paths).map { |node| instance_exec node, &transform } end end |
.one(name, *paths) ⇒ Object
Define an attribute that extracts a single value from a parsed document.
name - The Symbol name of the attribute. paths - One or more String XPath of CSS queries. An optional Proc
transformation on the extracted value may be appended. If none is
appended, the default transformation returns the stripped String
value of the node.
Returns nothing.
55 56 57 58 59 60 61 62 |
# File 'lib/craft.rb', line 55 def one(name, *paths) transform = pop_transform_from_paths paths @attribute_names << name define_method name do instance_exec @node.at(*paths), &transform end end |
.parse(body) ⇒ Object
Parse a document.
body - A String HTML or XML document.
Returns an instance of its self.
69 70 71 |
# File 'lib/craft.rb', line 69 def parse(body) new Nokogiri body end |
.stub(name, value = nil) ⇒ Object
Define an attribute that returns a value without parsing the document.
name - The Symbol name of the attribute. value - Some value the attribute should return. If given a Proc, the
value will be generated dynamically (default: nil).
Returns nothing.
80 81 82 83 84 85 86 |
# File 'lib/craft.rb', line 80 def stub(name, value = nil) @attribute_names << name define_method name do value.respond_to?(:call) ? instance_exec(&value) : value end end |
.to_proc ⇒ Object
88 89 90 91 |
# File 'lib/craft.rb', line 88 def to_proc klass = self ->(node) { klass.new node, self } end |
Instance Method Details
#attribute_names ⇒ Object
Returns an Array of names for the attributes on this object.
124 125 126 |
# File 'lib/craft.rb', line 124 def attribute_names self.class.attribute_names end |
#attributes ⇒ Object
Returns the Hash attributes.
119 120 121 |
# File 'lib/craft.rb', line 119 def attributes Hash[attribute_names.map { |key| [key, self.send(key)] }] end |