Class: Triad

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/triad.rb,
lib/triad/version.rb

Defined Under Namespace

Classes: InvalidAddition, ItemNotPresent

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Constructor Details

#initializeTriad

stored as => [‘Descriptor’, value]



11
12
13
# File 'lib/triad.rb', line 11

def initialize
  @storage = Concurrent::Hash.new
end

Instance Method Details

#<<(array) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/triad.rb', line 41

def <<(array)
  array_key = array[0]
  raise InvalidAddition.new("your array length must be 3") if array.length != 3
  raise InvalidAddition.new("the provided key already exists") if key_exists?(array_key)

  array_descriptor = array[1]
  array_value =      array[2]

  storage[array_key] = [array_descriptor, array_value]
  self
end

#descriptors(arg = :__no_argument_given__) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/triad.rb', line 25

def descriptors(arg=:__no_argument_given__)
  if arg == :__no_argument_given__
    storage.map{|_,(descriptor,_)| descriptor }
  else
    with_interest(arg).map{|_, descriptor, _| descriptor }
  end
end

#each(&block) ⇒ Object



57
58
59
60
61
# File 'lib/triad.rb', line 57

def each(&block)
  storage.each do |key, (descriptor, value)|
    block.call key, descriptor, value
  end
end

#keys(arg = :__no_argument_given__) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/triad.rb', line 17

def keys(arg=:__no_argument_given__)
  if arg == :__no_argument_given__
    storage.keys
  else
    with_interest(arg).map{|key, _, _| key }
  end
end

#update(key, descriptor, value) ⇒ Object



53
54
55
# File 'lib/triad.rb', line 53

def update(key, descriptor, value)
  storage[key] = [descriptor, value]
end

#values(arg = :__no_argument_given__) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/triad.rb', line 33

def values(arg=:__no_argument_given__)
  if arg == :__no_argument_given__
    storage.map{|_,(_,value)| value }.uniq
  else
    with_interest(arg).map{|_, _, value| value }
  end
end