Class: ActiveMocker::Mock::Base

Inherits:
Object
  • Object
show all
Extended by:
Queries
Includes:
PropertiesGetterAndSetter, DoNothingActiveRecordMethods, MockAbilities, TemplateMethods
Defined in:
lib/active_mocker/mock/base.rb

Defined Under Namespace

Modules: PropertiesGetterAndSetter Classes: ScopeRelation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Queries

all, average, delete_all, destroy_all, find, find_by, find_by!, limit, maximum, minimum, order, reverse_order, sum, update_all, where

Methods included from TemplateMethods

included

Methods included from MockAbilities

#clear_mocked_methods, included

Methods included from MockAbilities::InstanceAndClassMethods

#clear_mocked_methods, #mock_instance_method

Methods included from DoNothingActiveRecordMethods

#destroyed?, #errors, included, #marked_for_destruction?, #readonly?, #valid?

Constructor Details

#initialize(attributes = {}, &block) ⇒ Base

Returns a new instance of Base.



88
89
90
91
# File 'lib/active_mocker/mock/base.rb', line 88

def initialize(attributes = {}, &block)
  setup_instance_variables
  set_properties_block(attributes, &block)
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



86
87
88
# File 'lib/active_mocker/mock/base.rb', line 86

def associations
  @associations
end

#attributesObject (readonly)

Returns the value of attribute attributes.



86
87
88
# File 'lib/active_mocker/mock/base.rb', line 86

def attributes
  @attributes
end

#typesObject (readonly)

Returns the value of attribute types.



86
87
88
# File 'lib/active_mocker/mock/base.rb', line 86

def types
  @types
end

Class Method Details

.build_type(type) ⇒ Object



57
58
59
# File 'lib/active_mocker/mock/base.rb', line 57

def build_type(type)
  Virtus::Attribute.build(type)
end

.classes(klass) ⇒ Object



61
62
63
# File 'lib/active_mocker/mock/base.rb', line 61

def classes(klass)
  ActiveMocker::LoadedMocks.find(klass)
end

.clear_mockObject



73
74
75
76
# File 'lib/active_mocker/mock/base.rb', line 73

def clear_mock
  clear_mocked_methods
  delete_all
end

.create(attributes = {}, &block) ⇒ Object Also known as: create!



17
18
19
20
21
22
23
# File 'lib/active_mocker/mock/base.rb', line 17

def create(attributes = {}, &block)
  record = new
  record.save
  record.send(:set_properties, attributes) unless block_given?
  record.send(:set_properties_block, attributes, &block) if block_given?
  record
end

.delete(id) ⇒ Object



44
45
46
# File 'lib/active_mocker/mock/base.rb', line 44

def delete(id)
  find(id).delete
end

.delete_all(options = nil) ⇒ Object Also known as: destroy_all



50
51
52
53
# File 'lib/active_mocker/mock/base.rb', line 50

def delete_all(options=nil)
  return records.reset if options.nil?
  super
end

.destroyObject



48
49
50
# File 'lib/active_mocker/mock/base.rb', line 48

def delete(id)
  find(id).delete
end

.find_or_create_by(attributes) ⇒ Object



27
28
29
# File 'lib/active_mocker/mock/base.rb', line 27

def find_or_create_by(attributes)
  find_by(attributes) || create(attributes)
end

.find_or_initialize_by(attributes) ⇒ Object



31
32
33
# File 'lib/active_mocker/mock/base.rb', line 31

def find_or_initialize_by(attributes)
  find_by(attributes) || new(attributes)
end

.inherited(subclass) ⇒ Object



10
11
12
13
# File 'lib/active_mocker/mock/base.rb', line 10

def self.inherited(subclass)
  return ActiveMocker::LoadedMocks.add(subclass) if subclass.superclass == Base
  ActiveMocker::LoadedMocks.add_subclass(subclass)
end

.new_relation(collection) ⇒ Object



65
66
67
# File 'lib/active_mocker/mock/base.rb', line 65

def new_relation(collection)
  ScopeRelation.new(collection)
end

.recordsObject



35
36
37
# File 'lib/active_mocker/mock/base.rb', line 35

def records
  @records ||= Records.new
end

Instance Method Details

#==(obj) ⇒ Object



170
171
172
173
174
# File 'lib/active_mocker/mock/base.rb', line 170

def ==(obj)
  return false if obj.nil?
  return hash == obj.attributes.hash if obj.respond_to?(:attributes)
  hash == obj.hash if obj.respond_to?(:hash)
end

#deleteObject Also known as: destroy



138
139
140
# File 'lib/active_mocker/mock/base.rb', line 138

def delete
  records.delete(self)
end

#hashObject



166
167
168
# File 'lib/active_mocker/mock/base.rb', line 166

def hash
  attributes.hash
end

#inspectObject



162
163
164
# File 'lib/active_mocker/mock/base.rb', line 162

def inspect
  ObjectInspect.new(self.class.name, attributes).to_s
end

#new_record?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/active_mocker/mock/base.rb', line 150

def new_record?
  !records.new_record?(self)
end

#persisted?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/active_mocker/mock/base.rb', line 154

def persisted?
  records.persisted?(id)
end

#reloadObject



146
147
148
# File 'lib/active_mocker/mock/base.rb', line 146

def reload
  self
end

#save(*args) ⇒ Object Also known as: save!



123
124
125
126
127
128
# File 'lib/active_mocker/mock/base.rb', line 123

def save(*args)
  unless self.class.exists?(self)
    self.class.send(:insert, self)
  end
  true
end

#to_hashObject



158
159
160
# File 'lib/active_mocker/mock/base.rb', line 158

def to_hash
  attributes
end

#update(attributes = {}) ⇒ Object



107
108
109
# File 'lib/active_mocker/mock/base.rb', line 107

def update(attributes={})
  set_properties(attributes)
end