Class: OrientSupport::Array

Inherits:
Array show all
Includes:
Support
Defined in:
lib/support/orient.rb

Instance Method Summary collapse

Methods included from Support

#as, #compose_where, #generate_sql_list, #while_s

Methods inherited from Array

#analyse, #from_orient, #orient_flatten, #to_or, #to_orient

Constructor Details

#initialize(work_on:, work_with:) ⇒ Array

During initialisation the model-instance to work on is stored in @orient.

The keyword_parameter »work_on« holds the record to work on. The second argument holds the array to work with

If instead of a model-instance the model-class is provided, a new model-instance is created and returned Its up to the caller to save the new instance in the database

Further a list of array-elements is expected, which are forwarded (as Array) to Array

Its used to initialize Objects comming from the database (i.e. /lib/base.rb)

elsif iv.is_a? Array OrientSupport::Array.new( work_on: self, work_with: iv.from_orient){ key.to_sym }



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/support/orient.rb', line 47

def initialize( work_on:, work_with: )
	@orient = work_on.class == Class ? work_on.new : work_on
	super work_with
	begin
	@name =  block_given? ? yield : @orient.attributes.key(self)
	rescue TypeError => e   #  not defined
		ActiveOrient::Base.logger.debug{ "--------------------Type Error ----------------------------------" }
	ActiveOrient::Base.logger.debug("OrientSupport::Array"){ "Attributes  #{@orient.attributes.inspect}" }
	ActiveOrient::Base.logger.debug("OrientSupport::Array"){ e.inspect 
	ActiveOrient::Base.logger.debug{ "indicates a try to access a non existing array element" }}
	nil
	rescue NameError =>e
	ActiveOrient::Base.logger.debug{ "--------------------Name Error ------------" }
	ActiveOrient::Base.logger.debug ("OrientSupport::Array"){ e.inspect }
	#ActiveOrient::Base.logger.error{ e.backtrace.map {|l| "  #{l}\n"}.join  }
	ActiveOrient::Base.logger.debug{ "due to a bug in ActiveSupport DateTime Calculations" }
	# we just ignore the error
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/support/orient.rb', line 199

def method_missing method, *args
	return if empty?
	if @orient.is_a? ActiveOrient::Model  #  IB::Model
		# delegate to public methods
		self.map{|x| x.public_send(method, *args)}
	else
		self.map{|x| x.send method, *args }
	end
rescue NoMethodError => e
	ActiveOrient::Base.logger.error("OrientSupport::Array"){ "#{self.inspect} MethodMissing  -> Undefined method: #{args.first} --  Args: #{args[1..-1].inspect}"}
	ActiveOrient::Base.logger.error {" The Message #{e.message}"}
	ActiveOrient::Base.logger.error{ e.backtrace.map {|l| "  #{l}\n"}.join  }
	raise
end

Instance Method Details

#<<(arg) ⇒ Object

Append the argument to the Array, changes the Array itself.

Returns the modified Array ( and is chainable ) # # i= V.get( ‘89:0’) # ii=i.zwoebelkuchen << ‘z78’ << 6 << [454, 787] # => [7, 5, 6, “z78”, 78, 45, “z78”, 6, 454, 787]

The change is immediately transmitted to the database.

