Class: OrientDB::AR::Base

Inherits:
Object
  • Object
show all
Extended by:
Relations
Includes:
ActiveModel::AttributeMethods, DocumentMixin
Defined in:
lib/orientdb-ar/base.rb

Class Attribute Summary collapse

Attributes included from DocumentMixin

#odocument

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relations

embedds_many, embedds_one, links_many, links_one

Methods included from DocumentMixin

#<=>, #connection, #embedded?, #field?, #human_id, included, #initialize, #inspect, #method_missing, #oclass, #oclass_name, #related, #related_valid?, #respond_to?, #rid, #valid_with_default_group?, #validate, #validate_to_parent

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OrientDB::AR::DocumentMixin

Class Attribute Details

.oclass_name=(value) ⇒ Object (writeonly)

Sets the attribute oclass_name

Parameters:

  • value

    the value to set the attribute oclass_name to.



67
68
69
# File 'lib/orientdb-ar/base.rb', line 67

def oclass_name=(value)
  @oclass_name = value
end

Class Method Details

.all(conditions = { }) ⇒ Object



127
128
129
# File 'lib/orientdb-ar/base.rb', line 127

def all(conditions = { })
  OrientDB::AR::Query.new(self).where(conditions).all
end

.clearObject



151
152
153
# File 'lib/orientdb-ar/base.rb', line 151

def clear
  oclass.truncate
end

.countObject



147
148
149
# File 'lib/orientdb-ar/base.rb', line 147

def count
  oclass.count
end

.create(fields = { }) ⇒ Object



99
100
101
102
103
# File 'lib/orientdb-ar/base.rb', line 99

def create(fields = { })
  obj = new fields
  obj.save
  obj
end

.delete(*args) ⇒ Object



139
140
141
# File 'lib/orientdb-ar/base.rb', line 139

def delete(*args)
  OrientDB::AR::Delete.new(self).where(*args).run
end

.descends_from_base?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/orientdb-ar/base.rb', line 95

def descends_from_base?
  superclass && superclass == OrientDB::AR::Base
end

.embeddable?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/orientdb-ar/base.rb', line 69

def embeddable?
  false
end

.field(name, type, options = { }) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/orientdb-ar/base.rb', line 86

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



131
132
133
# File 'lib/orientdb-ar/base.rb', line 131

def first(conditions = { })
  OrientDB::AR::Query.new(self).where(conditions).first
end

.insert(*args) ⇒ Object



143
144
145
# File 'lib/orientdb-ar/base.rb', line 143

def insert(*args)
  from_orientdb OrientDB::AR::Insert.new(self).fields(*args).run
end

.limit(max_records) ⇒ Object



119
120
121
# File 'lib/orientdb-ar/base.rb', line 119

def limit(max_records)
  OrientDB::AR::Query.new(self).limit(max_records)
end

.oclassObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/orientdb-ar/base.rb', line 73

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



115
116
117
# File 'lib/orientdb-ar/base.rb', line 115

def order(*args)
  OrientDB::AR::Query.new(self).order(*args)
end

.range(lower_rid, upper_rid = nil) ⇒ Object



123
124
125
# File 'lib/orientdb-ar/base.rb', line 123

def range(lower_rid, upper_rid = nil)
  OrientDB::AR::Query.new(self).range(lower_rid, upper_rid)
end

.select(*args) ⇒ Object Also known as: columns



105
106
107
# File 'lib/orientdb-ar/base.rb', line 105

def select(*args)
  OrientDB::AR::Query.new(self).select(*args)
end

.update(*args) ⇒ Object



135
136
137
# File 'lib/orientdb-ar/base.rb', line 135

def update(*args)
  OrientDB::AR::Update.new(self).values(*args).run
end

.where(*args) ⇒ Object



111
112
113
# File 'lib/orientdb-ar/base.rb', line 111

def where(*args)
  OrientDB::AR::Query.new(self).where(*args)
end

Instance Method Details

#deleteObject



35
36
37
38
39
40
41
# File 'lib/orientdb-ar/base.rb', line 35

def delete
  _run_delete_callbacks do
    @odocument.delete
    @deleted = true
  end
  true
end

#deleted?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/orientdb-ar/base.rb', line 55

def deleted?
  @deleted ||= false
end

#persisted?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/orientdb-ar/base.rb', line 59

def persisted?
  saved? && !deleted?
end

#reloadObject



43
44
45
46
47
48
49
# File 'lib/orientdb-ar/base.rb', line 43

def reload
  raise "Not persisted, cannot reload" unless persisted?
  @odocument          = OrientDB::AR::Query.new(self.class).where('@rid' => rid.lit).first_result
  @changed_attributes = { }
  @errors             = ActiveModel::Errors.new(self)
  self
end

#save(perform_validations = true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/orientdb-ar/base.rb', line 10

def save(perform_validations = true)
  _run_save_callbacks do
    if perform_validations
      validate
      if @last_validation_result
        save_without_validations
      else
        false
      end
    else
      save_without_validations
    end
  end
end

#saved?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/orientdb-ar/base.rb', line 51

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