Module: JsonRecord::Serialized::InstanceMethods

Defined in:
lib/json_record/serialized.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/json_record/serialized.rb', line 50

def self.included (base)
  base.before_save :serialize_json_attributes
  base.alias_method_chain :reload, :serialized_json
  base.alias_method_chain :attributes, :serialized_json
  base.alias_method_chain :read_attribute, :serialized_json
  base.alias_method_chain :write_attribute, :serialized_json
  base.alias_method_chain :read_attribute_before_type_cast, :serialized_json
  base.alias_method_chain :attributes_before_type_cast, :serialized_json
  # Alias method chain doesn't work on [] methods and these are aliased directly in Rails 3.1
  base.send :alias_method, :[], :read_attribute_with_serialized_json
  base.send :alias_method, :[]=, :write_attribute_with_serialized_json
  base.send :public, :[], :[]=
end

Instance Method Details

#attributes_before_type_cast_with_serialized_jsonObject

:nodoc:



96
97
98
# File 'lib/json_record/serialized.rb', line 96

def attributes_before_type_cast_with_serialized_json #:nodoc:
  json_attributes_before_type_cast.merge(attributes_before_type_cast_without_serialized_json)
end

#attributes_with_serialized_jsonObject

:nodoc:



85
86
87
88
89
90
# File 'lib/json_record/serialized.rb', line 85

def attributes_with_serialized_json #:nodoc:
  attrs = json_attributes.reject{|k,v| !json_field_names.include?(k)}
  attrs.merge!(attributes_without_serialized_json)
  json_serialized_fields.keys.each{|name| attrs.delete(name)}
  return attrs
end

#json_attributes_before_type_castObject

:nodoc:



75
76
77
# File 'lib/json_record/serialized.rb', line 75

def json_attributes_before_type_cast # :nodoc:
  @json_attributes_before_type_cast ||= {}
end

#json_fieldsObject

Get the JsonField objects for the record.



65
66
67
68
69
70
71
72
73
# File 'lib/json_record/serialized.rb', line 65

def json_fields
  unless @json_fields
    @json_fields = {}
    json_serialized_fields.each_pair do |name, schemas|
      @json_fields[name] = JsonField.new(self, name, schemas)
    end
  end
  @json_fields
end

#read_attribute_before_type_cast_with_serialized_json(attr_name) ⇒ Object

:nodoc:



92
93
94
# File 'lib/json_record/serialized.rb', line 92

def read_attribute_before_type_cast_with_serialized_json (attr_name) #:nodoc:
  json_attributes_before_type_cast[attr_name.to_s] || read_attribute_before_type_cast_without_serialized_json(attr_name)
end

#reload_with_serialized_json(*args) ⇒ Object

:nodoc:



79
80
81
82
83
# File 'lib/json_record/serialized.rb', line 79

def reload_with_serialized_json (*args) #:nodoc:
  @json_fields = nil
  @json_attributes_before_type_cast = nil
  reload_without_serialized_json(*args)
end