Class: TestingRecord::Model

Inherits:
Object
  • Object
show all
Extended by:
DSL::Builder::Filters, DSL::Builder::Helpers, DSL::Builder::Settings
Defined in:
lib/testing_record/model.rb

Overview

The top level Model. Most of the behaviours specified here are fairly rudimentary ones that will then include other behaviour(s), from the included modules

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Builder::Filters

exists?, with_email, with_id, with_id?, with_primary_key, with_primary_key?

Methods included from DSL::Builder::Helpers

include_helpers

Methods included from DSL::Builder::Settings

__primary_key, caching, primary_key

Methods included from DSL::Validation::Input

#caching_valid?

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



113
114
115
# File 'lib/testing_record/model.rb', line 113

def initialize(attributes = {})
  @attributes = attributes
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



17
18
19
# File 'lib/testing_record/model.rb', line 17

def current
  @current
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



14
15
16
# File 'lib/testing_record/model.rb', line 14

def attributes
  @attributes
end

Class Method Details

.create(attributes) ⇒ TestingRecord::Model

Creates an instance of the model

-> Ensures that the primary key is specified in the attribute payload
-> Validates that a duplicate entity has not been made (If caching is enabled)
-> Creates iVar values (Symbol format) and attr_reader's for each attribute that was provided
-> Adds helper methods (If the model has been configured to include helpers)
-> Adds it to the cache (If caching is enabled)


27
28
29
30
31
32
33
34
# File 'lib/testing_record/model.rb', line 27

def create(attributes)
  attributes.transform_keys!(&:to_sym)
  if respond_to?(:all)
    create_with_caching(attributes)
  else
    create_without_caching(attributes)
  end
end

.delete(entity) ⇒ TestingRecord::Model?

Deletes the instance of the model from the cache (Does nothing if caching is disabled)

Returns:



51
52
53
54
55
56
# File 'lib/testing_record/model.rb', line 51

def delete(entity)
  return unless respond_to?(:all)

  self.current = nil if entity == current
  all.delete(entity)
end

.delete_by_id(id) ⇒ TestingRecord::Model?

Deletes the instance of the model from the cache (Does nothing if caching is disabled)

Returns:



61
62
63
# File 'lib/testing_record/model.rb', line 61

def delete_by_id(id)
  delete(with_id(id)) if respond_to?(:all)
end

Instance Method Details

#inspectString

View the entity in question in a readable format. Displays all iVars and their values (Primary Key first if set)

Returns:

  • (String)


120
121
122
123
# File 'lib/testing_record/model.rb', line 120

def inspect
  reorder_attributes_for_inspect!
  "#<#{self.class.name} #{attributes.map { |k, v| "@#{k}=#{v.inspect}" }.join(', ')}>"
end

#to_sString

Functionally equivalent to ‘inspect`

Returns:

  • (String)


128
129
130
# File 'lib/testing_record/model.rb', line 128

def to_s
  inspect
end

#update(attrs) ⇒ TestingRecord::Model

Updates an entity (instance), of a model

-> Updating iVar values for each attribute that was provided (Converting to symbolized format)
-> It will **not** create new reader methods for new variables added


137
138
139
140
141
142
143
144
# File 'lib/testing_record/model.rb', line 137

def update(attrs)
  attrs.transform_keys(&:to_sym).each do |key, value|
    attributes[key] = value
    instance_variable_set("@#{key}", value)
    TestingRecord.logger.info("Updated '#{key}' on the #{self.class} entity to be '#{value}'")
  end
  self
end