Class: OrientSupport::Array

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

Instance Method Summary collapse

Methods included from Support

#compose_where, #generate_sql_list

Methods inherited from Array

#analyse, #from_orient, #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 }



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/support/orient.rb', line 32

def initialize( work_on:, work_with: )
  @orient = work_on.class == Class ? work_on.new : work_on
  super work_with
  begin
  @name = @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
  @name =  yield if @name.nil? && block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/support/orient.rb', line 110

def method_missing *args

  self.map{|x| x.send *args }
rescue NoMethodError => e
  ActiveOrient::Base.logger.error("OrientSupport::Array"){ "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  }
end

Instance Method Details

#[]=(key, value) ⇒ Object

Updating of single items



94
95
96
97
# File 'lib/support/orient.rb', line 94

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

#append(*arg) ⇒ Object Also known as: <<

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

The change is immediately transmitted to the database.



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

def   append *arg

  @orient.update { "#{@name.to_s} = #{@name} || #{arg.to_or} "}[@name]
end

#as_json(o = nil) ⇒ Object



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

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

#recordObject



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

def record
  @orient
end

#remove(*k) ⇒ Object



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

def remove *k
  # todo combine queries in a transaction
  puts "delete: #{@name} --< #{k.map(&:to_or).join( ' :: ' )}"
  k.each{|item| @orient.remove( " #{@name} = #{item.to_or}")[@name] }
end

#to_humanObject



60
61
62
# File 'lib/support/orient.rb', line 60

def to_human
  map &:to_human 
end

#where(*item) ⇒ Object

just works with Hashes as parameters



102
103
104
105
106
107
108
# File 'lib/support/orient.rb', line 102

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.query q.to_s 

end