Sha256: 6e4e8c41fdd3c238640a5dcfd893d05ed36694023ec093915b98ec3bfc37efe9

Contents?: true

Size: 1.3 KB

Versions: 14

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Extensions #:nodoc:
    module Hash #:nodoc:
      module Accessors #:nodoc:

        # Remove a set of attributes from a hash. If the attributes are
        # contained in an array it will remove it from the array, otherwise it
        # will delete the child attribute completely.
        def remove(key, attrs)
          elements = self[key]
          if elements
            key.singular? ? self[key] = nil : elements.delete(attrs)
          end
        end

        # Inserts new attributes into the hash. If the elements are present in
        # the hash it will update them, otherwise it will add the new
        # attributes into the hash as either a child hash or child array of
        # hashes.
        def insert(key, attrs)
          elements = self[key]
          if elements
            if elements.kind_of?(::Array)
              elements.merge!(attrs)
            else
              self[key] = attrs.reverse_merge!(elements)
            end
          else
            self[key] = key.singular? ? attrs : [attrs]
          end
        end

        # If a _type key exists in the hash, return the class for the value.
        def klass
          class_name = self["_type"]
          class_name ? class_name.constantize : nil
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
mongoid-2.0.0.beta.20 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.19 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.18 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.17 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.16 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.15 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.14 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.13 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.11 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.10 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.8 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.9 lib/mongoid/extensions/hash/accessors.rb
mongoid-2.0.0.beta.12 lib/mongoid/extensions/hash/accessors.rb
mongoid-locomotive-2.0.0.beta9 lib/mongoid/extensions/hash/accessors.rb