lazy_lazer 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22872353f1cee531497c41b3d54db9f03b9ab9ee
4
- data.tar.gz: 4fc8d26919472b31c3257798af077d5d2b5fc02b
3
+ metadata.gz: 1ad5be6027b7147b22c96236407c5700e29cc7fe
4
+ data.tar.gz: 742cdb3547d6ede2afe121fd14f9e113af4d8de1
5
5
  SHA512:
6
- metadata.gz: 30fc0cb9bb106a2c6cc6f5dc959d920584bfbbf2dff95e5f5a5358fbe4542b91a5e4012b2b3446531f5662e2aa180b5722971e15030ff3e756d50914df2403f1
7
- data.tar.gz: f39411ddcc2fb04c16149607586736b236f135e78521e8077f9b544d1b9a049fcc4c61cf1a69031e0431db4c9d3f69176e4ac2ed34d7adaf0953f7a8e4063fbd
6
+ metadata.gz: d4ca6f7e61971cc4f12bdd5d7d9b9b30555d0f1b84777261b1bdafa65bcfed0c18b8b26cbdf6d29b78b2edc7c6272d38dff28fd52aba5405cae0c1b482232088
7
+ data.tar.gz: 2a89cd5569b7f43292810b9fbbdbf75bf5d1b33ee339a578c56d89058a5aa7b1cddb8575324602214525c37a0eb7641ad09707b4e98e96440d916e7275ed1a38
@@ -3,14 +3,19 @@
3
3
  module LazyLazer
4
4
  # A delegator for internal operations.
5
5
  class InternalModel
6
+ # @return [Boolean] whether the model is fully loaded
7
+ attr_accessor :fully_loaded
8
+
6
9
  # Create an internal model with a reference to a public model.
7
10
  # @param key_metadata [KeyMetadataStore] a reference to a metadata store
8
11
  # @param parent [LazyLazer] a reference to a LazyLazer model
9
12
  def initialize(key_metadata, parent)
10
13
  @key_metadata = key_metadata
11
14
  @parent = parent
15
+ @invalidated = Set.new
12
16
  @source_hash = {}
13
17
  @cache_hash = {}
18
+ @fully_loaded = false
14
19
  end
15
20
 
16
21
  # Verify that all the keys marked as required are present.
@@ -35,26 +40,47 @@ module LazyLazer
35
40
  todo.each_with_object(@cache_hash) { |key, cache| cache[key] = load_key_from_source(key) }.dup
36
41
  end
37
42
 
38
- # @return [Array<Symbol>] the locally processed and cached keys
39
- def cached_keys
40
- @cache_hash.keys
43
+ # @return [String] the string representation of the parent
44
+ def parent_inspect
45
+ "#<#{@parent.class.name} (#{@fully_loaded ? 'loaded' : 'unloaded'}): [" + \
46
+ @cache_hash.keys.join(', ') + ']>'
47
+ end
48
+
49
+ # Mark a key as tainted, forcing a reload on the next lookup.
50
+ # @param key_name [Symbol] the key to invalidate
51
+ # @return [void]
52
+ def invalidate(key_name)
53
+ @invalidated.add(key_name)
41
54
  end
42
55
 
43
56
  # Get the value of a key (fetching it from the cache if possible)
44
57
  # @param key_name [Symbol] the name of the key
45
58
  # @return [Object] the returned value
46
59
  # @raise MissingAttribute if the attribute wasn't found and there isn't a default
47
- def fetch(key_name)
60
+ def read_attribute(key_name)
61
+ if @invalidated.include?(key_name)
62
+ @parent.reload
63
+ @invalidated.delete(key_name)
64
+ end
48
65
  @cache_hash[key_name] ||= load_key_from_source(key_name)
49
66
  end
50
67
 
68
+ # Update an attribute.
69
+ # @param key_name [Symbol] the attribute to update
70
+ # @param new_value [Object] the new value
71
+ # @return [Object] the written value
72
+ def write_attribute(key_name, new_value)
73
+ unless @key_metadata.contains?(key_name)
74
+ raise ArgumentError, "#{key_name} is not a valid attribute for #{parent}"
75
+ end
76
+ @cache_hash[key_name] = new_value
77
+ end
78
+
51
79
  # Merge a hash into the model.
52
80
  # @param attributes [Hash<Symbol, Object>] the attributes to merge
53
- # @return [nil]
54
81
  def merge!(attributes)
55
82
  @cache_hash.clear
