Module: FamilySearch::Gedcomx::SuperCoercion::ClassMethods

Defined in:
lib/familysearch/gedcomx/super_coercion.rb

Instance Method Summary collapse

Instance Method Details

#coerce_key(*attrs) ⇒ Object Also known as: coerce_keys

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.

Examples:

Coerce a “user” subhash into a User object

class Tweet < Hash
  include FamilySearch::Gedcomx::SuperCoercion
  coerce_key :user, User
  coerce_key :tags, [Tag]
end

Parameters:

  • key (Object)

    the key or array of keys you would like to be coerced.

  • into (Class)

    the class into which you want the key(s) coerced.



102
103
104
105
106
107
108
109
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 102

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

#coerce_value(from, into, options = {}) ⇒ Object

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.

Examples:

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

Parameters:

  • from (Class)

    the type you would like coerced.

  • into (Class)

    the class into which you would like the value coerced.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :strict (Boolean) — default: true

    whether use exact source class only or include ancestors



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 152

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

#inherited(klass) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 124

def inherited(klass)
  super
  (@subclasses ||= Set.new) << klass
  klass.instance_variable_set('@key_coercions', self.key_coercions.dup)
  klass.instance_variable_set('@lenient_value_coercions', self.lenient_value_coercions.dup)
  klass.instance_variable_set('@strict_value_coercions', self.strict_value_coercions.dup)
end

#key_coercion(key) ⇒ Object

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



120
121
122
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 120

def key_coercion(key)
  key_coercions[key.to_sym]
end

#key_coercionsObject

Returns a hash of any existing key coercions.



114
115
116
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 114

def key_coercions
  @key_coercions || {}
end

#lenient_value_coercionsObject

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



171
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 171

def lenient_value_coercions; @value_coercions || {} end

#strict_value_coercionsObject

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



169
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 169

def strict_value_coercions; @strict_value_coercions || {} end

#value_coercion(value) ⇒ Object

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



174
175
176
177
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 174

def value_coercion(value)
  from = value.class
  strict_value_coercions[from] || lenient_value_coercions[from]
end