lib/arstotzka/fetcher.rb in arstotzka-1.1.0 vs lib/arstotzka/fetcher.rb in arstotzka-1.2.0

- old
+ new

@@ -8,29 +8,31 @@ class Fetcher include Base # Creates an instance of Artotzka::Fetcher # - # @param hash [Hash] Hash to be crawled for value # @param instance [Object] object whose methods will be called after for processing - # @param options_hash [Hash] options that will be passed to {Crawler}, {Wrapper} and {Reader} - def initialize(hash, instance, options_hash = {}) + # + # @overload iniitalize(instance, options_hash = {}) + # @param options_hash [Hash] options for {Crawler}, {Wrapper} and {Reader} + # + # @overload iniitalize(instance, options) + # @param options [Arstotzka::Options] options for {Crawler}, {Wrapper} and {Reader} + def initialize(instance, options_hash = {}) self.options = options_hash - @keys = options.path.to_s.split('.') - @hash = hash @instance = instance end # Crawls the hash for the value # # After the crawling, final transformation is applied on # the final result (collection not value) # # @return [Object] The final value found and transformed # - # @example + # @example Fetching with wrapping and processing # class Transaction # attr_reader :value, :type # # def initialize(value:, type:) # @value = value @@ -41,12 +43,18 @@ # type == 'income' # end # end # # class Account + # def initialize(json = {}) + # @json = json + # end + # # private # + # attr_reader :json + # # def filter_income(transactions) # transactions.select(&:positive?) # end # end # @@ -59,12 +67,14 @@ # { value: 10.23, type: 'outcome' }, # { value: 100.12, type: 'outcome' }, # { value: 101.00, type: 'outcome' } # ] # } + # # instance = Account.new - # fetcher = Arstotzka::Fetcher.new(hash, instance, + # + # fetcher = Arstotzka::Fetcher.new(instance, # path: 'transactions', # klass: Transaction, # after: :filter_income # ) # @@ -80,45 +90,38 @@ end private # @private - attr_reader :keys, :hash, :instance, :options + attr_reader :instance, :options delegate :after, :flatten, to: :options delegate :wrap, to: :wrapper + def hash + @hash ||= instance.send(:eval, options.json.to_s) + end + # @private # # Returns an instance of Aristotzka::Craler # # craler will be responsible to crawl the hash for # the final return # # @return [Arstotzka::Crawler] the crawler object def crawler @crawler ||= - Arstotzka::Crawler.new(crawler_options) do |value| + Crawler.new(options) do |value| wrap(value) end end # @private # - # Hash for crawler initialization - # - # @return [Hash] - # - # @see #crawler - def crawler_options - options.merge(keys: keys) - end - - # @private - # # Wrapper responsible for wrapping the value found # # @return [Arstotzka::Wrapper] the wrapper def wrapper - @wrapper ||= Arstotzka::Wrapper.new(options) + @wrapper ||= Wrapper.new(options) end end end