lib/familysearch/gedcomx/super_coercion.rb
# 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 FamilySearch::Gedcomx::SuperCoercion
#     coerce_key :user, User
#     coerce_key :tags, [Tag]
#   end
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.
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 .
def coerce_key(*attrs)
          @key_coercions ||= {}
          into = attrs.pop
          attrs.each { |key| @key_coercions[key] = into }
          if defined? @subclasses
            @subclasses.each { |klass| klass.coerce_key(attrs) }
          end
        end
def coerce_key ( * attrs ) @key_coercions ||= { } into = attrs . pop attrs . each { | key | @key_coercions [ key ] = into } if defined? @subclasses @subclasses . each { | klass | klass . coerce_key ( attrs ) } end end
Set up a coercion rule such that any time the specified
key is set it will be coerced into the specified class.
Set up a coercion rule such that any time the specified key is set it will be coerced into the specified class .
attrs
<not_specific>
<not_specific>
into the class into which you want the key(s) coerced.
into the class into which you want the key ( s ) coerced .
example
Coerce a "user" subhash into a User object class Tweet < Hash include FamilySearch::Gedcomx::SuperCoercion coerce_key :user, User coerce_key :tags, [Tag] end
Coerce a " user " subhash into a User object class Tweet < Hash include FamilySearch :: Gedcomx :: SuperCoercion coerce_key : user User coerce_key : tags [ Tag ] end