Class: ActiveMock::Base

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

Defined Under Namespace

Modules: MockAbilities, PropertiesGetterAndSetter

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!, included, included_klass, limit, maximum, minimum, order, reverse_order, sum, update_all, where

Methods included from Creators

create, find_or_create_by, find_or_initialize_by

Methods included from MockAbilities

included, #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.



80
81
82
83
# File 'lib/active_mock/base.rb', line 80

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

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



78
79
80
# File 'lib/active_mock/base.rb', line 78

def associations
  @associations
end

#attributesObject (readonly)

Returns the value of attribute attributes.



78
79
80
# File 'lib/active_mock/base.rb', line 78

def attributes
  @attributes
end

#typesObject (readonly)

Returns the value of attribute types.



78
79
80
# File 'lib/active_mock/base.rb', line 78

def types
  @types
end

Class Method Details

.build_type(type) ⇒ Object



53
54
55
# File 'lib/active_mock/base.rb', line 53

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

.classes(klass) ⇒ Object



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

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

.clear_mockObject



65
66
67
68
# File 'lib/active_mock/base.rb', line 65

def clear_mock
  @model_class_methods, @model_instance_methods = nil, nil
  delete_all
end

.delete(id) ⇒ Object



40
41
42
# File 'lib/active_mock/base.rb', line 40

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

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



46
47
48
49
# File 'lib/active_mock/base.rb', line 46

def delete_all(options=nil)
  return records.reset_all_records if options.nil?
  where(options).map { |r| r.delete }.count
end

.destroyObject



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

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

.inherited(subclass) ⇒ Object



24
25
26
27
# File 'lib/active_mock/base.rb', line 24

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

.recordsObject



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

def records
  @records ||= Records.new
end

Instance Method Details

#==(obj) ⇒ Object



160
161
162
163
164
# File 'lib/active_mock/base.rb', line 160

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



134
135
136
# File 'lib/active_mock/base.rb', line 134

def delete
  records.delete(self)
end

#hashObject



156
157
158
# File 'lib/active_mock/base.rb', line 156

def hash
  attributes.hash
end

#inspectObject



152
153
154
# File 'lib/active_mock/base.rb', line 152

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

#new_record?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/active_mock/base.rb', line 140

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

#persisted?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/active_mock/base.rb', line 144

def persisted?
  records.persisted?(id)
end

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



119
120
121
122
123
124
# File 'lib/active_mock/base.rb', line 119

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

#to_hashObject



148
149
150
# File 'lib/active_mock/base.rb', line 148

def to_hash
  attributes
end

#update(attributes = {}) ⇒ Object



103
104
105
# File 'lib/active_mock/base.rb', line 103

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