Class: DataMapper::Mash

### Home Classes Methods

### In Files

- lib/dm-core/support/mash.rb

### Parent

Hash

### Methods

- ::new
- #[]=
- #convert_key
- #convert_value
- #default
- #delete
- #except
- #except!
- #fetch
- #has_key?
- #include?
- #key?
- #member?
- #merge
- #merge!
- #regular_update
- #regular_writer
- #stringify_keys!
- #symbolize_keys
- #to_hash
- #update
- #values_at

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Adapters
- DataMapper::Adapters::AbstractAdapter
- DataMapper::Adapters::InMemoryAdapter
- DataMapper::Assertions
- DataMapper::Associations
- DataMapper::Associations::ManyToMany
- DataMapper::Associations::ManyToMany::Collection
- DataMapper::Associations::ManyToMany::Relationship
- DataMapper::Associations::ManyToOne
- DataMapper::Associations::ManyToOne::Relationship
- DataMapper::Associations::OneToMany
- DataMapper::Associations::OneToMany::Collection
- DataMapper::Associations::OneToMany::Relationship
- DataMapper::Associations::OneToOne
- DataMapper::Associations::OneToOne::Relationship
- DataMapper::Associations::Relationship
- DataMapper::Chainable
- DataMapper::Collection
- DataMapper::Deprecate
- DataMapper::DescendantSet
- DataMapper::Equalizer
- DataMapper::Ext
- DataMapper::Ext::Array
- DataMapper::Ext::Hash
- DataMapper::Ext::Module
- DataMapper::Ext::Object
- DataMapper::Ext::String
- DataMapper::Hook
- DataMapper::Hook::ClassMethods
- DataMapper::IdentityMap
- DataMapper::ImmutableDeletedError
- DataMapper::ImmutableError
- DataMapper::IncompleteModelError
- DataMapper::Inflector
- DataMapper::Inflector::Inflections
- DataMapper::LocalObjectSpace
- DataMapper::Logger
- DataMapper::Mash
- DataMapper::Model
- DataMapper::Model::Hook
- DataMapper::Model::Hook::MethodCommand
- DataMapper::Model::Hook::Methods
- DataMapper::Model::Hook::ProcCommand
- DataMapper::Model::Is
- DataMapper::Model::Property
- DataMapper::Model::Relationship
- DataMapper::Model::Scope
- DataMapper::NamingConventions
- DataMapper::NamingConventions::Field
- DataMapper::NamingConventions::Field::Underscored
- DataMapper::NamingConventions::Field::UnderscoredAndPluralized
- DataMapper::NamingConventions::Field::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Field::Yaml
- DataMapper::NamingConventions::Resource
- DataMapper::NamingConventions::Resource::Underscored
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralized
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutLeadingModule
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Resource::Yaml
- DataMapper::ObjectNotFoundError
- DataMapper::OrderedSet
- DataMapper::OrderedSet::Cache
- DataMapper::OrderedSet::Cache::API
- DataMapper::PersistenceError
- DataMapper::PluginNotFoundError
- DataMapper::Property
- DataMapper::Property::Binary
- DataMapper::Property::Boolean
- DataMapper::Property::Class
- DataMapper::Property::Date
- DataMapper::Property::DateTime
- DataMapper::Property::Decimal
- DataMapper::Property::Discriminator
- DataMapper::Property::Discriminator::Model
- DataMapper::Property::Float
- DataMapper::Property::Integer
- DataMapper::Property::Lookup
- DataMapper::Property::Numeric
- DataMapper::Property::Object
- DataMapper::Property::PassThroughLoadDump
- DataMapper::Property::Serial
- DataMapper::Property::String
- DataMapper::Property::Text
- DataMapper::Property::Time
- DataMapper::Property::Typecast
- DataMapper::Property::Typecast::Numeric
- DataMapper::Property::Typecast::Time
- DataMapper::PropertySet
- DataMapper::Query
- DataMapper::Query::Conditions
- DataMapper::Query::Conditions::AbstractComparison
- DataMapper::Query::Conditions::AbstractOperation
- DataMapper::Query::Conditions::AndOperation
- DataMapper::Query::Conditions::Comparison
- DataMapper::Query::Conditions::EqualToComparison
- DataMapper::Query::Conditions::FlattenOperation
- DataMapper::Query::Conditions::GreaterThanComparison
- DataMapper::Query::Conditions::GreaterThanOrEqualToComparison
- DataMapper::Query::Conditions::InclusionComparison
- DataMapper::Query::Conditions::LessThanComparison
- DataMapper::Query::Conditions::LessThanOrEqualToComparison
- DataMapper::Query::Conditions::LikeComparison
- DataMapper::Query::Conditions::NotOperation
- DataMapper::Query::Conditions::NullOperation
- DataMapper::Query::Conditions::Operation
- DataMapper::Query::Conditions::OrOperation
- DataMapper::Query::Conditions::RegexpComparison
- DataMapper::Query::Conditions::RelationshipHandler
- DataMapper::Query::Direction
- DataMapper::Query::Operator
- DataMapper::Query::Path
- DataMapper::Query::Sort
- DataMapper::RelationshipSet
- DataMapper::Repository
- DataMapper::RepositoryNotSetupError
- DataMapper::Resource
- DataMapper::Resource::PersistenceState
- DataMapper::Resource::PersistenceState::Clean
- DataMapper::Resource::PersistenceState::Deleted
- DataMapper::Resource::PersistenceState::Dirty
- DataMapper::Resource::PersistenceState::Immutable
- DataMapper::Resource::PersistenceState::Persisted
- DataMapper::Resource::PersistenceState::Transient
- DataMapper::SaveFailureError
- DataMapper::Spec
- DataMapper::Spec::Adapters
- DataMapper::Spec::Adapters::Adapter
- DataMapper::Spec::Adapters::Helpers
- DataMapper::Spec::Adapters::InMemoryAdapter
- DataMapper::Spec::CollectionHelpers
- DataMapper::Spec::CollectionHelpers::GroupMethods
- DataMapper::Spec::Helpers
- DataMapper::Spec::PendingHelpers
- DataMapper::Subject
- DataMapper::SubjectSet
- DataMapper::SubjectSet::NameCache
- DataMapper::Undefined
- DataMapper::UnknownRelationshipError
- DataMapper::UnsavedParentError
- DataMapper::UpdateConflictError
- Blog
- Blog::Article
- ChildSubType
- CounterAdapter
- Heffalump
- Kernel
- LazyArray
- Object
- OtherComparison
- Pathname
- SubType
- Symbol

