Module: Hashie::Extensions::Coercion::ClassMethods

### Home Classes Methods

### In Files

- lib/hashie/extensions/coercion.rb

### Methods

- #coerce_key
- #coerce_keys
- #coerce_value
- #inherited
- #key_coercion
- #key_coercions
- #lenient_value_coercions
- #strict_value_coercions
- #value_coercion

### Class/Module Index

Quicksearch

- Hashie
- Hashie::Clash
- Hashie::Clash::ChainError
- Hashie::CoercionError
- Hashie::Dash
- Hashie::Extensions
- Hashie::Extensions::Coercion
- Hashie::Extensions::Coercion::ClassMethods
- Hashie::Extensions::Coercion::InstanceMethods
- Hashie::Extensions::Dash
- Hashie::Extensions::Dash::IndifferentAccess
- Hashie::Extensions::Dash::IndifferentAccess::ClassMethods
- Hashie::Extensions::DeepFetch
- Hashie::Extensions::DeepFetch::UndefinedPathError
- Hashie::Extensions::DeepFind
- Hashie::Extensions::DeepMerge
- Hashie::Extensions::IgnoreUndeclared
- Hashie::Extensions::IndifferentAccess
- Hashie::Extensions::KeyConversion
- Hashie::Extensions::Mash
- Hashie::Extensions::Mash::SafeAssignment
- Hashie::Extensions::MergeInitializer
- Hashie::Extensions::MethodAccess
- Hashie::Extensions::MethodAccessWithOverride
- Hashie::Extensions::MethodOverridingWriter
- Hashie::Extensions::MethodQuery
- Hashie::Extensions::MethodReader
- Hashie::Extensions::MethodWriter
- Hashie::Extensions::Parsers
- Hashie::Extensions::Parsers::YamlErbParser
- Hashie::Extensions::PrettyInspect
- Hashie::Extensions::StringifyKeys
- Hashie::Extensions::StringifyKeys::ClassMethods
- Hashie::Extensions::SymbolizeKeys
- Hashie::Extensions::SymbolizeKeys::ClassMethods
- Hashie::Hash
- Hashie::Mash
- Hashie::Rash
- Hashie::Trash

No matching classes.

# Hashie::Extensions::Coercion::ClassMethods

### Attributes

key_coercions[W]

### Public Instance Methods

coerce_key(*attrs) click to toggle source

Set up a coercion rule such that any time the specified key is set it will be coerced into the specified class. Coercion will occur by first attempting to call Class.coerce and then by calling Class.new with the value as an argument in either case.

@param [Object] key the key or array of keys you would like to be coerced. @param [Class] into the class into which you want the key(s) coerced.

@example Coerce a "user" subhash into a User object

    class Tweet < Hash
      include Hashie::Extensions::Coercion
      coerce_key :user, User
    end

    # File lib/hashie/extensions/coercion.rb, line 106
    def coerce_key(*attrs)
      into = attrs.pop
      attrs.each { |key| key_coercions[key] = into }
    end

Also aliased as: coerce_keys
coerce_keys(*attrs) click to toggle source
Alias for: coerce_key
coerce_value(from, into, options = {}) click to toggle source

Set up a coercion rule such that any time a value of the specified type is set it will be coerced into the specified class.

@param [Class] from the type you would like coerced. @param [Class] into the class into which you would like the value coerced. @option options [Boolean] :strict (true) whether use exact source class only or include ancestors

@example Coerce all hashes into this special type of hash

    class SpecialHash < Hash
      include Hashie::Extensions::Coercion
      coerce_value Hash, SpecialHash

      def initialize(hash = {})
        super
        hash.each_pair do |k,v|
          self[k] = v
        end
      end
    end

    # File lib/hashie/extensions/coercion.rb, line 144
    def coerce_value(from, into, options = {})
      options = { strict: true }.merge(options)

      if ABSTRACT_CORE_TYPES.key? from
        ABSTRACT_CORE_TYPES[from].each do | type |
          coerce_value type, into, options
        end
      end

      if options[:strict]
        strict_value_coercions[from] = into
      else
        while from.superclass && from.superclass != Object
          lenient_value_coercions[from] = into
          from = from.superclass
        end
      end
    end

inherited(klass) click to toggle source

    # File lib/hashie/extensions/coercion.rb, line 178
    def inherited(klass)
      super

      klass.key_coercions = key_coercions
    end

key_coercion(key) click to toggle source

Returns the specific key coercion for the specified key, if one exists.

    # File lib/hashie/extensions/coercion.rb, line 120
    def key_coercion(key)
      key_coercions[key.to_sym]
    end

key_coercions() click to toggle source

Returns a hash of any existing key coercions.

    # File lib/hashie/extensions/coercion.rb, line 114
    def key_coercions
      @key_coercions ||= {}
    end

lenient_value_coercions() click to toggle source

Return all value coercions that have the :strict rule as false.

    # File lib/hashie/extensions/coercion.rb, line 168
    def lenient_value_coercions
      @lenient_value_coercions ||= {}
    end

strict_value_coercions() click to toggle source

Return all value coercions that have the :strict rule as true.

    # File lib/hashie/extensions/coercion.rb, line 164
    def strict_value_coercions
      @strict_value_coercions ||= {}
    end

value_coercion(value) click to toggle source

Fetch the value coercion, if any, for the specified object.

    # File lib/hashie/extensions/coercion.rb, line 173
    def value_coercion(value)
      from = value.class
      strict_value_coercions[from] || lenient_value_coercions[from]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.