Class: Scrapework::Object
- Inherits:
-
Object
- Object
- Scrapework::Object
- Includes:
- ActiveAttr::Model
- Defined in:
- lib/scrapework/object.rb
Overview
Base class for web data type rubocop:disable Metrics/ClassLength
Class Method Summary collapse
-
.belongs_to(object, options = {}) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
-
.has_many(objects, options = {}) ⇒ Object
rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
-
.has_one(object, options = {}) ⇒ Object
rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
- .load(url) ⇒ Object
-
.map(name, &block) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize.
-
.paginate(&block) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
- .reflections ⇒ Object
Instance Method Summary collapse
Class Method Details
.belongs_to(object, options = {}) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/scrapework/object.rb', line 22 def self.belongs_to(object, = {}) ivar = "@#{object}" mapped_method = "_mapped_#{object}" reflection_class = .fetch(:class) { object.to_s.classify }.to_s attr_writer object define_method(object) do return instance_variable_get(ivar) if instance_variable_defined?(ivar) attributes = __send__(mapped_method) return if attributes.nil? instance = reflection_class.constantize.new(attributes) instance_variable_set(ivar, instance) end reflections[object] = { type: 'belongs_to', class: reflection_class } end |
.has_many(objects, options = {}) ⇒ Object
rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/scrapework/object.rb', line 47 def self.has_many(objects, = {}) ivar = "@#{objects}" reflection_class = .fetch(:class) do objects.to_s.singularize.classify end.to_s inverse_reflection = self.class.name.underscore attr_writer objects define_method(objects) do return instance_variable_get(ivar) if instance_variable_defined?(ivar) instances = __send__("_mapped_#{objects}").map do |attributes| next if attributes.nil? instance = reflection_class.constantize.new(attributes) if instance.class.reflections.include?(inverse_reflection) instance.public_send("#{inverse_reflection}=", self) end instance end instance_variable_set(ivar, instances.compact) end reflections[objects] = { type: 'has_many', class: reflection_class } end |
.has_one(object, options = {}) ⇒ Object
rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/scrapework/object.rb', line 80 def self.has_one(object, = {}) ivar = "@#{object}" mapped_method = "_mapped_#{object}" reflection_class = .fetch(:class) { object.to_s.classify }.to_s inverse_reflection = self.class.name.underscore attr_writer object define_method(object) do return instance_variable_get(ivar) if instance_variable_defined?(ivar) attributes = __send__(mapped_method) return if attributes.nil? instance = reflection_class.constantize.new(attributes) if instance.class.reflections.include?(inverse_reflection) instance.public_send("#{inverse_reflection}=", self) end instance_variable_set(ivar, instance) end reflections[object] = { type: 'has_one', class: reflection_class } end |
.load(url) ⇒ Object
144 145 146 147 148 |
# File 'lib/scrapework/object.rb', line 144 def self.load(url) instance = new(url: url) instance.load instance end |
.map(name, &block) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/scrapework/object.rb', line 129 def self.map(name, &block) mapped_method = :"_mapped_#{name}" define_method(mapped_method) do value = begin instance_exec(_document, &block) rescue StandardError => e raise MappingError, "failed to scrape `#{name}`: #{e.}" end public_send("#{name}=", value) end private mapped_method end |
.paginate(&block) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/scrapework/object.rb', line 109 def self.paginate(&block) mapped_method = :_pagination define_method(mapped_method) do @_pagination ||= instance_exec(_document, &block) end define_method(:prev_page) do pages = __send__(mapped_method) self.class.new(pages[0]) if pages[0] end define_method(:next_page) do pages = __send__(mapped_method) self.class.new(pages[1]) if pages[1] end end |
.reflections ⇒ Object
16 17 18 |
# File 'lib/scrapework/object.rb', line 16 def self.reflections @reflections ||= {} end |
Instance Method Details
#_document ⇒ Object
150 151 152 |
# File 'lib/scrapework/object.rb', line 150 def _document @_document ||= Nokogiri::HTML(html) end |
#html ⇒ Object
154 155 156 157 |
# File 'lib/scrapework/object.rb', line 154 def html uri = URI.parse(url) uri.read end |
#load ⇒ Object
159 160 161 162 163 |
# File 'lib/scrapework/object.rb', line 159 def load attributes.except('url').each do |attribute, value| __send__("_mapped_#{attribute}") if value.nil? end end |