lib/orient.rb in active-orient-0.4 vs lib/orient.rb in active-orient-0.5

- old
+ new

@@ -1,98 +1,161 @@ module OrientSupport -=begin -This Module fences specialized ruby objects -=end +# This Module fences specialized Ruby objects + class Array < Array - include Support + include OrientSupport::Support + mattr_accessor :logger + =begin -Initialisation method stores the modelinstance in @orient. + 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 -Further a list of array-elements is expected, which are forwarded (as Array) to Array + 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 =end - def initialize modelinstance, *args - @orient = modelinstance - super args - @name = modelinstance.attributes.key(self) - + + 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 + def record + @orient + end +=begin +Append the argument to the Array, changes the Array itself. + +The change is transmitted to the database immediately +=end def << arg - @orient.add_item_to_property( @name, arg ) if @name.present? +# print "\n <<---> #{@name}, #{arg} <--- \n" + if @name.present? + @orient.add_item_to_property(@name, arg) + end super end =begin -Updating of single items + Updating of single items -this only works if the hole embedded Array is previosly loaded into the ruby-array. + This only works if the hole embedded Array is previously loaded into the Ruby-array. =end - + def []= key, value super - @orient.update set: { @name => self } if @name.present? + @orient.update set: {@name => self} if @name.present? end def [] *arg - # puts "[] ARG: #{arg.inspect}" - # arg.each{|u| puts "[] #{u.inspect} : #{u.class} " } super - end def delete_at *pos - if @name.present? - delete self[*pos] + if @name.present? + delete self[*pos] else - super + super end - # old version: works only if the hole array is loaded into memory -# self[*pos]=nil -# @orient.update set:{ @name => self.compact } end def delete_if if @name.present? - delete *self.map{|y| y if yield(y) }.compact # if the block returns true then delete the item + delete *self.map{|y| y if yield(y)}.compact # if the block returns true then delete the item else - super + super end end def delete *item - @orient.remove_item_from_property( @name ) { item } if @name.present? + @orient.remove_item_from_property(@name){item} if @name.present? end + ## just works with Hashes as parameters def where *item - where_string = item.map{|m| where_string = compose_where m }.join( ' and ' ) - query = "select from ( select expand( #{@name} ) from #{@orient.classname}) #{where_string} " - puts query - @orient.query query - + 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 def method_missing *args - map{|x| x.send args.first } + begin + map{|x| x.send args.first} + rescue NoMethodError => e + logger.progname = "OrientSupport::Array#MethodMissing" + logger.error{"Undefined method: #{e.message}"} + end end - end - class LinkMap < OrientSupport::Array + end #Class + + class LinkMap < OrientSupport::Array def []= arg end - end + end #Class -# -# class Hash < Hash_with_indifferent_access -# # additional and overlayed methods for Hash-Objects in OrientDB -# def initialize modelinstance, *args -# @orient = modelinstance -# super args -# @name = modelinstance.attributes.key(self) -# + + + + class Hash < HashWithIndifferentAccess + include OrientSupport::Support + def initialize modelinstance, args + @orient = modelinstance + super args.from_orient + # @name is the property of @orient to work on + @name = modelinstance.attributes.key(self) +# puts "ORIENT: #{@orient.inspect} " + @name = yield if @name.nil? && block_given? +# puts "NAME: #{@name.inspect}" +# puts "SELF: #{self.inspect}" + end + + + def []= key, value + puts " i will handle this in the future" + #@orient.attributes[key] = value + +# r = (@orient.query "update #{@orient.rid} put #{@name} = #{key.to_orient}, #{value.to_orient} RETURN AFTER @this").pop + super key, value + @orient.update set:{ @name => self} +# @orient = @orient.class(@orient.rid){r} if r.is_a? ActiveOrient::Model +# self[ key ]= value +# puts self.inspect + #@orient[@name]=self.merge key => value + # + end + + def delete key + super key + @orient.update set:{ @name => self} + end + + def delete_if &b + super &b + @orient.update set:{ @name => self} + + end + +# def << + + +# def to_orient +# self # end -# -# end -end + end +end #Module