Sha256: 55c3b3845d5e536afe9e76e47f8ce5a349aeac6614bf3fd9d592a80f148e09dd

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require 'delegate'
require 'time'
require 'peddler/structured_list'

module Peddler
  # @api private
  class Operation < SimpleDelegator
    def initialize(action)
      super('Action' => action)
    end

    def structure!(*list_keys)
      list_key = list_keys.shift
      found_key = keys.find { |key| key.end_with?(list_key) }
      if found_key
        builder = StructuredList.new(found_key, *list_keys)
        vals = delete(found_key)
        update(builder.build(vals))
      end

      self
    end

    def store(key, val, parent: '')
      key = camelize(key) if key.is_a?(Symbol)
      key = "#{parent}.#{key}" unless parent.empty?

      val = val.iso8601 if val.respond_to?(:iso8601)
      val = val.to_h if val.is_a?(Struct)

      if val.is_a?(Hash)
        val.each { |keyval| store(*keyval, parent: key) }
      else
        __getobj__.store(key, val)
      end
    end

    def update(hsh)
      hsh.each { |key, val| store(key, val) }
      self
    end

    alias add update

    private

    def camelize(sym)
      sym
        .to_s
        .split('_')
        .map { |token| token == 'sku' ? 'SKU' : token.capitalize }
        .join
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peddler-1.4.1 lib/peddler/operation.rb
peddler-1.4.0 lib/peddler/operation.rb
peddler-1.3.0 lib/peddler/operation.rb