Class: OrientDB::AR::Embedded
Class Attribute Summary collapse
Attributes included from Validations
#errors
Class Method Summary
collapse
Instance Method Summary
collapse
#valid?
Methods included from Conversion
#to_key, #to_model, #to_param
Methods included from Attributes
#[], #[]=, #attribute_names, #attributes, #changed, #changed?, #changed_attributes, #changes, #previous_changes
Constructor Details
#initialize(fields = {}) ⇒ Embedded
Returns a new instance of Embedded.
11
12
13
14
15
|
# File 'lib/model/embedded.rb', line 11
def initialize(fields = {})
@odocument = self.class.new_document fields
@changed_attributes = {}
@errors = ActiveModel::Errors.new(self)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/model/embedded.rb', line 34
def method_missing(method_name, *args, &blk)
return self[method_name] if field?(method_name)
if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
__send__("attribute#{$2}", $1)
elsif method_name.to_s =~ /(.*?)=$/
self[$1] = args.first
elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
!!self[$1]
else
super
end
end
|
Class Attribute Details
.oclass_name ⇒ Object
56
57
58
|
# File 'lib/model/embedded.rb', line 56
def oclass_name
@oclass_name ||= name.downcase.gsub('::', '_')
end
|
Class Method Details
.field(name, type, options = {}) ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/model/embedded.rb', line 68
def field(name, type, options = {})
name = name.to_sym
if fields.key? name
puts "Already defined field [#{name}]"
else
fields[name] = {:type => type}.update options
end
end
|
.fields ⇒ Object
64
65
66
|
# File 'lib/model/embedded.rb', line 64
def fields
@fields ||= ActiveSupport::OrderedHash.new
end
|
.new_document(fields = {}) ⇒ Object
77
78
79
80
|
# File 'lib/model/embedded.rb', line 77
def new_document(fields = {})
oclass
OrientDB::Document.new connection, oclass_name, fields
end
|
.oclass ⇒ Object
60
61
62
|
# File 'lib/model/embedded.rb', line 60
def oclass
@oclass ||= connection.get_or_create_class oclass_name
end
|
Instance Method Details
#field?(name) ⇒ Boolean
17
18
19
|
# File 'lib/model/embedded.rb', line 17
def field?(name)
@odocument.field?(name)
end
|
#respond_to?(method_name) ⇒ Boolean
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/model/embedded.rb', line 21
def respond_to?(method_name)
return true if field?(method_name)
return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
return true if method_name.to_s =~ /(.*?)=$/
return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
super
end
|