Class: HMachine::POSH::Base
- Inherits:
-
Object
- Object
- HMachine::POSH::Base
show all
- Extended by:
- HMachine
- Defined in:
- lib/hmachine/posh/base.rb
Constant Summary
Constants included
from HMachine
HMachine::PRODID, VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from HMachine
extract, extract_from, find, find_in, found_in?, get, get_document, get_url, map, normalize, parse, parse_first, search, valid?, validate
Constructor Details
#initialize(node, parent = nil) ⇒ Base
Returns a new instance of Base.
156
157
158
159
160
161
162
|
# File 'lib/hmachine/posh/base.rb', line 156
def initialize(node, parent=nil)
raise 'Uh OH' unless self.class.valid?(node)
@node = node
@source = node.document.url if node.document.url
@parent = parent if parent
@first_node = self.class.remove_nested(node.dup)
end
|
Instance Attribute Details
#node ⇒ Object
Returns the value of attribute node.
154
155
156
|
# File 'lib/hmachine/posh/base.rb', line 154
def node
@node
end
|
#parent ⇒ Object
Returns the value of attribute parent.
154
155
156
|
# File 'lib/hmachine/posh/base.rb', line 154
def parent
@parent
end
|
#source ⇒ Object
Returns the value of attribute source.
154
155
156
|
# File 'lib/hmachine/posh/base.rb', line 154
def source
@source
end
|
Class Method Details
.[](key) ⇒ Object
79
80
81
|
# File 'lib/hmachine/posh/base.rb', line 79
def [](key)
properties[key]
end
|
.add_property(property_name, &block) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/hmachine/posh/base.rb', line 62
def add_property(property_name, &block)
if property_name.respond_to?(:property_of?)
property = property_name
else
property = Class.new(HMachine::POSH::Base)
property.name property_name
property.parent self
property.instance_eval(&block) if block_given?
end
properties[property.name] = property
property
end
|
Instead of getting the contents of a node, this creates a POSH format from the node
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/hmachine/posh/base.rb', line 31
def (pattern = nil, &block)
if !@extract
if pattern
@extract = HMachine.map(pattern).
elsif block_given?
@extract = block
elsif properties.empty?
@extract = HMachine::Pattern::ValueClass.
end
end
@extract || lambda{|node| self.new(node) }
end
|
.get_properties(node, props = properties) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/hmachine/posh/base.rb', line 138
def get_properties(node, props=properties)
hash ||= {}
props.each_pair do |key, property|
hash[key] = if one && one.include?(property)
property.parse_first(node)
else
property.parse(node)
end
end
hash.reject do |key,value|
value.respond_to?(:empty?) ? value.empty? : value.nil?
end
end
|
.has_many(*property_names, &block) ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/hmachine/posh/base.rb', line 98
def has_many(*property_names, &block)
property_names.collect do |name|
property = has_many!(name, &block)
define_method property.name do
self[property.name]
end
property
end
end
|
.has_many!(property_name, &block) ⇒ Object
115
116
117
118
119
120
|
# File 'lib/hmachine/posh/base.rb', line 115
def has_many!(property_name, &block)
@has_many ||= []
property = add_property(property_name, &block)
@has_many << property
property
end
|
.has_one(*property_names, &block) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/hmachine/posh/base.rb', line 88
def has_one(*property_names, &block)
property_names.collect do |name|
property = has_one!(name, &block)
define_method property.name do
self[property.name]
end
property
end
end
|
.has_one!(property_name, &block) ⇒ Object
108
109
110
111
112
113
|
# File 'lib/hmachine/posh/base.rb', line 108
def has_one!(property_name, &block)
@has_one ||= []
property = add_property(property_name, &block)
@has_one << property
property
end
|
.inherited(subclass) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/hmachine/posh/base.rb', line 22
def inherited(subclass)
inheritable = [:@properties, :@has_one, :@has_many, :@search, :@extract, :@validate]
inheritable.each do |var|
subclass.instance_variable_set(var, instance_variable_get(var).dup) if instance_variable_get(var)
end
end
|
.many ⇒ Object
126
127
128
|
# File 'lib/hmachine/posh/base.rb', line 126
def many
@has_many
end
|
.name(root = nil) ⇒ Object
.one ⇒ Object
122
123
124
|
# File 'lib/hmachine/posh/base.rb', line 122
def one
@has_one
end
|
.parent(owner = nil) ⇒ Object
17
18
19
20
|
# File 'lib/hmachine/posh/base.rb', line 17
def parent(owner=nil)
@parent = owner.respond_to?(:extract) ? owner : HMachine.map(owner) if owner
@parent
end
|
.properties ⇒ Object
75
76
77
|
# File 'lib/hmachine/posh/base.rb', line 75
def properties
@properties ||= {}
end
|
.property_of?(format) ⇒ Boolean
Also known as:
child_of?
83
84
85
|
# File 'lib/hmachine/posh/base.rb', line 83
def property_of?(format)
HMachine.map(format) == parent
end
|
.remove_nested(node) ⇒ Object
Remove a format from a node if it is nested.
131
132
133
134
135
136
|
# File 'lib/hmachine/posh/base.rb', line 131
def remove_nested(node)
if (find_in(node) != node)
find_in(node).unlink if found_in?(node)
end
node
end
|
.search(&block) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/hmachine/posh/base.rb', line 44
def search(&block)
@search = block if block_given?
@search || lambda do |node|
if selector
node.css(selector)
elsif name
node.css(".#{name}".gsub('_','-'))
else
node
end
end
end
|
.selector(css = nil) ⇒ Object
12
13
14
15
|
# File 'lib/hmachine/posh/base.rb', line 12
def selector(css=nil)
@selector = css if css
@selector
end
|
.validate(&block) ⇒ Object
57
58
59
60
|
# File 'lib/hmachine/posh/base.rb', line 57
def validate(&block)
@validate = block if block_given?
@validate || lambda {|node| selector ? node.matches?(selector) : found_in?(node.parent) }
end
|
Instance Method Details
#[](property_key) ⇒ Object
164
165
166
|
# File 'lib/hmachine/posh/base.rb', line 164
def [](property_key)
to_h[property_key]
end
|
#each_pair(&block) ⇒ Object
198
199
200
|
# File 'lib/hmachine/posh/base.rb', line 198
def each_pair(&block)
to_h.each_pair(&block)
end
|
#empty? ⇒ Boolean
194
195
196
|
# File 'lib/hmachine/posh/base.rb', line 194
def empty?
to_h.empty?
end
|
#has_property?(key) ⇒ Boolean
182
183
184
|
# File 'lib/hmachine/posh/base.rb', line 182
def has_property?(key)
to_h.has_key?(key)
end
|
#inspect ⇒ Object
178
179
180
|
# File 'lib/hmachine/posh/base.rb', line 178
def inspect
to_h.inspect
end
|
#properties ⇒ Object
168
169
170
171
172
|
# File 'lib/hmachine/posh/base.rb', line 168
def properties
@properties ||= self.class.properties.reject { |key, property|
!property.found_in?(@first_node)
}
end
|
#to_h ⇒ Object
174
175
176
|
# File 'lib/hmachine/posh/base.rb', line 174
def to_h
@to_h ||= self.class.get_properties @first_node, properties
end
|
#to_html ⇒ Object
190
191
192
|
# File 'lib/hmachine/posh/base.rb', line 190
def to_html
node.to_html
end
|
#to_s ⇒ Object
186
187
188
|
# File 'lib/hmachine/posh/base.rb', line 186
def to_s
node.to_s
end
|