No matching classes.

# DataMapper::Mash

This class has dubious semantics and we only have it so that people can write params instead of params.

### Public Class Methods

new(constructor = {}) click to toggle source

Initializes a new mash.

@param [Hash, Object] constructor

    The default value for the mash. If +constructor+ is a Hash, a new mash
    will be created based on the keys of the hash and no default value will
    be set.

    # File lib/dm-core/support/mash.rb, line 12
    def initialize(constructor = {})
      if constructor.is_a?(Hash)
        super()
        update(constructor)
      else
        super(constructor)
      end
    end

### Public Instance Methods

[]=(key, value) click to toggle source

Sets the `value` associated with the specified `key`.

@param [Object] key The key to set. @param [Object] value The value to set the key to.

    # File lib/dm-core/support/mash.rb, line 42
    def []=(key, value)
      regular_writer(convert_key(key), convert_value(value))
    end

Also aliased as: regular_writer
default(key = nil) click to toggle source

Gets the default value for the mash.

@param [Object] key

    The default value for the mash. If +key+ is a Symbol and it is a key in
    the mash, then the default value will be set to the value matching the
    key.

    # File lib/dm-core/support/mash.rb, line 27
    def default(key = nil)
      if key.is_a?(Symbol) && include?(key = key.to_s)
        self[key]
      else
        super
      end
    end

delete(key) click to toggle source

@param [Object] key The key to delete from the mash.

    # File lib/dm-core/support/mash.rb, line 96
    def delete(key)
      super(convert_key(key))
    end

except(*keys) click to toggle source

Returns a mash that includes everything but the given `keys`.

@param [Array<String, Symbol>] *keys The mash keys to exclude.

