Module: Familia::Features::SafeDump::ClassMethods

Included in:
Familia::Features::SafeDump
Defined in:
lib/familia/features/safe_dump.rb

Instance Method Summary collapse

Instance Method Details

#safe_dump_field_mapObject

‘SafeDump.safe_dump_field_map` returns the field map that is used to dump the fields. The keys are the field names and the values are callables that will expect to receive the instance object as an argument.

The map is cached on the first call to this method.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/familia/features/safe_dump.rb', line 78

def safe_dump_field_map
  return @safe_dump_field_map if @safe_dump_field_map.any?

  # Operate directly on the @safe_dump_fields array to
  # build the map. This way we'll get the elements defined
  # in the hash syntax (i.e. since the safe_dump_fields getter
  # method returns only the symbols).
  @safe_dump_field_map = @safe_dump_fields.each_with_object({}) do |el, map|
    if el.is_a?(Symbol)
      field_name = el
      callable = lambda { |obj|
        if obj.respond_to?(:[]) && obj[field_name]
          obj[field_name] # Familia::RedisType classes
        elsif obj.respond_to?(field_name)
          obj.send(field_name) # Onetime::Models::RedisHash classes via method_missing 😩
        end
      }
    else
      field_name = el.keys.first
      callable = el.values.first
    end
    map[field_name] = callable
  end
end

#safe_dump_fieldsObject

‘SafeDump.safe_dump_fields` returns only the list of symbols in the order they were defined.



65
66
67
68
69
# File 'lib/familia/features/safe_dump.rb', line 65

def safe_dump_fields
  @safe_dump_fields.map do |field|
    field.is_a?(Symbol) ? field : field.keys.first
  end
end

#set_safe_dump_fields(*fields) ⇒ Object



59
60
61
# File 'lib/familia/features/safe_dump.rb', line 59

def set_safe_dump_fields(*fields)
  @safe_dump_fields = fields
end