Module: LazyRecord::BaseModule

Included in:
Base
Defined in:
lib/lazy_record/base_module.rb

Overview

This module gives the Base class its functionality, and can be included in any class as an alternative to inheriting from LazyRecord::Base

Constant Summary collapse

LAZY_RECORD_MODULES =

Modules that compose the functionality of the LazyRecord API

[
  ClassMethods,
  Scopes,
  Attributes,
  Collections,
  Associations,
  Callbacks,
  Validations,
  Methods,
  DynamicModules
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Extend these modules when BaseModule is included



21
22
23
24
# File 'lib/lazy_record/base_module.rb', line 21

def self.included(base)
  base.extend ScopedAttrAccessor
  LAZY_RECORD_MODULES.each { |mod| base.extend mod }
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



49
50
51
# File 'lib/lazy_record/base_module.rb', line 49

def ==(other)
  compare(other, :==)
end

#===(other) ⇒ Object



53
54
55
# File 'lib/lazy_record/base_module.rb', line 53

def ===(other)
  compare(other, :===)
end

#associationsObject



41
42
43
# File 'lib/lazy_record/base_module.rb', line 41

def associations
  []
end

#collectionsObject



45
46
47
# File 'lib/lazy_record/base_module.rb', line 45

def collections
  []
end

#compare(other, operator) ⇒ Object



57
58
59
60
61
# File 'lib/lazy_record/base_module.rb', line 57

def compare(other, operator)
  conditions = set_equality_conditions
  return false if !other.is_a?(self.class) || conditions.empty?
  conditions.all? { |attr| send(attr).send(operator, other.send(attr)) }
end

#hashObject



63
64
65
66
67
68
# File 'lib/lazy_record/base_module.rb', line 63

def hash
  conditions = set_equality_conditions
  hash = {}
  conditions.each { |attr| hash[attr] = send(attr) }
  hash.hash
end

#initialize(opts = {}) {|_self| ... } ⇒ Object

Use options hash to set attributes, and/or operate on object in a block. Checks each options key for a matching attribute setter method.

Yields:

  • (_self)

Yield Parameters:



28
29
30
31
32
33
# File 'lib/lazy_record/base_module.rb', line 28

def initialize(opts = {})
  opts.each do |key, val|
    send("#{key}=", val) if respond_to?("#{key}=")
  end
  yield self if block_given?
end

#inspectObject



35
36
37
38
39
# File 'lib/lazy_record/base_module.rb', line 35

def inspect
  format('#<%s%s>',
         self.class,
         displayable_attributes.join(', '))
end