Sha256: 11245866807452e331cc460ae1fb1e5372c8ee650cf99e51b246ece5e76892b8

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 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.first

      if has_key?(list_key)
        builder = StructuredList.new(*list_keys)
        vals = delete(list_key)
        update(builder.build(vals))
      end

      self
    end

    def store(key, val)
      key = camelize(key) if key.is_a?(Symbol)


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

      if val.is_a?(Hash)
        val.each do |subkey, subval|
          subkey = camelize(subkey) if subkey.is_a?(Symbol)
          store([key, subkey].join('.'), subval)
        end
      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(&:capitalize).join
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
peddler-0.8.0 lib/peddler/operation.rb
peddler-0.7.11 lib/peddler/operation.rb
peddler-0.7.10 lib/peddler/operation.rb
peddler-0.7.9 lib/peddler/operation.rb
peddler-0.7.8 lib/peddler/operation.rb
peddler-0.7.7 lib/peddler/operation.rb
peddler-0.7.6 lib/peddler/operation.rb
peddler-0.7.5 lib/peddler/operation.rb
peddler-0.7.3 lib/peddler/operation.rb
peddler-0.7.2 lib/peddler/operation.rb