Module: Familia::Features::SafeDump

Extended by:
ClassMethods
Defined in:
lib/familia/features/safe_dump.rb

Overview

SafeDump is a mixin that allows models to define a list of fields that are safe to dump. This is useful for serializing objects to JSON or other formats where you want to ensure that only certain fields are exposed.

To use SafeDump, include it in your model and define a list of fields that are safe to dump. The fields can be either symbols or hashes. If a field is a symbol, the method with the same name will be called on the object to retrieve the value. If the field is a hash, the key is the field name and the value is a lambda that will be called with the object as an argument. The hash syntax allows you to: * define a field name that is different from the method name * define a field that requires some computation on-the-fly * define a field that is not a method on the object

Example:

feature :safe_dump

@safe_dump_fields = [ :objid, :updated, :created, { :active => ->(obj) { obj.active? } } ]

Internally, all fields are normalized to the hash syntax and stored in of symbols in the order they were defined. From the example above, it would return [:objid, :updated, :created, :active].

Standalone Usage:

You can also use SafeDump by including it in your model and defining the safe dump fields using the class instance variable @safe_dump_fields.

Example:

class MyModel include Familia::Features::SafeDump

@safe_dump_fields = [
  :id, :name, { active: ->(obj) { obj.active? } }
]   end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.safe_dump_field_mapObject Originally defined in module ClassMethods

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.

.safe_dump_fieldsObject Originally defined in module ClassMethods

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

.set_safe_dump_fields(*fields) ⇒ Object Originally defined in module ClassMethods

Instance Method Details

#safe_dumpObject

Returns a hash of safe fields and their values. This method calls the callables defined in the safe_dump_field_map with the instance object as an argument.

The return values are not cached, so if you call this method multiple times, the callables will be called each time.

Example:

class Customer < Familia::HashKey include SafeDump @safe_dump_fields = [ :name, { :active => ->(cust) { cust.active? } } ]

def active?
  true # or false
end

cust = Customer.new :name => 'Lucy'
cust.safe_dump
#=> { :name => 'Lucy', :active => true }


140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/familia/features/safe_dump.rb', line 140

def safe_dump
  self.class.safe_dump_field_map.transform_values do |callable|
    transformed_value = callable.call(self)

    # If the value is a relative ancestor of SafeDump we can
    # call safe_dump on it, otherwise we'll just return the value as-is.
    if transformed_value.is_a?(SafeDump)
      transformed_value.safe_dump
    else
      transformed_value
    end
  end
end