Module: LazyLazer::InstanceMethods

Defined in:
lib/lazy_lazer.rb

Overview

The methods to extend the instance with.

Instance Method Summary collapse

Instance Method Details

#[](key_name) ⇒ Object

Return the value of the attribute, returning nil if not found.

Parameters:

  • key_name (Symbol)

    the attribute name

Returns:

  • (Object)

    the returned value



114
115
116
117
118
# File 'lib/lazy_lazer.rb', line 114

def [](key_name)
  read_attribute(key_name)
rescue MissingAttribute
  nil
end

#assign_attributes(new_attributes) ⇒ self Also known as: attributes=

Update multiple attributes at once.

Parameters:

  • new_attributes (Hash<Symbol, Object>)

    the new attributes

Returns:

  • (self)

    the updated object



131
132
133
134
# File 'lib/lazy_lazer.rb', line 131

def assign_attributes(new_attributes)
  new_attributes.each { |key, value| write_attribute(key, value) }
  self
end

#fully_loaded?Boolean

Returns whether the object is done with lazy loading

Returns:

  • (Boolean)

    whether the object is done with lazy loading



138
139
140
# File 'lib/lazy_lazer.rb', line 138

def fully_loaded?
  @_lazer_loaded
end

#initialize(attributes = {}) ⇒ void

Create a new instance of the class from a set of source attributes.

Parameters:

  • attributes (Hash) (defaults to: {})

    the model attributes

Raises:

  • RequiredAttribute if an attribute marked as required wasn't found



78
79
80
81
82
83
84
# File 'lib/lazy_lazer.rb', line 78

def initialize(attributes = {})
  @_lazer_model = InternalModel.new(self.class., self)
  @_lazer_model.merge!(attributes)
  @_lazer_model.verify_required!
  @_lazer_writethrough = {}
  @_lazer_loaded = false
end

#read_attribute(key_name) ⇒ Object

Return the value of the attribute.

Parameters:

  • key_name (Symbol)

    the attribute name

Returns:

  • (Object)

    the returned value

Raises:

  • MissingAttribute if the key was not found



105
106
107
108
109
# File 'lib/lazy_lazer.rb', line 105

def read_attribute(key_name)
  symbol_key = key_name.to_sym
  return @_lazer_writethrough[symbol_key] if @_lazer_writethrough.key?(symbol_key)
  @_lazer_model.fetch(symbol_key)
end

#reloadself

Reload the object. Calls #lazer_reload, then merges the results into the internal store. Also clears out the internal cache.

Returns:

  • (self)

    the updated object



95
96
97
98
99
# File 'lib/lazy_lazer.rb', line 95

def reload
  new_attributes = lazer_reload.to_h
  @_lazer_model.merge!(new_attributes)
  self
end

#to_hHash

Converts all the attributes that haven't been converted yet and returns the final hash.

Returns:

  • (Hash)

    a hash representation of the model



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

def to_h
  @_lazer_model.to_h
end

#write_attribute(key_name, value) ⇒ Object

Update an attribute.

Parameters:

  • key_name (Symbol)

    the attribute to update

  • value (Object)

    the new value

Returns:

  • (Object)

    the written value



124
125
126
# File 'lib/lazy_lazer.rb', line 124

def write_attribute(key_name, value)
  @_lazer_writethrough[key_name] = value
end