Class: OrientSupport::Array

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

Overview

This Module fences specialized Ruby objects

Direct Known Subclasses

LinkMap

Instance Method Summary collapse

Methods included from Support

#compose_where, #generate_sql_list

Methods inherited from Array

#from_orient, #to_orient

Constructor Details

#initialize(work_on:, work_with:) ⇒ Array

Initialisation method stores the model-instance to work on in @orient.

The keyword_parameter "work_on" holds the record to work_ion.
Ihe second argument is 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


20
21
22
23
24
25
26
27
28
# File 'lib/orient.rb', line 20

def initialize work_on:, work_with: 
  @orient = work_on.class == Class ? work_on.new : work_on
  super work_with
  @name = @orient.attributes.key(self)
#  puts "ORIENT: #{@orient.inspect} "
  @name =  yield if @name.nil? && block_given?
#  puts "NAME: #{@name.inspect}"
#  puts "SELF: #{self.inspect}"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/orient.rb', line 95

def method_missing *args
  begin
    map{|x| x.send args.first}
  rescue NoMethodError => e
    logger.progname = "OrientSupport::Array#MethodMissing"
    logger.error{"Undefined method: #{e.message}"}
  end
end

Instance Method Details

#<<(arg) ⇒ Object

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

The change is transmitted to the database immediately



38
39
40
41
42
43
44
# File 'lib/orient.rb', line 38

def << arg
#      print "\n <<---> #{@name}, #{arg} <--- \n"
  if @name.present?
	@orient.add_item_to_property(@name, arg)
  end
  super
end

#[](*arg) ⇒ Object



57
58
59
# File 'lib/orient.rb', line 57

def [] *arg
  super
end

#[]=(key, value) ⇒ Object

Updating of single items

This only works if the hole embedded Array is previously loaded into the Ruby-array.


52
53
54
55
# File 'lib/orient.rb', line 52

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

#delete(*item) ⇒ Object



77
78
79
# File 'lib/orient.rb', line 77

def delete *item
  @orient.remove_item_from_property(@name){item} if @name.present?
end

#delete_at(*pos) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/orient.rb', line 61

def delete_at *pos
  if @name.present?
    delete self[*pos]
  else
   super
  end
end

#delete_ifObject



69
70
71
72
73
74
75
# File 'lib/orient.rb', line 69

def delete_if
  if @name.present?
    delete *self.map{|y| y if yield(y)}.compact  # if the block returns true then delete the item
  else
   super
  end
end

#recordObject



30
31
32
# File 'lib/orient.rb', line 30

def record
  @orient
end

#where(*item) ⇒ Object

just works with Hashes as parameters



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/orient.rb', line 82

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
#      query = "SELECT FROM ( SELECT EXPAND( #{@name} ) FROM #{@orient.classname})  #{where_string} "
 # puts q.compose
 #  sql_cmd = -> (command) {{ type: "cmd", language: "sql", command: command }}
#  @orient.orientdb.execute do
#	  sql_cmd[query.to_s]
#      end
   @orient.query q 
end