module ModelRecord
Public Instance Methods
Removes the Model-Instance from the database.
It is overloaded in Vertex and Edge.
# File lib/model/the_record.rb, line 124 def delete orientdb.delete_record self end
# def execute query, delete_cash: false #
# query.from rrid if query.is_a?( OrientSupport::OrientQuery
) && query.from.nil? # ActiveOrient::Base.remove_rid
( self ) if delete_cash # result = orientdb.execute{ query.to_s } # result = if block_given? # result.is_a?(Array
)? result.map{|x| yield x } : yield(result) # else # result # end # if result.is_a? Array
# OrientSupport::Array.new
work_on: self, work_with: result.orient_flatten # else # result # end # return value # end # Fires a »where-Query» to the database starting with the current model-record.
Attributes:
-
a string ( obj.find “in().out().some_attribute >3” )
-
a hash ( obj.find 'some_embedded_obj.name' => 'test' )
-
an array
Returns the result-set, ie. a Query-Object which contains links to the addressed records.
# File lib/model/the_record.rb, line 94 def find attributes = {} q = OrientSupport::OrientQuery.new from: self, where: attributes query q end
flag whether a property exists on the Record-level
# File lib/model/the_record.rb, line 21 def has_property? property attributes.keys.include? property.to_sym end
How to handle other calls
-
if attribute is specified, display it
-
if attribute= is provided, assign to the known property or create a new one
Example:
ORD.create_class :a a = A.new a.test= 'test' # <--- attribute: 'test=', argument: 'test' a.test # <--- attribute: 'test' --> fetch attributes[:test]
Assignments are performed only in ruby-space.
Automatic database-updates are deactivated for now
# File lib/model/the_record.rb, line 273 def method_missing *args # if the first entry of the parameter-array is a known attribute # proceed with the assignment if args.size == 1 attributes[args.first.to_sym] # return the attribute-value elsif args[0][-1] == "=" if args.size == 2 # if rid.rid? # update set:{ args[0][0..-2] => args.last } # else self.attributes[ args[0][0..-2] ] = args.last # end else self.attributes[ args[0][0..-2] ] = args[1 .. -1] # update set: {args[0][0..-2] => args[1 .. -1] } if rid.rid? end else raise NameError, "Unknown method call #{args.first.to_s}", caller end end
# File lib/model/the_record.rb, line 25 def properties { "@type" => "d", "@class" => self.metadata[:class] }.merge attributes end
returns a OrientSupport::OrientQuery
# File lib/model/the_record.rb, line 53 def query **args OrientSupport::OrientQuery.new( **{ from: self}.merge(args)) end
# File lib/model/the_record.rb, line 237 def reload! transfer_content from: db.get_record(rid) self end
Obtain the RID of the Record (format: 00:00)
# File lib/model/the_record.rb, line 33 def rid begin "#{@metadata[:cluster]}:#{@metadata[:record]}" rescue "0:0" end end
The extended representation of RID (format: #00:00 )
# File lib/model/the_record.rb, line 43 def rrid "#" + rid end
Saves the record by calling update or creating the record
ORD.create_class :a a = A.new a.test = 'test' a.save a = A.first a.test = 'test' a.save
# File lib/model/the_record.rb, line 228 def save transfer_content from: if rid.rid? db.update self, attributes, version else db.create_record self, attributes: attributes, cache: false end ActiveOrient::Base.store_rid self end
# File lib/model/the_record.rb, line 48 def to_or rid.rid? ? rrid : "{ #{embedded} }" end
RECORD FUNCTIONS ###############
# File lib/model/the_record.rb, line 4 def to_s to_human end
protected
# File lib/model/the_record.rb, line 296 def transfer_content from: # »from« can be either # a model record (in case of create-record, get_record) or # a hash containing {"@type"=>"d", "@rid"=>"#xx:yy", "@version"=>n, "@class"=>'a_classname'} # and a list of updated properties (in case of db.update). Then update the version field and the # attributes. return nil if from.nil? if from.is_a? ActiveOrient::Model @metadata = from.metadata self.attributes = from.attributes else self.version = from['@version'] # throw away from["@..."] and convert keys to symbols, finally merge to attributes @attributes.merge! Hash[ from.delete_if{|k,_| k =~ /^@/}.map{|k,v| [k.to_sym, v.from_orient]}] end self # return the modified object end
Convenient update of the dataset
A) Using PATCH
Previously changed attributes are saved to the database.
Using the optional »:set:« argument ad-hoc attributes can be defined
V.create_class :contracts obj = Contracts.first obj.name = 'new_name' obj.update set: { yesterdays_event: 35 }
updates both, the »name« and the »yesterdays_event«-properties
B) Manual Modus
Update accepts a Block. The contents are parsed to »set«. Manual conversion of ruby-objects to the database-input format is necessary
i.e.
hct is an Array of ActiveOrient::Model-records.
then
obj.update { "positions = #{hct.to_or} " }
translates to
update #83:64 set positions = [#90:18, #91:18, #92:18] return after @this
and returns the modified record.
The manual modus accepts the keyword »remove«.
obj.update(remove: true) { "positions = #{hct.first.to_or} " }
translates to
update #83:64 remove positions = #90:18 return after @this
This can be achieved by
obj.positions
If the update process is not successful, nil is returned
# File lib/model/the_record.rb, line 171 def update set: {}, remove: {}, **args logger.progname = 'ActiveOrient::Model#Update' # query( kind: update, ) if block_given? # calling vs. a block is used internally # to remove an Item from lists and sets call update(remove: true){ query } set_or_remove = args[:remove].present? ? "remove" : "set" #transfer_content from: updated_record = db.execute{ "update #{rrid} #{ yield } return after $current" } &.first transfer_content from: updated_record if updated_record.present? else set = if remove.present? { remove: remove.merge!( args) } elsif set.present? set.merge!( args) else args end # set.merge updated_at: DateTime.now if rid.rid? q= query.kind(:update) if remove.present? q.remove(remove) else q.set(set) end transfer_content from: q.execute(reduce: true){ |y| y[:$current].reload! } else # new record self.attributes.merge! set save end end end