Sha256: 6bde5b0e4c88342c334d191288a1d0c6b72989c74d5f88116fb3b28c5ff0c397

Contents?: true

Size: 1.59 KB

Versions: 24

Compression:

Stored size: 1.59 KB

Contents

require 'hashie/mash'

module Restforce
  class Mash < Hashie::Mash

    class << self

      # Pass in an Array or Hash and it will be recursively converted into the
      # appropriate Restforce::Collection, Restforce::SObject and
      # Restforce::Mash objects.
      def build(val, client)
        if val.is_a?(Array)
          val.collect { |e| self.klass(e).new(e, client) }
        elsif val.is_a?(Hash)
          self.klass(val).new(val, client)
        else
          val
        end
      end

      # When passed a hash, it will determine what class is appropriate to
      # represent the data.
      def klass(val)
        if val.has_key? 'records'
          # When the hash has a records key, it should be considered a collection
          # of sobject records.
          Restforce::Collection
        elsif val.has_key? 'attributes'
          # When the hash contains an attributes key, it should be considered an
          # sobject record
          Restforce::SObject
        else
          # Fallback to a standard Restforce::Mash for everything else
          Restforce::Mash
        end
      end

    end

    def initialize(source_hash = nil, client = nil, default = nil, &blk)
      @client = client
      deep_update(source_hash) if source_hash
      default ? super(default) : super(&blk)
    end
  
    def convert_value(val, duping=false)
      case val
      when self.class
        val.dup
      when ::Hash
        val = val.dup if duping
        self.class.klass(val).new(val, @client)
      when Array
        val.collect{ |e| convert_value(e) }
      else
        val
      end
    end

  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
restforce-1.0.4 lib/restforce/mash.rb
restforce-1.0.3 lib/restforce/mash.rb
restforce-1.0.2 lib/restforce/mash.rb
restforce-1.0.1 lib/restforce/mash.rb
restforce-1.0.0 lib/restforce/mash.rb
restforce-0.1.10 lib/restforce/mash.rb
restforce-0.1.9 lib/restforce/mash.rb
restforce-0.1.8 lib/restforce/mash.rb
restforce-0.1.7 lib/restforce/mash.rb
restforce-0.1.6 lib/restforce/mash.rb
restforce-0.1.5 lib/restforce/mash.rb
restforce-0.1.4 lib/restforce/mash.rb
restforce-0.1.3 lib/restforce/mash.rb
restforce-0.1.2 lib/restforce/mash.rb
restforce-0.1.1 lib/restforce/mash.rb
restforce-0.1.0 lib/restforce/mash.rb
restforce-0.0.8 lib/restforce/mash.rb
restforce-0.0.7 lib/restforce/mash.rb
restforce-0.0.6 lib/restforce/mash.rb
restforce-0.0.5 lib/restforce/mash.rb