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

#as, #compose_where, #generate_sql_list, #where, #while_s

Methods inherited from Hash

#from_orient, #to_human, #to_or, #to_orient

Constructor Details

#initialize(modelinstance, args) ⇒ Hash



221
222
223
224
225
226
227
# File 'lib/support/orient.rb', line 221

def initialize modelinstance, args
  super()
  @orient = modelinstance
  self.merge! args
  @name =  block_given? ? yield : modelinstance.attributes.key(self)
  self
end

Instance Method Details

#[](arg) ⇒ Object



285
286
287
# File 'lib/support/orient.rb', line 285

def [] arg
  super
end

#delete_if(&b) ⇒ Object



269
270
271
272
273
# File 'lib/support/orient.rb', line 269

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

end

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

Keys are translated to symbols



241
242
243
244
# File 'lib/support/orient.rb', line 241

def merge **arg
  @orient.update @name => super(**arg)
  @orient.reload!
end

#remove(*k) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/support/orient.rb', line 261

def remove *k
  # todo combine queries in a transaction

  r=  k.map{|key|  @orient.update{   "remove #{@name} = #{key.to_s.to_or} "  } }
  @orient.reload!.send @name

end

#slice(arg) ⇒ Object

excepts a regular expression as well



278
279
280
281
282
283
284
# File 'lib/support/orient.rb', line 278

def slice arg
  if arg.is_a? Regexp
    find_all{ |key| key.to_s.match(arg) }.to_h
  else
     super arg.to_sym
  end
end

#store(k, v) ⇒ Object Also known as: []=



229
230
231
232
# File 'lib/support/orient.rb', line 229

def store  k, v
  @orient.update { "set #{@name}[#{k.to_s.to_or}] = #{v.to_or} "}[@name] #if check_if_complete
  @orient.reload!
end