Class: LazyLazer::InternalModel

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_lazer/internal_model.rb

Overview

A delegator for internal operations.

Instance Method Summary collapse

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)
   = 
  @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
  meta = (key_name_sym)
  @cache.delete(key_name)
  @writethrough.delete(key_name)
  @source.delete(meta.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)
  meta = (key_name)
  @source.key?(meta.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



88
89
90
# File 'lib/lazy_lazer/internal_model.rb', line 88

def fully_loaded?
  @fully_loaded
end

#inspectString



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)

Raises:

  • MissingAttribute if the attribute wasn’t found and there isn’t a default



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_propertiesArray

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
  .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 = .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.

Raises:

  • RequiredAttribute if a required attribute is missing



96
97
98
99
100
101
# File 'lib/lazy_lazer/internal_model.rb', line 96

def verify_required!
  .required_properties.each do |key_name|
    next if @source.key?(.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)
  meta = (key_name)
  @source.delete(meta.source_key)
  @writethrough[key_name] = @cache[key_name] = new_value
end