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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/familia/features/safe_dump.rb', line 90

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::DataType 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.



77
78
79
80
81
# File 'lib/familia/features/safe_dump.rb', line 77

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



71
72
73
# File 'lib/familia/features/safe_dump.rb', line 71

def set_safe_dump_fields(*fields)
  @safe_dump_fields = fields
end