module LazyRecord::BaseModule

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

Constants

LAZY_RECORD_MODULES

Modules that compose the functionality of the LazyRecord API

Public Class Methods

included(base) click to toggle source

Extend these modules when BaseModule is included

# 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
new(opts = {}) { |self| ... } click to toggle source

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

# 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

Public Instance Methods

==(other) click to toggle source
# File lib/lazy_record/base_module.rb, line 49
def ==(other)
  compare(other, :==)
end
Also aliased as: eql?
===(other) click to toggle source
# File lib/lazy_record/base_module.rb, line 53
def ===(other)
  compare(other, :===)
end
associations() click to toggle source
# File lib/lazy_record/base_module.rb, line 41
def associations
  []
end
collections() click to toggle source
# File lib/lazy_record/base_module.rb, line 45
def collections
  []
end
compare(other, operator) click to toggle source
# 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
eql?(other)
Alias for: ==
hash() click to toggle source
# 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
inspect() click to toggle source
# File lib/lazy_record/base_module.rb, line 35
def inspect
  format('#<%s%s>',
         self.class,
         displayable_attributes.join(', '))
end

Private Instance Methods

associations_to_s() click to toggle source
# File lib/lazy_record/base_module.rb, line 87
def associations_to_s
  []
end
collection_counts_to_s() click to toggle source
# File lib/lazy_record/base_module.rb, line 76
def collection_counts_to_s
  []
end
displayable_attributes() click to toggle source
# File lib/lazy_record/base_module.rb, line 91
def displayable_attributes
  attributes = [
    public_attr_readers_to_s.dup,
    associations_to_s.dup,
    collection_counts_to_s.dup
  ].flatten
  return attributes if attributes.empty?
  attributes.unshift(' ' + attributes.shift)
end
public_attr_readers_to_s() click to toggle source
# File lib/lazy_record/base_module.rb, line 80
def public_attr_readers_to_s
  self.class.send(:public_attr_readers).map do |attr|
    value = send(attr)
    "#{attr}: #{stringify_value(value)}"
  end
end
set_equality_conditions() click to toggle source
# File lib/lazy_record/base_module.rb, line 72
def set_equality_conditions
  self.class.send(:attr_readers) + associations
end
stringify_value(value) click to toggle source
# File lib/lazy_record/base_module.rb, line 101
def stringify_value(value)
  if value.is_a?(String)
    "\"#{value}\""
  elsif value.nil?
    'nil'
  else
    value
  end
end