Module: LazyLazer::InstanceMethods

Extended by:
Forwardable
Defined in:
lib/lazy_lazer.rb

Overview

The methods to extend the instance with.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

Equality check, performed using required keys.

Parameters:

  • other (Object)

    the other object

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/lazy_lazer.rb', line 85

def ==(other)
  return false if self.class != other.class
  return super if @_lazer_model.required_properties.empty?
  @_lazer_model.required_properties.each do |key_name|
    return false if read_attribute(key_name) != other.read_attribute(key_name)
  end
  true
end

#[](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



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

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



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

def assign_attributes(new_attributes)
  new_attributes.each { |key, value| write_attribute(key, value) }
  self
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



76
77
78
79
80
# File 'lib/lazy_lazer.rb', line 76

def initialize(attributes = {})
  @_lazer_model = InternalModel.new(self.class.instance_variable_get(:@_lazer_metadata), self)
  @_lazer_model.merge!(attributes)
  @_lazer_model.verify_required!
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



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

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