@return [Mash] A new mash without the selected keys.

@example

    { :one => 1, :two => 2, :three => 3 }.except(:one)
      #=> { "two" => 2, "three" => 3 }

    # File lib/dm-core/support/mash.rb, line 109
    def except(*keys)
      self.dup.except!(*keys.map {|k| convert_key(k)})
    end

except!(*keys) click to toggle source

Removes the specified `keys` from the mash.

@param [Array] *keys The mash keys to exclude.

@return [Hash] `hash`

@example

    mash = { :one => 1, :two => 2, :three => 3 }
    mash.except!(:one, :two)
    mash # => { :three => 3 }

    # File lib/dm-core/support/mash.rb, line 123
    def except!(*keys)
      keys.each { |key| delete(key) }
      self
    end

fetch(key, *extras) click to toggle source

@param [Object] key The key to fetch. @param [Array] *extras Default value.

@return [Object] The value at key or the default value.

    # File lib/dm-core/support/mash.rb, line 76
    def fetch(key, *extras)
      super(convert_key(key), *extras)
    end

has_key?(key) click to toggle source
Alias for: key?
include?(key) click to toggle source
Alias for: key?
key?(key) click to toggle source

Determines whether the mash contains the specified `key`.

@param [Object] key The key to check for. @return [Boolean] True if the key exists in the mash.

    # File lib/dm-core/support/mash.rb, line 64
    def key?(key)
      super(convert_key(key))
    end

Also aliased as: include?, has_key?, member?
member?(key) click to toggle source
Alias for: key?
merge(hash) click to toggle source

@param [Hash] hash The hash to merge with the mash.

@return [Mash] A new mash with the hash values merged in.

    # File lib/dm-core/support/mash.rb, line 91
    def merge(hash)
      self.dup.update(hash)
    end

merge!(other_hash) click to toggle source
Alias for: update
regular_update(other_hash) click to toggle source
Alias for: update
regular_writer(key, value) click to toggle source
Alias for: []=
stringify_keys!() click to toggle source

Used to provide the same interface as Hash.

@return [Mash] This mash unchanged.

    # File lib/dm-core/support/mash.rb, line 131
    def stringify_keys!; self end

symbolize_keys() click to toggle source

@return [Hash] The mash as a Hash with symbolized keys.

    # File lib/dm-core/support/mash.rb, line 134
    def symbolize_keys
      h = Hash.new(default)
      each { |key, val| h[key.to_sym] = val }
      h
    end

to_hash() click to toggle source

@return [Hash] The mash as a Hash with string keys.

    # File lib/dm-core/support/mash.rb, line 141
    def to_hash
      Hash.new(default).replace(self)
    end

update(other_hash) click to toggle source

Updates the mash with the key/value pairs from the specified hash.

@param [Hash] other_hash

    A hash to update values in the mash with. The keys and the values will be
    converted to Mash format.

@return [Mash] The updated mash.

    # File lib/dm-core/support/mash.rb, line 53
    def update(other_hash)
      other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
      self
    end

Also aliased as: regular_update, merge!
values_at(*indices) click to toggle source

@param [Array] *indices

    The keys to retrieve values for.

@return [Array] The values at each of the provided keys.

    # File lib/dm-core/support/mash.rb, line 84
    def values_at(*indices)
      indices.collect {|key| self[convert_key(key)]}
    end

### Protected Instance Methods

convert_key(key) click to toggle source

@param [Object] key The key to convert.

@param [Object]

    The converted key. If the key was a symbol, it will be converted to a
    string.

@api private

    # File lib/dm-core/support/mash.rb, line 153
    def convert_key(key)
      key.kind_of?(Symbol) ? key.to_s : key
    end

convert_value(value) click to toggle source

@param [Object] value The value to convert.

@return [Object]

    The converted value. A Hash or an Array of hashes, will be converted to
    their Mash equivalents.

@api private

    # File lib/dm-core/support/mash.rb, line 164
    def convert_value(value)
      if value.class == Hash
         mash = Mash.new(value)
         mash.default = value.default
         mash
      elsif value.is_a?(Array)
        value.collect { |e| convert_value(e) }
      else
        value
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.