The difference to ‘append`: that method accepts a komma separated list of arguments and returns the modified database-document. `<<` accepts only one argument. An Array is translated into multi-arguments of `append`

> t = G21.create ll: [‘test’,‘test_2’, 5, 8 , 7988, “uzg”] INFO->CREATE VERTEX ml_g21 CONTENT href=""test","test_2",5,8,7988,"uzg"">ll”: => #<ML::G21:0x0000000002622cb0 @metadata=:class=>“ml_g21”, :version=>1, :fieldTypes=>nil, :cluster=>271, :record=>0, @attributes=“test_2”, 5, 8, 7988, “uzg”]>

> t.ll << [9,10]
   INFO->update #271:0 set ll = ll || [9, 10]   return after @this
    => ["test", "test_2", 5, 8, 7988, "uzg"] 
> t.ll << [9,10] << 'u'
   INFO->update #271:0 set ll = ll || [9, 10]   return after @this
   INFO->update #271:0 set ll = ll || ['u']   return after @this
=> ["test", "test_2", 5, 8, 7988, "uzg", 9, 10]

The Array can be treated separately

> z =  t.ll

=> [“test”, “test_2”, 5, 8, 7988, “uzg”]

> z << 78
INFO->update #272:0 set ll = ll || [78]   return after @this
=> ["test", "test_2", 5, 8, 7988, "uzg", 78]


126
127
128
# File 'lib/support/orient.rb', line 126

def << arg
	append( *arg).send @name 
end

#[]=(key, value) ⇒ Object

Updating of single items



183
184
185
186
# File 'lib/support/orient.rb', line 183

def []= key, value
	super
	@orient.update set: {@name => self} if @name.present?  if check_if_complete
end

#append(*arg) ⇒ Object

Appends the arguments to the Array.

Returns the modified database-document (not the array !!)



83
84
85
86
87
# File 'lib/support/orient.rb', line 83

def   append *arg
	
	@orient.update { "set #{@name.to_s} = #{@name} || #{arg.to_or} "}[@name] if check_if_complete
	@orient.reload!
end

#as_json(o = nil) ⇒ Object



66
67
68
# File 'lib/support/orient.rb', line 66

def as_json o=nil
	map{|x| x.rid? ? x.rid : x }
end

#check_if_completeObject



172
173
174
175
176
177
178
179
# File 'lib/support/orient.rb', line 172

def check_if_complete
	if @name.blank?
		@orient.logger.warn{ "Database is uneffected. Operation is incomplete/ not allowed" }
		false
	else
		true
	end
end

#recordObject



70
71
72
# File 'lib/support/orient.rb', line 70

def record
	@orient
end

#remove(*k) ⇒ Object

Removes the specified list entries from the Array

Returns the modified Array (and is chainable).

> t= G21.first
> t.ll
  => ["test", "test_2", 7988, "uzg", 6789, "xvy"] 
> u=  t.ll << 'xvz'
# INFO->update #272:0 set ll = ll || ['xvz']   return after @this
=> ["test", "test_2", 7988, "uzg", 6789, "xvy", "xvz"] 
> z=  u.remove 'xvy'
# INFO->update #272:0 remove  ll = 'xvy'  return after @this
=> ["test", "test_2", 7988, "uzg", 6789, "xvz"]

The ModelInstance is updated, too, as shown by calling

> t.ll

=> ["test", "test_2", 7988, "uzg", 6789, "xvz"]

Thus

> t.ll.remove 7988
# INFO->update #272:0 remove  ll = 7988  return after @this
=> ["test", "test_2", "uzg", 6789, "xvz"]

returns thea modified Array



160
161
162
163
164
165
166
# File 'lib/support/orient.rb', line 160

def remove *k
	# todo combine queries in a transaction
	ActiveOrient::Base.logger.debug { "delete: #{@name} --< #{k.map(&:to_or).join( ' :: ' )}"}
 k.map{|l|	@orient.update( {remove: { @name => l} } ) }
	#		@orient.reload!
	#		@orient.send @name 
end

#remove_by_index(index) ⇒ Object



168
169
170
# File 'lib/support/orient.rb', line 168

def remove_by_index index
	@orient.update( { remove: { @name => "#{@name[index]}" } } )
end

#to_humanObject



74
75
76
# File 'lib/support/orient.rb', line 74

def to_human
	map &:to_human 
end

#where(*item) ⇒ Object

just works with Hashes as parameters



191
192
193
194
195
196
197
# File 'lib/support/orient.rb', line 191

def where *item
	where_string = item.map{|m| where_string = compose_where( m ) }.join(' and ')
	subquery= OrientSupport::OrientQuery.new from: @orient, projection: "expand( #{@name})"
	q= OrientSupport::OrientQuery.new from: subquery, where: item
	@orient.db.execute{  q.to_s } if check_if_complete

end