Class: IMW::Recordizer::HTMLSelectorRecordizer
- Inherits:
-
Object
- Object
- IMW::Recordizer::HTMLSelectorRecordizer
- Defined in:
- lib/imw/recordizer/html_selector_recordizer.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ HTMLSelectorRecordizer
constructor
A new instance of HTMLSelectorRecordizer.
- #recordize(doc) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize ⇒ HTMLSelectorRecordizer
Returns a new instance of HTMLSelectorRecordizer.
17 18 19 |
# File 'lib/imw/recordizer/html_selector_recordizer.rb', line 17 def initialize self.class.rules.each { |name, (s, k, plural)| send("#{name}=", []) if plural } end |
Class Method Details
.element(*args, &block) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/imw/recordizer/html_selector_recordizer.rb', line 5 def self.element(*args, &block) selector, name, delegate = parse_rule_declaration(*args, &block) rules[name] = [selector, delegate] attr_accessor name name end |
.elements(*args, &block) ⇒ Object
12 13 14 15 |
# File 'lib/imw/recordizer/html_selector_recordizer.rb', line 12 def self.elements(*args, &block) name = element(*args, &block) rules[name] << true end |
.recordize(doc) ⇒ Object
21 22 23 |
# File 'lib/imw/recordizer/html_selector_recordizer.rb', line 21 def self.recordize(doc) self.new.recordize(doc) end |
Instance Method Details
#recordize(doc) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/imw/recordizer/html_selector_recordizer.rb', line 25 def recordize(doc) self.class.rules.each do |target, (selector, delegate, plural)| if plural send(target).concat doc.search(selector).map { |i| parse_result(i, delegate) } else send("#{target}=", parse_result(doc.at(selector), delegate)) end end self.to_hash end |
#to_hash ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/imw/recordizer/html_selector_recordizer.rb', line 36 def to_hash converter = lambda { |obj| obj.respond_to?(:to_hash) ? obj.to_hash : obj } self.class.rules.keys.inject({}) do |hash, name| value = send(name) hash[name.to_sym] = Array === value ? value.map(&converter) : converter[value] hash end end |