Sha256: 4a913ab302b5b82dd97a31179fe9216460454b9bd78d14be1522ff18277d8ccc

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Zuora
  class ZObject < SimpleDelegator
    def initialize(attributes={})
      super(self.class.zobject_class.new).tap do |zobject|
        attributes.each do |attr, value|
          zobject.send("#{attr}=", value)
        end
      end
    end

    def to_zobject
      __getobj__
    end

    def self.zobject_class
      return @zobject_class if @zobject_class
      klass_name = name.match(/(\w+)::(\w+)/)
      @zobject_class = "#{klass_name[1].upcase}::#{klass_name[2]}".constantize
    end

    #TODO: This sucks attributes need to be clearly defined
    def self.attribute_names
      @attribute_names ||= zobject_class.instance_variable_get("@attributes").reject{|name| name == :fieldsToNull }
    end

    def self.where(conditions={})
      zobjects = self.client.query("select #{self.attribute_names.join(", ")} from #{self.name.gsub(/Zuora::/,"")} where #{build_filter_statments(conditions)}")
      zobjects.map{|zobject| self.new zobject }
    end

    def self.build_filter_statments(filter_statments)
      filter_statments.map{|key, value|
        value = "'#{value}'" if value.kind_of?(String)
        "#{key} = #{value}"
      }.join(" and ")
    end

    def self.find(id)
      zobject = client.query("select #{attribute_names.join(", ")} from #{self.name.gsub(/Zuora::/,"")} where Id = '#{id}'").first
      self.new zobject if zobject
    end

    def self.all
      zobjects = client.query("select #{attribute_names.join(", ")} from #{self.name.gsub(/Zuora::/,"")}")
      zobjects.map{|zobject| self.new zobject }
    end

    def self.client
      @client ||= Zuora::Client.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_zuora-1.3.0 lib/zuora/zobject.rb