Class: OrientSupport::Hash

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

Overview

WithIndifferentAccess

Instance Method Summary collapse

Methods included from Support

#compose_where, #generate_sql_list

Methods inherited from Hash

#coerce, #from_orient, #to_human, #to_or, #to_orient

Constructor Details

#initialize(modelinstance, args) ⇒ Hash

Returns a new instance of Hash.


129
130
131
132
133
134
135
136
137
138
# File 'lib/support/orient.rb', line 129

def initialize modelinstance, args
	super()
#	puts "Hash.new args #{args}"
	@orient = modelinstance
	self.merge! args
	@name = modelinstance.attributes.key(self)
	@name =  yield if @name.nil? && block_given?
#	puts "@name #{@name}"
	self
end

Instance Method Details

#[]=(k, v) ⇒ Object


141
142
143
# File 'lib/support/orient.rb', line 141

def []=  k, v
	 @orient.update { "#{@name.to_s}.#{k.to_s} = #{v.to_or}" }
end

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

Inserts the provided Hash to the (possibly empty) list-property and returns a hash


146
147
148
149
150
151
# File 'lib/support/orient.rb', line 146

def append arg
	# the argument is simply provided as JSON-parameter to »update«
	# generated query: update {rrid} set { @name } = { arg.to_json } return after @this
	# todo : consider returning a OrientSuport::Hash
	@orient.update { "#{@name.to_s} = "+ arg.to_json }[@name]
end

#delete_if(&b) ⇒ Object

key.each do | k | o = OrientSupport::OrientQuery.new from: @orient, kind: 'update', set: “#@name.#OrientSupport::Hash.kk.to_s”, return: “$current.#@name” @orient.db.execute{ o.to_s.gsub( 'set ', 'remove ' ) }.first.send( @name ) # extracts the modified array (from DB) from the result end @orient.reload! @orient.send @name # return value end


175
176
177
178
179
# File 'lib/support/orient.rb', line 175

def delete_if &b
	super &b
	@orient.update set:{ @name => self}

end

#remove(*k) ⇒ Object

removes a key-value entry from the hash.

parameter: list of key's (duplicate values are removed)


158
159
160
161
# File 'lib/support/orient.rb', line 158

def remove *k
	# todo combine queries in a transaction
	k.map{ |key| @orient.update( remove: true ) { "#{@name.to_s}.#{key} " } }.last
end