Class: LazyLazer::InternalModel
- Inherits:
-
Object
- Object
- LazyLazer::InternalModel
- Defined in:
- lib/lazy_lazer/internal_model.rb
Overview
A delegator for internal operations.
Instance Method Summary collapse
-
#delete_attribute(key_name) ⇒ void
Delete an attribute.
-
#exists_locally?(key_name) ⇒ Boolean
Whether the key doesn’t need to be lazily loaded.
-
#fully_loaded! ⇒ void
Mark the model as fully loaded.
-
#fully_loaded? ⇒ Boolean
Whether the object is done with lazy loading.
-
#initialize(key_metadata, parent) ⇒ InternalModel
constructor
Create an internal model with a reference to a public model.
-
#inspect ⇒ String
The string representation of the parent.
-
#merge!(attributes) ⇒ Object
private
Merge a hash into the model.
-
#not_fully_loaded! ⇒ void
Mark the model as not fully loaded.
-
#read_attribute(key_name) ⇒ Object
Get the value of a key (fetching it from the cache if possible).
-
#required_properties ⇒ Array
private
The identity properties.
-
#to_h(strict: false) ⇒ Hash
Converts all unconverted keys and packages them as a hash.
-
#verify_required! ⇒ void
private
Verify that all the keys marked as required are present.
-
#write_attribute(key_name, new_value) ⇒ Object
Update an attribute.
Constructor Details
#initialize(key_metadata, parent) ⇒ InternalModel
Create an internal model with a reference to a public model.
11 12 13 14 15 16 17 18 |
# File 'lib/lazy_lazer/internal_model.rb', line 11 def initialize(, parent) @key_metadata = @parent = parent @cache = {} @source = {} @writethrough = {} @fully_loaded = false end |
Instance Method Details
#delete_attribute(key_name) ⇒ void
This method returns an undefined value.
Delete an attribute
66 67 68 69 70 71 72 73 |
# File 'lib/lazy_lazer/internal_model.rb', line 66 def delete_attribute(key_name) key_name_sym = key_name.to_sym = (key_name_sym) @cache.delete(key_name) @writethrough.delete(key_name) @source.delete(.source_key) nil end |
#exists_locally?(key_name) ⇒ Boolean
Whether the key doesn’t need to be lazily loaded.
39 40 41 42 43 |
# File 'lib/lazy_lazer/internal_model.rb', line 39 def exists_locally?(key_name) return true if @cache.key?(key_name) || @writethrough.key?(key_name) = (key_name) @source.key?(.source_key) end |
#fully_loaded! ⇒ void
This method returns an undefined value.
Mark the model as fully loaded.
77 78 79 |
# File 'lib/lazy_lazer/internal_model.rb', line 77 def fully_loaded! @fully_loaded = true end |
#fully_loaded? ⇒ Boolean
Returns whether the object is done with lazy loading.
88 89 90 |
# File 'lib/lazy_lazer/internal_model.rb', line 88 def fully_loaded? @fully_loaded end |
#inspect ⇒ String
Returns the string representation of the parent.
31 32 33 34 |
# File 'lib/lazy_lazer/internal_model.rb', line 31 def inspect "#<#{@parent.class.name} (#{@fully_loaded ? 'loaded' : 'unloaded'}): [" + \ @cache.keys.join(', ') + ']>' end |
#merge!(attributes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Merge a hash into the model.
112 113 114 115 |
# File 'lib/lazy_lazer/internal_model.rb', line 112 def merge!(attributes) @cache.clear @source.merge!(attributes) end |
#not_fully_loaded! ⇒ void
This method returns an undefined value.
Mark the model as not fully loaded.
83 84 85 |
# File 'lib/lazy_lazer/internal_model.rb', line 83 def not_fully_loaded! @fully_loaded = false end |
#read_attribute(key_name) ⇒ Object
Get the value of a key (fetching it from the cache if possible)
49 50 51 |
# File 'lib/lazy_lazer/internal_model.rb', line 49 def read_attribute(key_name) @cache.fetch(key_name) { load_key_strict(key_name) } end |
#required_properties ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the identity properties.
105 106 107 |
# File 'lib/lazy_lazer/internal_model.rb', line 105 def required_properties @key_metadata.required_properties end |
#to_h(strict: false) ⇒ Hash
Converts all unconverted keys and packages them as a hash.
22 23 24 25 26 27 28 |
# File 'lib/lazy_lazer/internal_model.rb', line 22 def to_h(strict: false) todo = @key_metadata.keys - @cache.keys todo.each do |key| strict ? load_key_strict(key) : load_key_lenient(key) end @cache.dup end |
#verify_required! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Verify that all the keys marked as required are present.
96 97 98 99 100 101 |
# File 'lib/lazy_lazer/internal_model.rb', line 96 def verify_required! @key_metadata.required_properties.each do |key_name| next if @source.key?(@key_metadata.get(key_name).source_key) raise RequiredAttribute, "#{@parent} requires `#{key_name}`" end end |
#write_attribute(key_name, new_value) ⇒ Object
Update an attribute.
57 58 59 60 61 |
# File 'lib/lazy_lazer/internal_model.rb', line 57 def write_attribute(key_name, new_value) = (key_name) @source.delete(.source_key) @writethrough[key_name] = @cache[key_name] = new_value end |