Class: Mockery
Constant Summary
Mockumentary::Model::DUMP_NAME
Class Method Summary
collapse
classes, constantize, evaluate, fake_data, generate, init_defaults, init_opts, #initialize, mock, mock!, mock_defaults, mock_opts, overrides, relationships, #save, save_defaults, save_fields, save_opts, uid
Class Method Details
.ar_class ⇒ Object
2
3
4
|
# File 'lib/mockumentary/mockery.rb', line 2
def self.ar_class
@ar_class ||= infer_ar_class
end
|
.ar_class=(klass) ⇒ Object
6
7
8
9
10
|
# File 'lib/mockumentary/mockery.rb', line 6
def self.ar_class=(klass)
@ar_class = klass
introspect if klass
klass
end
|
.build(klass) ⇒ Object
20
21
22
23
|
# File 'lib/mockumentary/mockery.rb', line 20
def self.build(klass)
super
class_eval "#{container_name}::#{klass}.ar_class = ::#{klass}"
end
|
.container_name ⇒ Object
12
13
14
|
# File 'lib/mockumentary/mockery.rb', line 12
def self.container_name
"Mockery"
end
|
.data_dump ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/mockumentary/mockery.rb', line 59
def self.data_dump
classes.inject({}) do |result, klass|
init = klass.init_defaults.dup
init.merge!(klass.overrides[:init]) if klass.overrides && klass.overrides[:init]
save = klass.save_defaults.dup
save.merge!(klass.overrides[:save]) if klass.overrides && klass.overrides[:save]
mock = klass.mock_defaults.dup
mock.merge!(klass.overrides[:mock]) if klass.overrides && klass.overrides[:mock]
relationships = klass.relationships.inject({}) do |result, array|
key = array.first
value = array.last relationship_type = value.call.type.to_s.gsub(/^Mockery::/, '')
result[key] = relationship_type
result
end
result[klass.ar_class.to_s] = {
:init => init,
:save => save,
:mock => mock,
:relationships => relationships
}
result
end
end
|
.dump(dir = "#{Rails.root}/config") ⇒ Object
53
54
55
56
57
|
# File 'lib/mockumentary/mockery.rb', line 53
def self.dump dir = "#{Rails.root}/config"
File.open("#{dir}/#{DUMP_NAME}", 'w') do |f|
f.write(YAML.dump(data_dump))
end
end
|
.infer_ar_class ⇒ Object
16
17
18
|
# File 'lib/mockumentary/mockery.rb', line 16
def self.infer_ar_class
self.to_s.gsub(/^#{container_name}/, '').constantize
end
|
.introspect ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/mockumentary/mockery.rb', line 32
def self.introspect
reset_defaults
ar_class.columns.each do |c|
name = c.name.to_sym
if save_fields.include?(name)
save_defaults[name] = c.type if name != :id
else
mock_defaults[name] = c.type
end
end
ar_class.reflections.each do |name, reflection|
attr_accessor name
class_name = reflection.options[:class_name] || name.to_s.classify
relationships[name] = lambda { Mockumentary::Collection.new(build(class_name)) } if reflection.collection?
end
end
|
.reset_defaults ⇒ Object
25
26
27
28
29
30
|
# File 'lib/mockumentary/mockery.rb', line 25
def self.reset_defaults
@relationships = nil
@save_defaults = nil
@mock_defaults = nil
@uid = nil
end
|