Class: OrientDB::AR::Base
- Inherits:
-
Object
- Object
- OrientDB::AR::Base
show all
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Translation
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, Comparable, Attributes, Conversion, Validations
- Defined in:
- lib/model/base.rb
Class Attribute Summary collapse
Instance 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 = {}) ⇒ Base
Returns a new instance of Base.
35
36
37
38
39
|
# File 'lib/model/base.rb', line 35
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/model/base.rb', line 59
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
123
124
125
|
# File 'lib/model/base.rb', line 123
def oclass_name
@oclass_name ||= name.to_s
end
|
Instance Attribute Details
#odocument ⇒ Object
Returns the value of attribute odocument.
33
34
35
|
# File 'lib/model/base.rb', line 33
def odocument
@odocument
end
|
Class Method Details
.all(conditions = {}) ⇒ Object
192
193
194
|
# File 'lib/model/base.rb', line 192
def all(conditions = {})
OrientDB::AR::Query.new(self).where(conditions).all
end
|
.clear ⇒ Object
200
201
202
|
# File 'lib/model/base.rb', line 200
def clear
oclass.truncate
end
|
.create(fields = {}) ⇒ Object
164
165
166
167
168
|
# File 'lib/model/base.rb', line 164
def create(fields = {})
obj = new fields
obj.save
obj
end
|
.descends_from_base? ⇒ Boolean
149
150
151
|
# File 'lib/model/base.rb', line 149
def descends_from_base?
superclass && superclass == OrientDB::AR::Base
end
|
.field(name, type, options = {}) ⇒ Object
140
141
142
143
144
145
146
147
|
# File 'lib/model/base.rb', line 140
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
|
.first(conditions = {}) ⇒ Object
196
197
198
|
# File 'lib/model/base.rb', line 196
def first(conditions = {})
OrientDB::AR::Query.new(self).where(conditions).first
end
|
.limit(max_records) ⇒ Object
184
185
186
|
# File 'lib/model/base.rb', line 184
def limit(max_records)
OrientDB::AR::Query.new(self).limit(max_records)
end
|
.new_document(fields = {}) ⇒ Object
159
160
161
162
|
# File 'lib/model/base.rb', line 159
def new_document(fields = {})
oclass
OrientDB::Document.new connection, oclass_name, fields
end
|
.new_from_doc(doc) ⇒ Object
204
205
206
207
208
209
|
# File 'lib/model/base.rb', line 204
def new_from_doc(doc)
klass = doc.getClassName.constantize
obj = klass.new
obj.instance_variable_set "@odocument", doc
obj
end
|
.oclass ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/model/base.rb', line 127
def oclass
unless defined?(@oclass)
options = {}
unless descends_from_base?
super_oclass = superclass.oclass
options[:super] = super_oclass
options[:use_cluster] = super_oclass.cluster_ids.first
end
@oclass = connection.get_or_create_class oclass_name, options
end
@oclass
end
|
.order(*args) ⇒ Object
180
181
182
|
# File 'lib/model/base.rb', line 180
def order(*args)
OrientDB::AR::Query.new(self).order(*args)
end
|
.range(lower_rid, upper_rid = nil) ⇒ Object
188
189
190
|
# File 'lib/model/base.rb', line 188
def range(lower_rid, upper_rid = nil)
OrientDB::AR::Query.new(self).range(lower_rid, upper_rid)
end
|
.schema! ⇒ Object
153
154
155
156
157
|
# File 'lib/model/base.rb', line 153
def schema!
fields.each do |field, options|
oclass.add field, options[:type], options.except(:type)
end
end
|
.select(*args) ⇒ Object
Also known as:
columns
170
171
172
|
# File 'lib/model/base.rb', line 170
def select(*args)
OrientDB::AR::Query.new(self).select(*args)
end
|
.where(*args) ⇒ Object
176
177
178
|
# File 'lib/model/base.rb', line 176
def where(*args)
OrientDB::AR::Query.new(self).where(*args)
end
|
Instance Method Details
#<=>(other) ⇒ Object
115
116
117
|
# File 'lib/model/base.rb', line 115
def <=>(other)
to_s <=> other.to_s
end
|
#delete ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/model/base.rb', line 87
def delete
_run_delete_callbacks do
@odocument.delete
@deleted = true
end
true
end
|
#deleted? ⇒ Boolean
99
100
101
|
# File 'lib/model/base.rb', line 99
def deleted?
@deleted ||= false
end
|
#field?(name) ⇒ Boolean
41
42
43
44
|
# File 'lib/model/base.rb', line 41
def field?(name)
res = @odocument.field?(name)
res
end
|
#inspect ⇒ Object
Also known as:
to_s
107
108
109
110
111
|
# File 'lib/model/base.rb', line 107
def inspect
attrs = attributes.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
super_klass = self.class.descends_from_base? ? '' : "(#{self.class.superclass.name})"
%{#<#{self.class.name}#{super_klass}:#{@odocument.rid} #{attrs}>}
end
|
#persisted? ⇒ Boolean
103
104
105
|
# File 'lib/model/base.rb', line 103
def persisted?
saved? && !deleted?
end
|
#respond_to?(method_name) ⇒ Boolean
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/model/base.rb', line 46
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
|
#save ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/model/base.rb', line 77
def save
_run_save_callbacks do
@odocument.save
@saved = true
@previously_changed = @changed_attributes
@changed_attributes.clear
end
true
end
|
#saved? ⇒ Boolean
95
96
97
|
# File 'lib/model/base.rb', line 95
def saved?
@saved || @odocument.rid != '-1:-1'
end
|