Module: ModelRecord
- Included in:
- ActiveOrient::Model
- Defined in:
- lib/model/the_record.rb
Class Method Summary collapse
-
.classname ⇒ Object
Returns just the name of the Class.
Instance Method Summary collapse
-
#find(attributes = {}) ⇒ Object
Fires a »where-Query» to the database starting with the current model-record.
-
#from_orient ⇒ Object
GET #############.
-
#has_property?(property) ⇒ Boolean
flag whether a property exists on the Record-level.
-
#increment_version ⇒ Object
:nodoc:.
-
#is_edge? ⇒ Boolean
An Edge is defined * when inherented from the superclass »E» (formal definition) * if it has an in- and an out property.
-
#method_missing(*args) ⇒ Object
How to handle other calls.
- #properties ⇒ Object
-
#query(query) ⇒ Object
Query uses the current model-record as origin of the query.
- #reload! ⇒ Object
-
#remove ⇒ Object
(also: #delete)
Removes the Model-Instance from the database.
-
#rid ⇒ Object
Obtain the RID of the Record (format: 00:00).
-
#rrid ⇒ Object
(also: #to_orient)
The extended representation of RID (format: #00:00 ).
-
#save ⇒ Object
Saves the record by calling update or creating the record.
- #to_or ⇒ Object
- #transfer_content(from:) ⇒ Object
-
#update(set: {}, add: nil, to: nil, **args) ⇒ Object
Convient update of the dataset by calling sql-patch.
-
#update_attribute(the_attribute, the_value) ⇒ Object
mocking active record.
-
#update_attributes(**args) ⇒ Object
:nodoc:.
-
#version ⇒ Object
Get the version of the object.
-
#version=(version) ⇒ Object
:nodoc:.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
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
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/model/the_record.rb', line 243 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 |
Class Method Details
.classname ⇒ Object
Returns just the name of the Class
12 13 14 |
# File 'lib/model/the_record.rb', line 12 def self.classname # :nodoc: self.class.to_s.split(':')[-1] end |
Instance Method Details
#find(attributes = {}) ⇒ Object
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.
82 83 84 85 |
# File 'lib/model/the_record.rb', line 82 def find attributes = {} q = OrientSupport::OrientQuery.new from: self, where: attributes query q end |
#from_orient ⇒ Object
GET #############
6 7 8 |
# File 'lib/model/the_record.rb', line 6 def from_orient # :nodoc: self end |
#has_property?(property) ⇒ Boolean
flag whether a property exists on the Record-level
18 19 20 |
# File 'lib/model/the_record.rb', line 18 def has_property? property attributes.keys.include? property.to_s end |
#increment_version ⇒ Object
:nodoc:
100 101 102 |
# File 'lib/model/the_record.rb', line 100 def increment_version # :nodoc: @metadata[:version] += 1 end |
#is_edge? ⇒ Boolean
An Edge is defined
* when inherented from the superclass »E» (formal definition)
* if it has an in- and an out property
Actually we just check the second term as we trust the constuctor to work properly
224 225 226 |
# File 'lib/model/the_record.rb', line 224 def is_edge? # :nodoc: attributes.keys.include?('in') && attributes.keys.include?('out') end |
#properties ⇒ Object
22 23 24 |
# File 'lib/model/the_record.rb', line 22 def properties { "@type" => "d", "@class" => self.[:class] }.merge attributes end |
#query(query) ⇒ Object
Query uses the current model-record as origin of the query.
It sends the OrientSupport::OrientQuery directly to the database and returns an ActiveOrient::Model-Object or an Array of Model-Objects as result.
Usage: Query the Database by traversing through edges and vertices starting at a known location
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/model/the_record.rb', line 58 def query query query.from = rrid if query.is_a? OrientSupport::OrientQuery result = orientdb.execute do query.to_s end if result.is_a? Array OrientSupport::Array.new work_on: self, work_with: result else result end # return value end |
#reload! ⇒ Object
193 194 195 196 |
# File 'lib/model/the_record.rb', line 193 def reload! transfer_content from: db.get_record(rid) self end |
#remove ⇒ Object Also known as: delete
Removes the Model-Instance from the database.
It is overloaded in Vertex and Edge.
111 112 113 114 |
# File 'lib/model/the_record.rb', line 111 def remove orientdb.delete_record self ActiveOrient::Base.remove_rid self ##if is_edge? # removes the obj from the rid_store end |
#rid ⇒ Object
Obtain the RID of the Record (format: 00:00)
30 31 32 33 34 35 36 |
# File 'lib/model/the_record.rb', line 30 def rid begin "#{@metadata[:cluster]}:#{@metadata[:record]}" rescue "0:0" end end |
#rrid ⇒ Object Also known as: to_orient
The extended representation of RID (format: #00:00 )
40 41 42 |
# File 'lib/model/the_record.rb', line 40 def rrid "#" + rid end |
#save ⇒ Object
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
184 185 186 187 188 189 190 191 |
# File 'lib/model/the_record.rb', line 184 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 |
#to_or ⇒ Object
45 46 47 |
# File 'lib/model/the_record.rb', line 45 def to_or rid.rid? ? rrid : "{ #{} }" end |
#transfer_content(from:) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/model/the_record.rb', line 199 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. if from.is_a? ActiveOrient::Model @metadata = from. @attributes = from.attributes else self.version = from['@version'] # throw from["@..."] away and convert keys to symbols, merge that into attributes @attributes.merge! Hash[ from.delete_if{|k,_| k =~ /^@/}.map{|k,v| [k.to_sym, v.from_orient]}] end end |
#update(set: {}, add: nil, to: nil, **args) ⇒ Object
Convient update of the dataset by calling sql-patch
Previously changed attributes are saved to the database.
Using the optional :set argument ad-hoc attributes can be defined
obj = ActiveOrient::Model::Contracts.first
obj.name = 'new_name'
obj.update set: { yesterdays_event: 35 }
updates both, the »name« and the »yesterdays_event«-properties
note: The keyword »set« is optional, thus
obj.update yesterdays_event: 35
is identical
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/model/the_record.rb', line 137 def update set:{}, add: nil, to: nil, **args logger.progname = 'ActiveOrient::Model#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: query( "update #{rrid} #{set_or_remove} #{ yield } return after @this" )&.first else set.merge! args # set.merge updated_at: DateTime.now if rid.rid? transfer_content from: db.update( self, set, version ) # if the updated dataset changed, drop the changes made siently # self # return value else # new record @attributes.merge! set save end end end |
#update_attribute(the_attribute, the_value) ⇒ Object
mocking active record
161 162 163 |
# File 'lib/model/the_record.rb', line 161 def update_attribute the_attribute, the_value # :nodoc: update { " #{the_attribute} = #{the_value.to_or} " } end |
#update_attributes(**args) ⇒ Object
:nodoc:
165 166 167 |
# File 'lib/model/the_record.rb', line 165 def update_attributes **args # :nodoc: update args end |
#version ⇒ Object
Get the version of the object
88 89 90 91 92 93 94 |
# File 'lib/model/the_record.rb', line 88 def version # :nodoc: if document.present? document.version else @metadata[:version] end end |
#version=(version) ⇒ Object
:nodoc:
96 97 98 |
# File 'lib/model/the_record.rb', line 96 def version= version # :nodoc: @metadata[:version] = version end |