lib/familysearch/gedcomx/super_coercion.rb
def coerce_value(from, into, options = {})
          options = {:strict => true}.merge(options)

          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
          if defined? @subclasses
            @subclasses.each { |klass| klass.coerce_value(from,into,options) }
          end
        end
# 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 FamilySearch::Gedcomx::SuperCoercion
#     coerce_value Hash, SpecialHash
#
#     def initialize(hash = {})
#       super
#       hash.each_pair do |k,v|
#         self[k] = v
#       end
#     end
#   end
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.
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 .
def coerce_value(from, into, options = {})
          options = {:strict => true}.merge(options)
          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
          if defined? @subclasses
            @subclasses.each { |klass| klass.coerce_value(from,into,options) }
          end
        end
def coerce_value ( from , into , options = { } ) options = { :strict => true } . merge ( options ) 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 if defined? @subclasses @subclasses . each { | klass | klass . coerce_value ( from , into , options ) } end end
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.
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 .
from
into
options
:strict (true) whether use exact source class only or include ancestors
: strict ( true ) whether use exact source class only or include ancestors
into the class into which you would like the value coerced.
into the class into which you would like the value coerced .
example
Coerce all hashes into this special type of hash class SpecialHash < Hash include FamilySearch::Gedcomx::SuperCoercion coerce_value Hash, SpecialHash def initialize(hash = {}) super hash.each_pair do |k,v| self[k] = v end end end
Coerce all hashes into this special type of hash class SpecialHash < Hash include FamilySearch :: Gedcomx :: SuperCoercion coerce_value Hash SpecialHash def initialize ( hash = {} ) super hash . each_pair do |k v| self [ k ] = v end end end