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.

Defined Under Namespace

Modules: ValidationCallbacks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

The parent object of the document.



34
35
36
# File 'lib/json_record/embedded_document.rb', line 34

def parent
  @parent
end

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/json_record/embedded_document.rb', line 8

def self.included (base)
  base.send :include, ActiveModel::AttributeMethods
  base.send :include, ActiveModel::Dirty
  base.send :include, ActiveModel::Validations
  base.send :include, AttributeMethods
  base.send :include, ActiveSupport::Callbacks
  
  base.define_callbacks :validation
  base.alias_method_chain(:valid?, :callbacks)
  base.extend ValidationCallbacks
  
  base.class_attribute :schema
  base.schema = Schema.new(base, nil)
end

Instance Method Details

#==(val) ⇒ Object



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

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

#[](name) ⇒ Object

Get a field from the schema with the specified name.



67
68
69
70
# File 'lib/json_record/embedded_document.rb', line 67

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.



73
74
75
76
# File 'lib/json_record/embedded_document.rb', line 73

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.



44
45
46
# File 'lib/json_record/embedded_document.rb', line 44

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

#attributes=(attrs) ⇒ Object

Set all the attributes at once.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/json_record/embedded_document.rb', line 49

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.



62
63
64
# File 'lib/json_record/embedded_document.rb', line 62

def attributes_before_type_cast
  json_attributes_before_type_cast
end

#eql?(val) ⇒ Boolean

Returns:



86
87
88
# File 'lib/json_record/embedded_document.rb', line 86

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

#equal?(val) ⇒ Boolean

Returns:



94
95
96
# File 'lib/json_record/embedded_document.rb', line 94

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

#hashObject



98
99
100
# File 'lib/json_record/embedded_document.rb', line 98

def hash
  attributes.hash + parent.hash
end

#initialize(attrs = {}) ⇒ Object

Create an embedded document with the specified attributes.



37
38
39
40
41
# File 'lib/json_record/embedded_document.rb', line 37

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

#inspectObject



102
103
104
# File 'lib/json_record/embedded_document.rb', line 102

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

#to_hashObject



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

def to_hash
  @json_attributes
end

#to_json(*args) ⇒ Object



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

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

#valid_with_callbacks?Boolean

:nodoc:

Returns:



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

def valid_with_callbacks? #:nodoc:
  run_callbacks(:validation) do
    valid_without_callbacks?
  end
end