Class: ActiveOrient::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations, OrientDB
Defined in:
lib/base.rb

Overview

Base class for tableless IB data Models, extends ActiveModel API

Direct Known Subclasses

Model

Constant Summary collapse

@@rid_store =

Every Rest::Base-Object is stored in the @@rid_store

The Objects are just references to the @@rid_store.
Any Change of the Object is thus synchonized to any allocated variable.
Hash.new

Constants included from OrientDB

OrientDB::DocumentDatabase, OrientDB::DocumentDatabasePool, OrientDB::DocumentDatabasePooled, OrientDB::GraphDatabase, OrientDB::IndexType, OrientDB::OClassImpl, OrientDB::OTraverse, OrientDB::PropertyImpl, OrientDB::RemoteStorage, OrientDB::SQLCommand, OrientDB::SQLSynchQuery, OrientDB::Schema, OrientDB::SchemaProxy, OrientDB::SchemaType, OrientDB::ServerAdmin, OrientDB::User, OrientDB::UsingJava

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, opts = {}) ⇒ Base

If a opts hash is given, keys are taken as attribute names, values as data. The model instance fields are then set automatically from the opts Hash.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/base.rb', line 68

def initialize attributes = {}, opts = {}
  logger.progname = "ActiveOrient::Base#initialize"
   = HashWithIndifferentAccess.new
  @d =  nil
  run_callbacks :initialize do
  if RUBY_PLATFORM == 'java' && attributes.is_a?( Document )
    @d = attributes
    attributes =  @d.values
    [:class]      = @d.class_name
    [:version]    = @d.version
    [:cluster], [:record] = @d.rid[1,@d.rid.size].split(':')



  end
  attributes.keys.each do |att|
    unless att[0] == "@" # @ identifies Metadata-attributes
 att = att.to_sym if att.is_a?(String)
 unless self.class.instance_methods.detect{|x| x == att}
   self.class.define_property att, nil
 else
   #logger.info{"Property #{att.to_s} NOT assigned"}
 end
    end
  end

  if attributes['@type'] == 'd'  # document via REST
    [:type]       = attributes.delete '@type'
    [:class]      = attributes.delete '@class'
    [:version]    = attributes.delete '@version'
    [:fieldTypes] = attributes.delete '@fieldTypes'
    if attributes.has_key?('@rid')
 rid = attributes.delete '@rid'
 cluster, record = rid[1,rid.size].split(':')
 [:cluster] = cluster.to_i
 [:record]  = record.to_i
    end

    if [:fieldTypes ].present? && ([:fieldTypes] =~ /=g/)
 edges = ['fieldTypes'].split(',').find_all{|x| x=~/=g/}.map{|x| x.split('=').first}
 edges.each do |edge|
   operator, *base_edge = edge.split('_')
   base_edge = base_edge.join('_')
   unless self.class.instance_methods.detect{|x| x == base_edge}
    ## define two methods: out_{Edge}/in_{Edge} -> edge.
    self.class.define_property base_edge, nil
    self.class.send :alias_method, base_edge.underscore, edge
   end
 end
    end
  end
  self.attributes = attributes # set_attribute_defaults is now after_init callback
  end
  #      puts "Storing #{self.rid} to rid-store"
  ActiveOrient::Base.store_rid self
end

Instance Attribute Details

#metadataObject (readonly)

Used to read the metadata



22
23
24
# File 'lib/base.rb', line 22

def 
  
end

Class Method Details

.attr_accessible(*args) ⇒ Object



192
193
# File 'lib/base.rb', line 192

def self.attr_accessible *args
end

.attr_protected(*args) ⇒ Object

Noop methods mocking ActiveRecord::Base macros



189
190
# File 'lib/base.rb', line 189

def self.attr_protected *args
end

.display_ridObject



31
32
33
# File 'lib/base.rb', line 31

def self.display_rid
  @@rid_store
end

.get_rid(rid) ⇒ Object



39
40
41
42
# File 'lib/base.rb', line 39

def self.get_rid rid
  rid =  rid[1..-1] if rid[0]=='#'
  @@rid_store[rid] 
end

.remove_rid(obj) ⇒ Object



35
36
37
# File 'lib/base.rb', line 35

def self.remove_rid obj
  @@rid_store.delete obj.rid
end

.serialize(*properties) ⇒ Object

ActiveRecord::Base association API mocks

def self.belongs_to model, *args
  attr_accessor model
end

def self.has_one model, *args
  attr_accessor model
end

def self.has_many models, *args
  attr_accessor models
  define_method(models) do
    self.instance_variable_get("@#{models}") || self.instance_variable_set("@#{models}", [])
  end
end

def self.find *args
  []
end

ActiveRecord::Base misc



218
219
# File 'lib/base.rb', line 218

def self.serialize *properties
end

.store_rid(obj) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/base.rb', line 44

def self.store_rid obj
  if obj.rid.present? && obj.rid.rid?
    # return the presence of a stored object as true by the block
    # the block is only executed if the presence is confirmed
    # Nothing is returned from the class-method
   if @@rid_store[obj.rid].present?
     yield if block_given?
   end
   @@rid_store[obj.rid] = obj
   @@rid_store[obj.rid]  # return_value
  else
   obj # no rid-value: just return the obj
  end
end

Instance Method Details

#[](key) ⇒ Object

ActiveModel-style read/write_attribute accessors

Autoload mechanism and data conversion are defined in the method "from_orient" of each class


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/base.rb', line 140

def [] key
  iv = attributes[key.to_sym]
  if [:fieldTypes].present? && [:fieldTypes].include?(key.to_s+"=t")
  iv =~ /00:00:00/ ? Date.parse(iv) : DateTime.parse(iv)
  elsif iv.is_a? Array
    OrientSupport::Array.new( work_on: self, work_with: iv.from_orient){ key.to_sym }
 elsif iv.is_a? Hash
    OrientSupport::Hash.new( self, iv){ key.to_sym }
#     elsif iv.is_a? RecordMap 
 #      iv
#       puts "RecordSet detected"
  else
  iv.from_orient
  end
end

#[]=(key, val) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/base.rb', line 156

def []= key, val
  val = val.rid if val.is_a?( ActiveOrient::Model ) && val.rid.rid?
  attributes[key.to_sym] = case val
      when Array
 if val.first.is_a?(Hash)
   v = val.map do |x|
     if x.is_a?(Hash)
       HashWithIndifferentAccess.new(x)
     else
       x
     end
   end
   OrientSupport::Array.new(work_on: self, work_with: v )
 else
   OrientSupport::Array.new(work_on: self, work_with: val )
 end
      when Hash
 HashWithIndifferentAccess.new(val)
      else
 val
      end
end

#attributesObject

ActiveModel API (for serialization)



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

def attributes
  @attributes ||= HashWithIndifferentAccess.new
end

#attributes=(attrs) ⇒ Object



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

def attributes= attrs
  attrs.keys.each{|key| self.send("#{key}=", attrs[key])}
end

#documentObject



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

def document
  @d
end

#to_modelObject



183
184
185
# File 'lib/base.rb', line 183

def to_model
  self
end

#update_attribute(key, value) ⇒ Object



179
180
181
# File 'lib/base.rb', line 179

def update_attribute key, value
  @attributes[key] = value
end