Class: OrientDB::AR::Base

Inherits:
Object
  • Object
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

Methods included from Validations

#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)
  # Simple field value lookup
  return self[method_name] if field?(method_name)
  # Dirty
  if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
    __send__("attribute#{$2}", $1)
    # Setter
  elsif method_name.to_s =~ /(.*?)=$/
    self[$1] = args.first
    # Boolean
  elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
    !!self[$1]
    # Unknown pattern
  else
    super
  end
end

Class Attribute Details

.oclass_nameObject



123
124
125
# File 'lib/model/base.rb', line 123

def oclass_name
  @oclass_name ||= name.to_s
end

Instance Attribute Details

#odocumentObject (readonly)

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

.clearObject



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

Returns:

  • (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

.oclassObject



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

#deleteObject



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

Returns:

  • (Boolean)


99
100
101
# File 'lib/model/base.rb', line 99

def deleted?
  @deleted ||= false
end

#field?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/model/base.rb', line 41

def field?(name)
  res = @odocument.field?(name)
  res
end

#inspectObject 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

Returns:

  • (Boolean)


103
104
105
# File 'lib/model/base.rb', line 103

def persisted?
  saved? && !deleted?
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (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)
  # Simple field value lookup
  return true if field?(method_name)
  # Dirty
  return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
  # Setter
  return true if method_name.to_s =~ /(.*?)=$/
  # Boolean
  return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
  # Unknown pattern
  super
end

#saveObject



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

Returns:

  • (Boolean)


95
96
97
# File 'lib/model/base.rb', line 95

def saved?
  @saved || @odocument.rid != '-1:-1'
end