56
83
  @source_hash.merge!(attributes)
57
- nil
58
84
  end
59
85
 
60
86
  private
@@ -85,7 +111,7 @@ module LazyLazer
85
111
  # @return [void]
86
112
  # @raise MissingAttribute if runtime_required is true and the key can't be loaded.
87
113
  def ensure_key_is_loaded(source_key, runtime_required)
88
- @parent.reload if !@source_hash.key?(source_key) && !@parent.fully_loaded?
114
+ @parent.reload if !@source_hash.key?(source_key) && !@fully_loaded
89
115
  return if @source_hash.key?(source_key) || !runtime_required
90
116
  raise MissingAttribute, "`#{source_key} is missing for #{@parent}`"
91
117
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module LazyLazer
4
4
  # The gem's semantic version number.
5
- VERSION = '0.5.4'
5
+ VERSION = '0.6.0'
6
6
  end
data/lib/lazy_lazer.rb CHANGED
@@ -68,8 +68,6 @@ module LazyLazer
68
68
  @_lazer_model = InternalModel.new(self.class.instance_variable_get(:@_lazer_metadata), self)
69
69
  @_lazer_model.merge!(attributes)
70
70
  @_lazer_model.verify_required!
71
- @_lazer_writethrough = {}
72
- @_lazer_loaded = false
73
71
  end
74
72
 
75
73
  # Equality check, performed using required keys.
@@ -92,8 +90,7 @@ module LazyLazer
92
90
 
93
91
  # @return [String] a human-friendly view of the model
94
92
  def inspect
95
- "#<#{self.class.name} (#{fully_loaded? ? 'loaded' : 'unloaded'}): [" + \
96
- (@_lazer_model.cached_keys + @_lazer_writethrough.keys).join(', ') + ']>'
93
+ @_lazer_model.parent_inspect
97
94
  end
98
95
 
99
96
  # Reload the object. Calls {#lazer_reload}, then merges the results into the internal store.
@@ -110,9 +107,7 @@ module LazyLazer
110
107
  # @return [Object] the returned value
111
108
  # @raise MissingAttribute if the key was not found
112
109
  def read_attribute(key_name)
113
- symbol_key = key_name.to_sym
114
- return @_lazer_writethrough[symbol_key] if @_lazer_writethrough.key?(symbol_key)
115
- @_lazer_model.fetch(symbol_key)
110
+ @_lazer_model.read_attribute(key_name.to_sym)
116
111
  end
117
112
 
118
113
  # Return the value of the attribute, returning nil if not found.
@@ -126,10 +121,10 @@ module LazyLazer
126
121
 
127
122
  # Update an attribute.
128
123
  # @param key_name [Symbol] the attribute to update
129
- # @param value [Object] the new value
124
+ # @param new_value [Object] the new value
130
125
  # @return [Object] the written value
131
- def write_attribute(key_name, value)
132
- @_lazer_writethrough[key_name] = value
126
+ def write_attribute(key_name, new_value)
127
+ @_lazer_model.write_attribute(key_name, new_value)
133
128
  end
134
129
 
135
130
  # Update multiple attributes at once.
@@ -143,7 +138,7 @@ module LazyLazer
143
138
 
144
139
  # @return [Boolean] whether the object is done with lazy loading
145
140
  def fully_loaded?
146
- @_lazer_loaded
141
+ @_lazer_model.fully_loaded
147
142
  end
148
143
 
149
144
  private
@@ -155,16 +150,23 @@ module LazyLazer
155
150
  {}
156
151
  end
157
152
 
153
+ # Mark a key as tainted, forcing a reload on the next lookup.
154
+ # @param key_name [Symbol] the key to invalidate
155
+ # @return [void]
156
+ def invalidate(key_name)
157
+ @_lazer_model.invalidate(key_name)
158
+ end
159
+
158
160
  # Mark the model as fully loaded.
159
161
  # @return [void]
160
162
  def fully_loaded!
161
- @_lazer_loaded = true
163
+ @_lazer_model.fully_loaded = true
162
164
  end
163
165
 
164
166
  # Mark the model as not fully loaded.
165
167
  # @return [void]
166
168
  def not_fully_loaded!
167
- @_lazer_loaded = false
169
+ @_lazer_model.fully_loaded = false
168
170
  end
169
171
  end
170
172
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_lazer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avinash Dwarapu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-30 00:00:00.000000000 Z
11
+ date: 2017-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler