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.

Parameters:

  • key_metadata (KeyMetadataStore)

    a reference to a metadata store

  • parent (LazyLazer)

    a reference to a LazyLazer 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

Parameters:

  • key_name (Symbol)

    the name of the key



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.

Parameters:

  • key_name (Symbol)

    the key to check

Returns:

  • (Boolean)

    whether the key exists locally.



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

Returns whether the object is done with lazy loading.

Returns:

  • (Boolean)

    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

#inspectString

Returns the string representation of the parent.

Returns:

  • (String)

    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.

Parameters:

  • attributes (Hash<Symbol, Object>)

    the attributes to merge



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)

Parameters:

  • key_name (Symbol)

    the name of the key

Returns:

  • (Object)

    the returned value

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.

Returns:

  • (Array)

    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.

Returns:

  • (Hash)

    the converted 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.

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!
  @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.

Parameters:

  • key_name (Symbol)

    the attribute to update

  • new_value (Object)

    the new value

Returns:

  • (Object)

    the written value



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