Module: JsonRecord::Serialized::ActsMethods

Defined in:
lib/json_record/serialized.rb

Instance Method Summary collapse

Instance Method Details

#serialize_to_json(field_name, &block) ⇒ Object

Specify a field name that contains serialized JSON. The block will be yielded to with a Schema object that can then be used to define the fields in the JSON document. A class can have multiple fields that store JSON documents if necessary.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/json_record/serialized.rb', line 12

def serialize_to_json (field_name, &block)
  unless include?(InstanceMethods)
    class_inheritable_accessor :json_serialized_fields
    extend ClassMethods
    include InstanceMethods
  end
  field_name = field_name.to_s
  self.json_serialized_fields ||= {}
  schema = Schema.new(self, field_name)
  field_schemas = json_serialized_fields[field_name]
  if field_schemas
    field_schemas = field_schemas.dup
  else
    field_schemas = []
  end
  json_serialized_fields[field_name] = field_schemas
  field_schemas << schema
  block.call(schema) if block
end