Sha256: 1d6bba83b4c71092ba812af06c8756a2cce5e4205bbb42b4f49d16e83ca52448

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module Adapter::ActiveModel::CommonHelpers
  def objectize_collection(collection, root: nil, existing: true)
    if root
      collection = collection[root]
    end

    return collection.map{|resource|
      object_hash(resource, existing: existing)
    }
  end

  def objectize_resource(resource, root: nil, existing: true)
    if root
      obj = object_hash(resource[root], existing: existing)
    else
      obj = object_hash(resource, existing: existing)
    end

    return obj
  end

  def object_hash(hash, existing: true)
    ObjectHash.new(hash, existing: existing)
  end

  class ObjectHash
    #existing denotes whether we search for attributes that exist on the
    #resource or attributes that shouldn't exist
    attr_accessor :hash, :existing
    def initialize(hash, existing: true)
      @hash = HashWithIndifferentAccess.new(hash)
      @existing = existing
    end

    def method_missing(name)
      if existing
        if hash.key?(name)
          return hash[name]
        else
          return raise KeyError.new("Attribute not found in resource: #{name}")
        end
      else
        if hash.key?(name)
          return raise(
            KeyError.new(
              "Attribute found in resource when it shouldn't: #{name}"
            )
          )
        else
          return :attribute_not_found
        end
      end
    end
  end

  def superset_mismatch_error(superset, subset)
    "Expected \n #{subset.to_a.to_s} \n to be included in \n #{superset.to_a.to_s}"
  end

  def parse_model(model)
    return model unless model.is_a? Hash
    
    return object_hash(model)
  end

  def modifier_for(key)
    if modifiers_hash[key]
      return modifiers_hash[key]
    else
      return proc{|i| i}
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-api_helpers-1.0.2 lib/rspec/api_helpers/adapter/active_model/common_helpers.rb