Sha256: 9cf49cb07b4f581023139dd6a219f13389e1d1ab056a43c2ecc44b827de9e667

Contents?: true

Size: 862 Bytes

Versions: 9

Compression:

Stored size: 862 Bytes

Contents

module Transferwise
  module TransferwiseObject

    def self.included(base)
      base.extend ClassMethods
    end

    def initialize()
      @values = {}
    end

    def initialize_from(values)
      add_methods(values.keys)
      update_attributes(values)
      self
    end

    def add_methods(keys)
      self.instance_eval do
        keys.each do |k|
          self.class.send(:define_method, k.underscore) { @values[k] }
        end
      end
    end

    def update_attributes(values)
      values.each do |k, v|
        @values[k] = v
      end
    end

    module ClassMethods
      def convert_to_transferwise_object(resp)
        case resp
        when Array
          resp.map { |i| convert_to_transferwise_object(i) }
        when Hash
          self.new.initialize_from(resp)
        else
          resp
        end
      end
    end

  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
transferwise-rb-0.3.0 lib/transferwise/transferwise_object.rb
transferwise-0.2.6 lib/transferwise/transferwise_object.rb
transferwise-0.2.5 lib/transferwise/transferwise_object.rb
transferwise-0.2.4 lib/transferwise/transferwise_object.rb
transferwise-0.2.3 lib/transferwise/transferwise_object.rb
transferwise-0.2.2 lib/transferwise/transferwise_object.rb
transferwise-0.2.1 lib/transferwise/transferwise_object.rb
transferwise-0.2.0 lib/transferwise/transferwise_object.rb
transferwise-0.1.6 lib/transferwise/transferwise_object.rb