lib/gap50/cache/hashchain.rb in gap50-0.1.0 vs lib/gap50/cache/hashchain.rb in gap50-0.1.1
- old
+ new
@@ -1,52 +1,105 @@
module Gap
class HashChain
HashNode = Struct.new :hash, :next
+ USAGE = {}
+ module Package
+ PACKAGES = {}
+ def self.register(a, b)
+ if PACKAGES.include?(a) && PACKAGES[a] != b
+ raise ArgumentError, "Already exists #{a}"
+ else
+ PACKAGES[a] = b
+ end
+ end
+ def self.unregister(a)
+ PACKAGES.delete(a)
+ end
+
+ def self.[](a)
+ if !PACKAGES.include?(a)
+ raise ArgumentError, "Not exists #{a}"
+ else
+ PACKAGES[a]
+ end
+ end
+
+ def self.call(hnode)
+ self[hnode.getone(:package)]
+ end
+ end
+ USAGE[:package] = Package
class HashNode
+ attr_accessor :usagetag
def initialize(hash = {}, nx = nil)
super(hash, nx)
end
- def [](a)
+ def usage
+ if self.usagetag
+ USAGE[self.usagetag]
+ end
+ end
+
+ def getone(a)
+ self.hash[a]
+ end
+
+ def get(a)
if self.hash.include?(a)
self.hash[a]
elsif self.next
self.next[a]
else
nil
end
end
- def []=(a, b)
+ def set(a, b)
self.hash[a] = b
end
+ def [](a)
+ return usage.call(self)[a] if usage
+ get a
+ end
+
+
+ def []=(a, b)
+ return (usage.call(self)[a] = b) if usage
+ set a, b
+ end
+
def include?(a)
+ return (usage.call(self).include?(a)) if usage
if self.hash.include?(a)
true
elsif self.next
self.next.include?(a)
else
false
end
end
def delete(a)
+ return (usage.call(self).delete(a)) if usage
self.hash.delete a
end
def deleteAll(a)
+ return (usage.call(self).deleteAll(a)) if usage
self.hash.delete a
self.next.deleteAll a if self.next
end
end
def initialize(hash = {})
@hashnode = HashNode.new hash
end
- def append(node = HashNode.new)
+ def append(node = HashNode.new, usage = nil)
+ node.usagetag = usage if usage
node.next = @hashnode
@hashnode = node
end
def [](a)
@@ -55,9 +108,21 @@
def []=(a, b)
@hashnode[a] = b
end
+ def get(a)
+ @hashnode.get a
+ end
+
+ def getone(a)
+ @hashnode.getone a
+ end
+
+ def set(a, b)
+ @hashnode.set a, b
+ end
+
def include?(a)
@hashnode.include?(a)
end
def delete(a)
\ No newline at end of file