Sha256: 65a3f5f4eda0ddd3a957bdd48187eecd0ee34cab28dcda30d93f510bcf6d12db

Contents?: true

Size: 866 Bytes

Versions: 1

Compression:

Stored size: 866 Bytes

Contents

module Securetrading
  class BaseModel
    def initialize(attrs_hash = {})
      @attributes_hash = attrs_hash.presence && attrs_hash.stringify_keys
    end

    def ox_xml
      XmlDoc.elements(xml_tag_name => @attributes_hash).first
    end

    private

    def xml_tag_name
      self.class.name.demodulize.downcase
    end

    def method_missing(m, *args, &block)
      return super unless attributes_hash.key?(m.to_s)
      determine_value(m.to_s)
    end

    def determine_value(name)
      if name == 'error'
        Securetrading::ResponseError.new(attributes_hash[name])
      elsif name.in?(sub_classes)
        "Securetrading::#{name.capitalize}".constantize
          .public_send(:new, attributes_hash[name])
      else
        attributes_hash[name]
      end
    end

    def sub_classes
      []
    end

    attr_reader :attributes_hash
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
securetrading-0.3.0 lib/securetrading/base_model.rb