Class: ActiveOrient::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Serialization, ActiveModel::Serializers::JSON, 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.



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/base.rb', line 72

def initialize attributes = {}, opts = {}
  logger.progname = "ActiveOrient::Base#initialize"
  @metadata = HashWithIndifferentAccess.new
  @d =  nil
  run_callbacks :initialize do
	if RUBY_PLATFORM == 'java' && attributes.is_a?( Document )
	  @d = attributes
	  attributes =  @d.values
	  @metadata[:class]      = @d.class_name
	  @metadata[:version]    = @d.version
	  @metadata[:cluster], @metadata[: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
	  @metadata[:type]       = attributes.delete '@type'
	  @metadata[:class]      = attributes.delete '@class'
	  @metadata[:version]    = attributes.delete '@version'
	  @metadata[:fieldTypes] = attributes.delete '@fieldTypes'
	  if attributes.has_key?('@rid')
 rid = attributes.delete '@rid'
 cluster, record = rid[1,rid.size].split(':')
 @metadata[:cluster] = cluster.to_i
 @metadata[:record]  = record.to_i
	  end

	  if @metadata[:fieldTypes ].present? && (@metadata[:fieldTypes] =~ /=g/)
 @metadata[:edges] = { :in => [], :out => [] }
 edges = @metadata['fieldTypes'].split(',').find_all{|x| x=~/=g/}.map{|x| x.split('=').first}
	  #  puts "Detected EDGES: #{edges.inspect}"
 edges.each do |edge|
   operator, *base_edge = edge.split('_')
   base_edge = base_edge.join('_')
   @metadata[:edges][operator.to_sym] << base_edge
 end
	  #    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
	  #      allocate_edge_method = -> (edge)  do
	  #        unless (ee=db.get_db_superclass(edge)) == "E"
	  #          allocate_edge_method[ee]
	  #          self.class.send :alias_method, base_edge.underscore, edge
	  #      ## define inherented classes, tooa
	  #      

	  #    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 
  @metadata
end

Class Method Details

.attr_accessible(*args) ⇒ Object



225
226
# File 'lib/base.rb', line 225

def self.attr_accessible *args
end

.attr_protected(*args) ⇒ Object

Noop methods mocking ActiveRecord::Base macros



222
223
# File 'lib/base.rb', line 222

def self.attr_protected *args
end

.belongs_to(model, *args) ⇒ Object

ActiveRecord::Base association API mocks



230
231
232
# File 'lib/base.rb', line 230

def self.belongs_to model, *args
  attr_accessor model
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

.has_many(models, *args) ⇒ Object



238
239
240
241
242
243
# File 'lib/base.rb', line 238

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

.has_one(model, *args) ⇒ Object



234
235
236
# File 'lib/base.rb', line 234

def self.has_one model, *args
  attr_accessor model
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

.reset_rid_storeObject



44
45
46
# File 'lib/base.rb', line 44

def self.reset_rid_store
  @@rid_store = Hash.new
end

.serialize(*properties) ⇒ Object

ActiveRecord::Base misc



251
252
# File 'lib/base.rb', line 251

def self.serialize *properties
end

.store_rid(obj) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/base.rb', line 48

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


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/base.rb', line 167

def [] key
  iv = attributes[key.to_sym]
#      puts my_metadata( key: key) 
   if ( key: key) == "t"
#	puts "time detected"
	iv =~ /00:00:00/ ? Date.parse(iv) : DateTime.parse(iv)
  elsif ( key: key) == "x"
	iv = ActiveOrient::Model.autoload_object 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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/base.rb', line 187

def []= key, val
#      puts "here I am: #{key}, #{val}"
  val = val.rid if val.is_a?( ActiveOrient::Model ) && val.rid.rid?
  attributes[key.to_sym] = case val
      when Array
#				 puts "I am an 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)



141
142
143
# File 'lib/base.rb', line 141

def attributes
  @attributes ||= HashWithIndifferentAccess.new
end

#attributes=(attrs) ⇒ Object



145
146
147
# File 'lib/base.rb', line 145

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

#documentObject



63
64
65
# File 'lib/base.rb', line 63

def document
  @d
end

#my_metadata(key: nil, symbol: nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/base.rb', line 149

def  key: nil, symbol: nil
  if @metadata[:fieldTypes].present?  
	meta= Hash[ @metadata['fieldTypes'].split(',').map{|x| x.split '='} ]
	if key.present?
	  meta[key.to_s]
	elsif symbol.present?
	  meta.map{|x,y| x if y == symbol.to_s }.compact
	else
	  meta
	end
  end
end

#to_modelObject



216
217
218
# File 'lib/base.rb', line 216

def to_model
  self
end

#update_attribute(key, value) ⇒ Object



212
213
214
# File 'lib/base.rb', line 212

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