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?, find_by, with_email, with_id, with_id?, with_primary_key, with_primary_key?

Methods included from DSL::Validation::Input

#caching_valid?, #filter_logic_valid?

Methods included from DSL::Builder::Helpers

include_helpers

Methods included from DSL::Builder::Settings

__primary_key, caching, primary_key

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



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

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

Class Attribute Details

.currentObject

Returns the value of attribute current.



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

def current
  @current
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

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)


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

def create(attributes)
  attributes.transform_keys! { |key| key.to_s.tr('-', '_').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:



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

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:



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

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)


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

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)


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

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


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

def update(attrs)
  attrs.transform_keys! { |key| key.to_s.tr('-', '_').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