Module: JsonRecord::EmbeddedDocument

Defined in:
lib/json_record/embedded_document.rb

Overview

Classes that include EmbeddedDocument can be used as the type for keys or many field definitions in Schema. Embedded documents are then extensions of the schema. In this way, complex documents represented in JSON can be deserialized as complex objects.

To define the schema for an embedded document, call schema.key or schema.many from the class definition.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

The parent object of the document.



79
80
81
# File 'lib/json_record/embedded_document.rb', line 79

def parent
  @parent
end

Class Method Details

.included(base) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/json_record/embedded_document.rb', line 56

def self.included (base)
  base.send :include, ActiveRecordStub
  base.send :include, ActiveRecord::Validations
  base.send :include, AttributeMethods
  base.send :include, ActiveSupport::Callbacks
  
  if base.respond_to?(:set_callback)
    # Poor man's check for ActiveSupport 3.0 which completely changed around how callbacks work.
    # This is a temporary work around so that the same gem can be compatible with both 2.x and 3.x for now.
    # Incoporating ActiveModel will fix all.
    base.define_callbacks :validation
    base.alias_method_chain(:valid?, :callbacks_3)
    base.extend(ActiveSupport3Callbacks)
  else
    base.define_callbacks :before_validation, :after_validation
    base.alias_method_chain(:valid?, :callbacks)
  end
  
  base.write_inheritable_attribute(:schema, Schema.new(base, nil))
  base.class_inheritable_reader :schema
end

Instance Method Details

#==(val) ⇒ Object



150
151
152
# File 'lib/json_record/embedded_document.rb', line 150

def == (val)
  eql?(val)
end

#[](name) ⇒ Object

Get a field from the schema with the specified name.



127
128
129
130
# File 'lib/json_record/embedded_document.rb', line 127

def [] (name)
  field = schema.fields[name.to_s]
  read_attribute(field, self) if field
end

#[]=(name, value) ⇒ Object

Set a field from the schema with the specified name.



133
134
135
136
# File 'lib/json_record/embedded_document.rb', line 133

def []= (name, value)
  field = schema.fields[name.to_s] || FieldDefinition.new(name, :type => value.class)
  write_attribute(field, value, self)
end

#attributesObject

Get the attributes of the document.



89
90
91
# File 'lib/json_record/embedded_document.rb', line 89

def attributes
  @json_attributes.reject{|k,v| !schema.fields.include?(k)}
end

#attributes=(attrs) ⇒ Object

Set all the attributes at once.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/json_record/embedded_document.rb', line 94

def attributes= (attrs)
  attrs.each_pair do |name, value|
    field = schema.fields[name.to_s] || FieldDefinition.new(name, :type => value.class)
    setter = "#{name}=".to_sym
    if respond_to?(setter)
      send(setter, value)
    else
      write_attribute(field, value, self)
    end
  end
end

#attributes_before_type_castObject

Get the attribute values of the document before they were type cast.



107
108
109
# File 'lib/json_record/embedded_document.rb', line 107

def attributes_before_type_cast
  @attributes
end

#changedObject

Get the list of attributes changed.



117
118
119
# File 'lib/json_record/embedded_document.rb', line 117

def changed
  changed_attributes.keys
end

#changed?Boolean

Determine if the document has been changed.

Returns:



112
113
114
# File 'lib/json_record/embedded_document.rb', line 112

def changed?
  !changed_attributes.empty?
end

#changesObject

Get a list of changes to the document.



122
123
124
# File 'lib/json_record/embedded_document.rb', line 122

def changes
  changed.inject({}) {|h, attr| h[attr] = attribute_change(attr); h}
end

#eql?(val) ⇒ Boolean

Returns:



146
147
148
# File 'lib/json_record/embedded_document.rb', line 146

def eql? (val)
  val.class == self.class && val.attributes == attributes && val.parent == parent
end

#equal?(val) ⇒ Boolean

Returns:



154
155
156
# File 'lib/json_record/embedded_document.rb', line 154

def equal? (val)
  eql?(val)
end

#hashObject



158
159
160
# File 'lib/json_record/embedded_document.rb', line 158

def hash
  attributes.hash + parent.hash
end

#initialize(attrs = {}) ⇒ Object

Create an embedded document with the specified attributes.



82
83
84
85
86
# File 'lib/json_record/embedded_document.rb', line 82

def initialize (attrs = {})
  @attributes = {}
  @json_attributes = {}
  self.attributes = attrs
end

#inspectObject



162
163
164
# File 'lib/json_record/embedded_document.rb', line 162

def inspect
  "#<#{self.class.name} #{attributes.inspect}>"
end

#to_hashObject



142
143
144
# File 'lib/json_record/embedded_document.rb', line 142

def to_hash
  @json_attributes
end

#to_json(*args) ⇒ Object



138
139
140
# File 'lib/json_record/embedded_document.rb', line 138

def to_json (*args)
  @json_attributes.to_json(*args)
end

#valid_with_callbacks?Boolean

:nodoc:

Returns:



166
167
168
169
170
171
# File 'lib/json_record/embedded_document.rb', line 166

def valid_with_callbacks? #:nodoc:
  run_callbacks(:before_validation)
  valid = valid_without_callbacks?
  run_callbacks(:after_validation)
  valid
end

#valid_with_callbacks_3?Boolean

:nodoc:

Returns:



173
174
175
176
177
# File 'lib/json_record/embedded_document.rb', line 173

def valid_with_callbacks_3? #:nodoc:
  run_callbacks(:validation) do
    valid_without_callbacks_3?
  end
end