teton 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ae26f8067acdb56f89e1df1754b9e91e89ff3d96c6f6b30c3d46e3c0f7651f3
4
- data.tar.gz: d6858d2075ec88c423bb463406f8adeef82b51ab796f76da3aeb021c60993a93
3
+ metadata.gz: d0ae17f69664066dc41b866710d796b0191718219cd2b8d8ebd46e7ee513e4c4
4
+ data.tar.gz: fd29fdac420cd364cf8ef98101f8ac52e2c8a44d569e347c62691e756f226fa3
5
5
  SHA512:
6
- metadata.gz: 2d3fddc1585ea06a7323b7d146942db50c0cdfd224aa5dd6faa088b4735d0fc1b84ff3db9e7a90fb202f0b09b9033946bed31afabfabc995f9ac504321117f17
7
- data.tar.gz: 8d5e24cb57f502f37db40e90adcf439100c5c9547fe9296344c088b28cd0eb2b59c308a388e5c0cccff9530d42c170e3925dbadd26d066fb67082df6ddb5037e
6
+ metadata.gz: 5562f125e730627254377f95049b5bf636b05423322291fd4a9349075c6b084b0d125c4c9095469e8945a04779c49284fbf0d1937b583331606224acd47968f6
7
+ data.tar.gz: 055c6914b343d7288de36d63d3e8ca35b2a2fbe2694d852ce4763438518e90aa6850f8fce1926ad014c95f96a25eafcd823b99c545b8fbf9cc7eb0fce4be02e6
data/.rubocop.yml CHANGED
@@ -21,3 +21,6 @@ RSpec/ExampleLength:
21
21
 
22
22
  Metrics/ClassLength:
23
23
  Max: 150
24
+
25
+ Layout/LineLength:
26
+ Max: 125
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
- #### 0.0.1 - TBD
2
+ #### 0.0.2 - Monday, Sept. 5th, 2022
3
3
 
4
4
  * Initial proof-of-concept release.
data/lib/teton/key.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'key_pointer'
4
+
3
5
  module Teton
4
6
  # Understands a fully-qualified path to a resource or resources.
5
7
  class Key
@@ -22,8 +24,10 @@ module Teton
22
24
  (parts + suffix_keys).join(separator)
23
25
  end
24
26
 
25
- def traverse(&block)
26
- parts.each_with_index(&block)
27
+ def traverse
28
+ parts.each_with_index do |_part, index|
29
+ yield KeyPointer.new(self, index)
30
+ end
27
31
  end
28
32
 
29
33
  def entry?
@@ -33,5 +37,9 @@ module Teton
33
37
  def resource?
34
38
  parts.length.odd?
35
39
  end
40
+
41
+ def last_part
42
+ parts.last
43
+ end
36
44
  end
37
45
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Teton
4
+ # Points to a section within a key's parts. A section can either be a resource or an entry.
5
+ class KeyPointer
6
+ attr_reader :key, :index
7
+
8
+ def initialize(key, index)
9
+ @key = key
10
+ @index = index.to_i
11
+
12
+ freeze
13
+ end
14
+
15
+ def value
16
+ key.parts[index]
17
+ end
18
+
19
+ def not_last?
20
+ !last?
21
+ end
22
+
23
+ def last?
24
+ index == key.parts.length - 1
25
+ end
26
+
27
+ def entry?
28
+ index.odd?
29
+ end
30
+
31
+ def resource?
32
+ index.even? || index.zero?
33
+ end
34
+ end
35
+ end
@@ -20,80 +20,45 @@ module Teton
20
20
  # Main Object API
21
21
 
22
22
  def set(key, data)
23
- pointer = store
24
-
25
- key.traverse do |part, index|
26
- if index < key.parts.length - 1
27
- pointer = insert_traverse(index, pointer, part)
28
- else
29
- # last id
30
- upsert(pointer, part, data)
31
- end
32
- end
23
+ store_pointer = insert_traverse_to_last(key)
24
+
25
+ upsert(store_pointer, key.last_part, data)
33
26
 
34
27
  self
35
28
  end
36
29
 
37
30
  def get(key)
38
- pointer = store
39
-
40
- key.traverse do |part, index|
41
- break unless pointer
42
-
43
- pointer =
44
- if index < key.parts.length - 1
45
- # not last part
46
- traverse(index, pointer, part)
47
- elsif key.resource?
48
- # last part
49
- entries(key, pointer, part)
50
- else
51
- # id
52
- entry(key, pointer, part)
53
- end
54
- end
31
+ store_pointer = traverse_to_last(key)
55
32
 
56
- pointer
33
+ return unless store_pointer
34
+
35
+ if key.resource?
36
+ entries(key, store_pointer, key.last_part)
37
+ else
38
+ entry(key, store_pointer, key.last_part)
39
+ end
57
40
  end
58
41
 
59
42
  def del(key)
60
- pointer = store
61
-
62
- key.traverse do |part, index|
63
- break unless pointer
64
-
65
- if index < key.parts.length - 1
66
- # not last part
67
- pointer = traverse(index, pointer, part)
68
- else
69
- # last part
70
- pointer.delete(part)
71
- end
72
- end
43
+ store_pointer = traverse_to_last(key)
44
+
45
+ return self unless store_pointer
46
+
47
+ store_pointer.delete(key.last_part)
73
48
 
74
49
  self
75
50
  end
76
51
 
77
52
  def count(key)
78
- count = 0
79
- pointer = store
80
-
81
- key.traverse do |part, index|
82
- break unless pointer
83
-
84
- if index < key.parts.length - 1
85
- # not last part
86
- pointer = traverse(index, pointer, part)
87
- elsif key.resource?
88
- # last part
89
- count = (pointer.dig(part, IDS_KEY) || {}).keys.length
90
- else
91
- # id
92
- count = pointer.dig(part, DATA_KEY) ? 1 : 0
93
- end
94
- end
53
+ store_pointer = traverse_to_last(key)
54
+
55
+ return 0 unless store_pointer
95
56
 
96
- count
57
+ if key.resource?
58
+ (store_pointer.dig(key.last_part, IDS_KEY) || {}).keys.length
59
+ else
60
+ store_pointer.dig(key.last_part, DATA_KEY) ? 1 : 0
61
+ end
97
62
  end
98
63
 
99
64
  # Persistence API
@@ -157,6 +122,28 @@ module Teton
157
122
  end
158
123
  end
159
124
 
125
+ def insert_traverse_to_last(key)
126
+ store_pointer = store
127
+
128
+ key.traverse do |key_pointer|
129
+ store_pointer = insert_traverse(key_pointer.index, store_pointer, key_pointer.value) if key_pointer.not_last?
130
+ end
131
+
132
+ store_pointer
133
+ end
134
+
135
+ def traverse_to_last(key)
136
+ store_pointer = store
137
+
138
+ key.traverse do |key_pointer|
139
+ break unless store_pointer
140
+
141
+ store_pointer = traverse(key_pointer.index, store_pointer, key_pointer.value) if key_pointer.not_last?
142
+ end
143
+
144
+ store_pointer
145
+ end
146
+
160
147
  def entry(key, pointer, part)
161
148
  data = pointer.dig(part, DATA_KEY)
162
149
  meta = pointer.dig(part, META_KEY)
data/lib/teton/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Teton
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ruggio
@@ -182,6 +182,7 @@ files:
182
182
  - lib/teton/db.rb
183
183
  - lib/teton/entry.rb
184
184
  - lib/teton/key.rb
185
+ - lib/teton/key_pointer.rb
185
186
  - lib/teton/stores/memory.rb
186
187
  - lib/teton/version.rb
187
188
  - teton.gemspec