Class: Audrey::Node::Hash

Inherits:
Collection show all
Defined in:
lib/audrey.rb

Overview

Audrey::Node::Hash

Instance Attribute Summary

Attributes inherited from Audrey::Node

#db, #pk

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

#any?, #changed, #changed=, #child_pks, #clear, create_object, #length

Methods inherited from Audrey::Node

#alive?, #ancestors, #delete_object, #fco, #initialize, #isolate, #row

Constructor Details

This class inherits a constructor from Audrey::Node

Class Method Details

.new_objectObject


new_object



903
904
905
# File 'lib/audrey.rb', line 903

def self.new_object
  return({})
end

Instance Method Details

#[](key) ⇒ Object




914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'lib/audrey.rb', line 914

def [](key)
  # $tm.hrm
  # puts "@pk: #{@pk}"
  # puts "key: #{key}"
  
  read_check()
  hsh = @engine.row_hash_by_key(@pk, key)
  
  # if we got a hash
  if hsh
    return @db.object_from_row(hsh)
    
  # else return nil
  else
    return nil
  end
end

#[]=(key, child) ⇒ Object


[]=



939
940
941
942
943
944
945
946
947
948
# File 'lib/audrey.rb', line 939

def []=(key, child)
  write_check()
  
  # test for checks outside of a transaction
  @db.checks_outside_transaction @aclass
  
  @engine.hash_element_delete @pk, key
  save_child child, 'key'=>key.to_s
  return child
end

#attach_to(hsh) ⇒ Object


attach_to



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'lib/audrey.rb', line 1047

def attach_to(hsh)
  # hold on to original values
  orgs = hsh.clone
  
  # audrey()
  def hsh.audrey(p_node = nil)
    if p_node
      @audrey_node = p_node
    end
    
    # return
    return @audrey_node
  end
  
  # []
  def hsh.[](key)
    return @audrey_node[key]
  end
  
  # []=
  def hsh.[]=(key, child)
    return @audrey_node[key] = child
  end
  
  # each
  def hsh.each()
    if block_given?
      @audrey_node.each do |k, v|
        yield k, v
      end
    else
      return @audrey_node.each
    end
  end
  
  # length
  def hsh.length()
    return @audrey_node.length
  end
  
  # keys
  def hsh.keys()
    return @audrey_node.keys
  end
  
  # values
  def hsh.values()
    return @audrey_node.values
  end
  
  # delete
  def hsh.delete(*opts)
    return @audrey_node.delete(*opts)
  end
  
  # clear
  def hsh.clear()
    return @audrey_node.clear()
  end
  
  # has_key?
  def hsh.has_key?(key)
    return @audrey_node.has_key?(key)
  end
  
  # any?
  def hsh.any?()
    return @audrey_node.any?
  end
  
  # ==
  def hsh.==(*opts)
    return @audrey_node.==(*opts)
  end
  
  # <=
  def hsh.<=(*opts)
    return @audrey_node.<=(*opts)
  end
  
  # >=
  def hsh.>=(*opts)
    return @audrey_node.>=(*opts)
  end
  
  # ===
  def hsh.===(*opts)
    return @audrey_node.===(*opts)
  end
  
  # initial call to audrey object
  hsh.audrey self
  
  # save child elements
  orgs.each do |k, v|
    hsh[k] = v
  end
end

#delete(key) ⇒ Object


delete



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/audrey.rb', line 1016

def delete(key)
  write_check()
  record = @engine.hash_element_delete(@pk, key)
  
  # if record, return node, else return nil
  if record
    return @db.object_from_row(record)
  else
    return nil
  end
end

#eachObject


each



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/audrey.rb', line 989

def each
  read_check()
  rv = nil
  
  # loop through records
  @engine.hash_each(@pk) do |key, row|
    object = @db.object_from_row(row)
    
    if block_given?
      yield key, object
    else
      rv ||= []
      rv.push [key, object]
    end
  end
  
  # return
  return rv
end

#has_key?(key) ⇒ Boolean


has_key?



1035
1036
1037
1038
# File 'lib/audrey.rb', line 1035

def has_key?(key)
  read_check()
  return @engine.hash_has_key?(@pk, key)
end

#keysObject


keys



957
958
959
960
# File 'lib/audrey.rb', line 957

def keys
  read_check()
  return @engine.hash_keys(@pk)
end

#valuesObject


values



969
970
971
972
973
974
975
976
977
978
979
980
# File 'lib/audrey.rb', line 969

def values
  read_check()
  rv = []
  
  # loop through records
  @engine.hash_each(@pk) do |key, row|
    rv.push @db.object_from_row(row)
  end
  
  # return
  return rv
end