active_mocker 1.3.2 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +136 -24
- data/Rakefile +8 -2
- data/active_mocker.gemspec +3 -3
- data/lib/active_mock/association.rb +7 -0
- data/lib/active_mock/base.rb +250 -0
- data/lib/active_mock/collection.rb +52 -0
- data/lib/active_mock/creators.rb +25 -0
- data/lib/active_mock/do_nothing_active_record_methods.rb +51 -0
- data/lib/active_mock/has_and_belongs_to_many.rb +7 -0
- data/lib/active_mock/has_many.rb +54 -0
- data/lib/active_mock/next_id.rb +16 -0
- data/lib/active_mock/object_inspect.rb +39 -0
- data/lib/{active_mocker/collection → active_mock}/queries.rb +26 -19
- data/lib/active_mock/records.rb +81 -0
- data/lib/active_mock/relation.rb +8 -0
- data/lib/active_mocker.rb +3 -1
- data/lib/active_mocker/active_mock.rb +26 -0
- data/lib/active_mocker/active_record.rb +18 -0
- data/lib/active_mocker/active_record/relationships.rb +57 -6
- data/lib/active_mocker/active_record/schema.rb +1 -1
- data/lib/active_mocker/active_record/scope.rb +1 -1
- data/lib/active_mocker/active_record/unknown_class_method.rb +1 -1
- data/lib/active_mocker/active_record/unknown_module.rb +10 -9
- data/lib/active_mocker/db_to_ruby_type.rb +26 -0
- data/lib/active_mocker/field.rb +16 -8
- data/lib/active_mocker/generate.rb +19 -174
- data/lib/active_mocker/loaded_mocks.rb +48 -8
- data/lib/active_mocker/logger.rb +2 -0
- data/lib/active_mocker/mock_template.erb +123 -53
- data/lib/active_mocker/model_reader.rb +42 -13
- data/lib/active_mocker/model_schema.rb +279 -0
- data/lib/active_mocker/model_schema/generate.rb +175 -0
- data/lib/active_mocker/reparameterize.rb +23 -3
- data/lib/active_mocker/table.rb +2 -2
- data/lib/active_mocker/version.rb +1 -1
- data/sample_app_rails_4/Gemfile +1 -1
- data/sample_app_rails_4/app/models/micropost.rb +13 -1
- data/sample_app_rails_4/db/schema.rb +1 -1
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +194 -7
- data/sample_app_rails_4/spec/micropost_mock_spec.rb +145 -0
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +81 -55
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +85 -54
- data/sample_app_rails_4/spec/mocks/user_mock.rb +71 -72
- data/sample_app_rails_4/spec/reload_spec.rb +1 -1
- data/sample_app_rails_4/spec/user_mock_spec.rb +25 -7
- data/spec/lib/acitve_mock/queriable_spec.rb +207 -0
- data/spec/lib/active_mocker/db_to_ruby_type_spec.rb +124 -0
- data/spec/lib/active_mocker/loaded_mocks_spec.rb +167 -0
- data/spec/lib/active_mocker/logger_spec.rb +32 -0
- data/spec/lib/active_mocker/model_reader_spec.rb +79 -28
- data/spec/lib/active_mocker/model_schema/generate_spec.rb +111 -0
- data/spec/lib/active_mocker/model_schema_spec.rb +145 -0
- data/spec/lib/model.rb +2 -1
- data/spec/lib/reparameterize_spec.rb +202 -0
- data/spec/unit_logger.rb +2 -2
- metadata +55 -35
- data/lib/active_hash/ar_api.rb +0 -77
- data/lib/active_hash/init.rb +0 -32
- data/lib/active_mocker/collection/association.rb +0 -12
- data/lib/active_mocker/collection/base.rb +0 -65
- data/lib/active_mocker/collection/relation.rb +0 -11
- data/lib/active_mocker/mock_class_methods.rb +0 -92
- data/lib/active_mocker/mock_instance_methods.rb +0 -84
- data/lib/active_mocker/mock_requires.rb +0 -10
- data/spec/lib/active_mocker/collection.rb +0 -94
@@ -5,30 +5,70 @@ module ActiveMocker
|
|
5
5
|
mocks.merge!({mocks_to_add.name => mocks_to_add})
|
6
6
|
end
|
7
7
|
|
8
|
+
def self.add_subclass(subclass)
|
9
|
+
subclasses.merge!({subclass.mocked_class => subclass})
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.subclasses
|
13
|
+
@subclasses ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.clear_subclasses
|
17
|
+
@subclasses = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.mocks
|
21
|
+
@mocks ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.find(klass)
|
25
|
+
class_name_to_mock[klass]
|
26
|
+
end
|
27
|
+
|
8
28
|
def self.all
|
9
29
|
mocks
|
10
30
|
end
|
11
31
|
|
12
32
|
def self.clear_all
|
13
|
-
mocks.each { |n, m| m.clear_mock }
|
33
|
+
action = -> (mocks){ mocks.each { |n, m| m.clear_mock } }
|
34
|
+
action.call(mocks)
|
35
|
+
action.call(subclasses)
|
36
|
+
clear_subclasses
|
14
37
|
end
|
15
38
|
|
16
39
|
def self.delete_all
|
17
|
-
mocks.each { |n, m| m.delete_all }
|
40
|
+
action = -> (mocks) { mocks.each { |n, m| m.delete_all } }
|
41
|
+
action.call(mocks)
|
42
|
+
action.call(subclasses)
|
18
43
|
end
|
19
44
|
|
20
45
|
def self.reload_all
|
21
|
-
mocks.each { |n, m| m.reload }
|
46
|
+
action = -> (mocks) { mocks.each { |n, m| m.send(:reload) } }
|
47
|
+
action.call(mocks)
|
48
|
+
action.call(subclasses)
|
22
49
|
end
|
23
50
|
|
24
|
-
def self.
|
25
|
-
|
51
|
+
def self.undefine_all
|
52
|
+
action = -> (mocks) { mocks.each do |n, m|
|
53
|
+
Object.send(:remove_const, n) if Object.const_defined?(n)
|
54
|
+
end }
|
55
|
+
action.call(mocks)
|
56
|
+
action.call(subclasses)
|
26
57
|
end
|
27
58
|
|
28
|
-
def self.
|
29
|
-
|
30
|
-
|
59
|
+
def self.class_name_to_mock
|
60
|
+
hash = {}
|
61
|
+
mocks.each do |mock_name, mock_constant|
|
62
|
+
hash[mock_constant.mocked_class] = mock_constant
|
31
63
|
end
|
64
|
+
hash.merge(subclasses)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def self.internal_clear
|
70
|
+
clear_subclasses
|
71
|
+
@mocks = nil
|
32
72
|
end
|
33
73
|
end
|
34
74
|
|
data/lib/active_mocker/logger.rb
CHANGED
@@ -1,24 +1,49 @@
|
|
1
|
-
require 'active_mocker/
|
2
|
-
Object.send(:remove_const, "<%= class_name %>") if
|
1
|
+
require 'active_mocker/active_mock'
|
2
|
+
Object.send(:remove_const, "<%= class_name + @mock_append_name %>") if Object.const_defined?("<%= class_name + @mock_append_name %>")
|
3
3
|
|
4
|
-
class <%= class_name %> < ::
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
class <%= class_name + @mock_append_name %> < ActiveMock::Base
|
5
|
+
<% constants.each do |constant| %>
|
6
|
+
<%= constant.first %> = <%= constant.last.inspect %>
|
7
|
+
<% end -%>
|
8
8
|
|
9
|
-
|
10
|
-
@attributes = HashWithIndifferentAccess.new(<%= default_attributes %>)
|
11
|
-
@associations = HashWithIndifferentAccess.new(<%= associations %>)
|
12
|
-
super(attributes, &block)
|
13
|
-
end
|
9
|
+
class << self
|
14
10
|
|
11
|
+
def attributes
|
12
|
+
@attributes ||= HashWithIndifferentAccess.new(<%= attributes_with_defaults %>)
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def types
|
16
|
+
@types ||= <%= types_hash %>
|
17
|
+
end
|
18
|
+
|
19
|
+
def associations
|
20
|
+
@associations ||= <%= associations %>
|
21
|
+
end
|
22
|
+
|
23
|
+
def model_instance_methods
|
24
|
+
@model_instance_methods ||= <%= instance_method_not_implemented %>
|
25
|
+
end
|
26
|
+
|
27
|
+
def model_class_methods
|
28
|
+
@model_class_methods ||= <%= class_method_not_implemented %>
|
29
|
+
end
|
30
|
+
|
31
|
+
def mocked_class
|
32
|
+
'<%= class_name %>'
|
33
|
+
end
|
34
|
+
|
35
|
+
def column_names
|
36
|
+
attribute_names
|
37
|
+
end
|
38
|
+
|
39
|
+
def attribute_names
|
40
|
+
@attribute_names ||= <%= attribute_names %>
|
41
|
+
end
|
42
|
+
|
43
|
+
def primary_key
|
44
|
+
<%= primary_key.name.inspect %>
|
45
|
+
end
|
19
46
|
|
20
|
-
def self.attribute_names
|
21
|
-
@attribute_names = <%= attribute_names %>
|
22
47
|
end
|
23
48
|
|
24
49
|
##################################
|
@@ -26,75 +51,120 @@ class <%= class_name %> < ::ActiveHash::Base
|
|
26
51
|
##################################
|
27
52
|
<% attributes.each do |meth| %>
|
28
53
|
def <%= meth.name %>
|
29
|
-
|
54
|
+
read_attribute(:<%= meth.name %>)
|
30
55
|
end
|
31
56
|
|
32
57
|
def <%= meth.name %>=(val)
|
33
|
-
|
34
|
-
|
58
|
+
<% association = belongs_to_foreign_key(meth.name) -%>
|
59
|
+
write_attribute(:<%= meth.name %>, val)
|
60
|
+
<% if association -%>
|
61
|
+
association = classes('<%= association.class_name %>').try(:find, <%= meth.name %>)
|
62
|
+
write_association(:<%= association.name %>,association) unless association.nil?
|
63
|
+
<% end -%>
|
35
64
|
end
|
36
65
|
<% end %>
|
37
66
|
##################################
|
38
|
-
#
|
67
|
+
# Associations #
|
39
68
|
##################################
|
40
69
|
|
41
|
-
|
42
|
-
|
70
|
+
<%= '# belongs_to' unless belongs_to.empty? -%>
|
71
|
+
<% belongs_to.each do |meth| %>
|
72
|
+
def <%= meth.name %>
|
73
|
+
@associations[:<%= meth.name %>]
|
43
74
|
end
|
44
|
-
|
45
|
-
def <%= meth
|
46
|
-
associations[
|
75
|
+
|
76
|
+
def <%= meth.name %>=(val)
|
77
|
+
@associations[:<%= meth.name %>] = val
|
78
|
+
write_attribute(:<%= meth.foreign_key %>, val.id) if val.respond_to?(:persisted?) && val.persisted?
|
79
|
+
end
|
80
|
+
|
81
|
+
def build_<%= meth.name %>(attributes={}, &block)
|
82
|
+
<% association = relation_find(:name, meth.name).first -%>
|
83
|
+
<% if association -%>
|
84
|
+
association = classes('<%= association.class_name %>').try(:new, attributes, &block)
|
85
|
+
write_association(:<%= meth.name %>, association) unless association.nil?
|
86
|
+
<% end -%>
|
47
87
|
end
|
48
88
|
|
49
|
-
def <%= meth
|
50
|
-
|
89
|
+
def create_<%= meth.name %>(attributes={}, &block)
|
90
|
+
<% association = relation_find(:name, meth.name).first -%>
|
91
|
+
<% if association -%>
|
92
|
+
association = classes('<%= association.class_name %>').try(:create,attributes, &block)
|
93
|
+
write_association(:<%= meth.name %>, nil) unless association.nil?
|
94
|
+
<% end -%>
|
51
95
|
end
|
96
|
+
alias_method :create_<%= meth.name %>!, :create_<%= meth.name %>
|
52
97
|
<% end -%>
|
53
|
-
|
54
|
-
|
55
|
-
|
98
|
+
<%= '# has_one' unless has_one.empty? -%>
|
99
|
+
<% has_one.each do |meth| %>
|
100
|
+
def <%= meth.name %>
|
101
|
+
@associations['<%= meth.name %>']
|
56
102
|
end
|
57
103
|
|
58
|
-
def <%= meth %>=(val)
|
59
|
-
associations['<%= meth %>'] =
|
104
|
+
def <%= meth.name %>=(val)
|
105
|
+
@associations['<%= meth.name %>'] = val
|
60
106
|
end
|
107
|
+
|
108
|
+
def build_<%= meth.name %>(attributes={}, &block)
|
109
|
+
<% association = relation_find(:name, meth.name).first -%>
|
110
|
+
<% if association -%>
|
111
|
+
write_association(:<%= meth.name %>, classes('<%= association.class_name %>').new(attributes, &block)) if classes('<%= association.class_name %>')
|
61
112
|
<% end -%>
|
113
|
+
end
|
62
114
|
|
63
|
-
|
64
|
-
|
65
|
-
|
115
|
+
def create_<%= meth.name %>(attributes={}, &block)
|
116
|
+
<% association = relation_find(:name, meth.name).first -%>
|
117
|
+
<% if association -%>
|
118
|
+
write_association(:<%= meth.name %>,classes('<%= association.class_name %>').create(attributes, &block)) if classes('<%= association.class_name %>')
|
119
|
+
<% end -%>
|
120
|
+
end
|
121
|
+
alias_method :create_<%= meth.name %>!, :create_<%= meth.name %>
|
122
|
+
<% end -%>
|
66
123
|
|
67
|
-
|
68
|
-
|
124
|
+
<%= '# has_many' unless has_many.empty? -%>
|
125
|
+
<% has_many.each do |meth| %>
|
126
|
+
def <%= meth.name %>
|
127
|
+
@associations[:<%= meth.name %>] ||= ActiveMock::HasMany.new([],'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>'))
|
69
128
|
end
|
70
129
|
|
71
|
-
def
|
72
|
-
@
|
130
|
+
def <%= meth.name %>=(val)
|
131
|
+
@associations[:<%= meth.name %>] ||= ActiveMock::HasMany.new(val,'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>'))
|
132
|
+
end
|
133
|
+
<% end -%>
|
134
|
+
<%= '# has_and_belongs_to_many' unless has_and_belongs_to_many.empty? -%>
|
135
|
+
<% has_and_belongs_to_many.each do |meth| %>
|
136
|
+
def <%= meth.name %>
|
137
|
+
@associations[:<%= meth.name %>] ||= ActiveMock::HasAndBelongsToMany.new
|
73
138
|
end
|
74
139
|
|
75
|
-
def
|
76
|
-
@
|
77
|
-
delete_all
|
140
|
+
def <%= meth.name %>=(val)
|
141
|
+
@associations[:<%= meth.name %>] = ActiveMock::HasAndBelongsToMany.new(val)
|
78
142
|
end
|
143
|
+
<% end -%>
|
144
|
+
|
145
|
+
##################################
|
146
|
+
# Model Methods getter/setters #
|
147
|
+
##################################
|
148
|
+
|
79
149
|
<% instance_methods.each do |method| %>
|
80
|
-
def <%= method.
|
81
|
-
block = model_instance_methods['<%= method.
|
82
|
-
self.class.is_implemented(block, '#<%= method.
|
83
|
-
instance_exec(*[<%= method.
|
150
|
+
def <%= method.name %>(<%= method.arguments %>)
|
151
|
+
block = model_instance_methods['<%= method.name %>']
|
152
|
+
self.class.is_implemented(block, '#<%= method.name %>')
|
153
|
+
instance_exec(*[<%= method.arguments.passable %>], &block)
|
84
154
|
end
|
85
155
|
<% end -%>
|
86
156
|
<% class_methods.each do |method| %>
|
87
|
-
def self.<%= method.
|
88
|
-
block = model_class_methods['<%= method.
|
89
|
-
is_implemented(block, '::<%= method.
|
90
|
-
instance_exec(*[<%= method.
|
157
|
+
def self.<%= method.name %>(<%= method.arguments %>)
|
158
|
+
block = model_class_methods['<%= method.name %>']
|
159
|
+
is_implemented(block, '::<%= method.name %>')
|
160
|
+
instance_exec(*[<%= method.arguments.passable %>], &block)
|
91
161
|
end
|
92
162
|
<% end -%>
|
93
163
|
|
164
|
+
private
|
165
|
+
|
94
166
|
def self.reload
|
95
167
|
load __FILE__
|
96
168
|
end
|
97
169
|
|
98
|
-
end
|
99
|
-
|
100
|
-
ActiveMocker::LoadedMocks.add(<%= class_name %>)
|
170
|
+
end
|
@@ -12,7 +12,8 @@ module ActiveMocker
|
|
12
12
|
def parse(model_name)
|
13
13
|
@model_name = model_name
|
14
14
|
klass
|
15
|
-
self
|
15
|
+
return self unless @klass == false
|
16
|
+
return false
|
16
17
|
end
|
17
18
|
|
18
19
|
def klass
|
@@ -20,22 +21,17 @@ module ActiveMocker
|
|
20
21
|
end
|
21
22
|
|
22
23
|
def eval_file
|
23
|
-
failure =
|
24
|
-
while failure
|
24
|
+
failure = false
|
25
25
|
begin
|
26
26
|
m = Module.new
|
27
27
|
m.module_eval(read_file, file_path)
|
28
|
-
rescue
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
Logger_.debug "ActiveMocker :: Can't can't find Constant #{const_name} from class #{model_name}..\n #{caller}"
|
33
|
-
next
|
28
|
+
rescue Exception => e
|
29
|
+
Logger.error "ActiveMocker :: Error loading Model: #{model_name} \n \t\t\t\t\t\t\t\t`#{e}` \n"
|
30
|
+
Logger.error "\t\t\t\t\t\t\t\t #{file_path}\n"
|
31
|
+
failure = true
|
34
32
|
end
|
35
|
-
|
36
|
-
|
37
|
-
end
|
38
|
-
model
|
33
|
+
return m.const_get m.constants.first unless failure
|
34
|
+
return false
|
39
35
|
end
|
40
36
|
|
41
37
|
def read_file
|
@@ -82,6 +78,39 @@ module ActiveMocker
|
|
82
78
|
klass.single_relationships.flatten.compact
|
83
79
|
end
|
84
80
|
|
81
|
+
def belongs_to
|
82
|
+
klass.relationships.belongs_to
|
83
|
+
end
|
84
|
+
|
85
|
+
def has_one
|
86
|
+
klass.relationships.has_one
|
87
|
+
end
|
88
|
+
|
89
|
+
def has_and_belongs_to_many
|
90
|
+
klass.relationships.has_and_belongs_to_many
|
91
|
+
end
|
92
|
+
|
93
|
+
def has_many
|
94
|
+
klass.relationships.has_many
|
95
|
+
end
|
96
|
+
|
97
|
+
def table_name
|
98
|
+
klass.table_name
|
99
|
+
end
|
100
|
+
|
101
|
+
def primary_key
|
102
|
+
klass.primary_key
|
103
|
+
end
|
104
|
+
|
105
|
+
def constants
|
106
|
+
const = {}
|
107
|
+
klass.constants.each {|c| const[c] = klass.const_get(c)}
|
108
|
+
const = const.reject do |c, v|
|
109
|
+
v.class == Module || v.class == Class
|
110
|
+
end
|
111
|
+
const
|
112
|
+
end
|
113
|
+
|
85
114
|
end
|
86
115
|
|
87
116
|
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
module ActiveMocker
|
2
|
+
|
3
|
+
module LoggerToJson
|
4
|
+
|
5
|
+
def initialize(*args)
|
6
|
+
super(args)
|
7
|
+
UnitLogger.unit.info "#{caller_locations[1]}\n"
|
8
|
+
obj = JSON.parse(self.to_hash.to_json)
|
9
|
+
UnitLogger.unit.info "#{self.class.name}: #{JSON.pretty_unparse(obj)}\n"
|
10
|
+
puts "#{self.class.name}: #{JSON.pretty_unparse(obj)}\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class ModelSchemaCollection
|
16
|
+
|
17
|
+
include Enumerable
|
18
|
+
|
19
|
+
attr_accessor :collection
|
20
|
+
|
21
|
+
def initialize(collection)
|
22
|
+
@collection = collection
|
23
|
+
end
|
24
|
+
|
25
|
+
def each(&block)
|
26
|
+
collection.each do |item|
|
27
|
+
block.call(item)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def find_by(options={})
|
32
|
+
collection.select{|c| c.send(options.keys.first) == options.values.first}.first
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_by_in_relationships(options={})
|
36
|
+
result = nil
|
37
|
+
collection.each do |item|
|
38
|
+
result = item.relationships.select do |attr|
|
39
|
+
attr.send(options.keys.first) == options.values.first
|
40
|
+
end
|
41
|
+
end
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def [](val)
|
47
|
+
collection[val]
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
class ModelSchema
|
53
|
+
|
54
|
+
include LoggerToJson
|
55
|
+
|
56
|
+
attr_reader :class_name, :table_name, :attributes, :relationships, :methods
|
57
|
+
|
58
|
+
def initialize( class_name: nil,
|
59
|
+
table_name: nil,
|
60
|
+
attributes: nil,
|
61
|
+
relationships: nil,
|
62
|
+
methods: nil,
|
63
|
+
constants: nil,
|
64
|
+
is_join_table: nil,
|
65
|
+
join_table_members: nil
|
66
|
+
)
|
67
|
+
@class_name = class_name
|
68
|
+
@table_name = table_name
|
69
|
+
@attributes = attributes unless attributes.nil?
|
70
|
+
@relationships = relationships unless relationships.nil?
|
71
|
+
@methods = methods unless (methods || []).empty?
|
72
|
+
@constants = constants unless (constants || []).empty?
|
73
|
+
@is_join_table = is_join_table unless is_join_table.nil?
|
74
|
+
@join_table_members = join_table_members unless join_table_members.nil?
|
75
|
+
|
76
|
+
# UnitLogger.unit.info "#{caller_locations[1]}\n"
|
77
|
+
# obj = ::JSON.parse(self.to_json)
|
78
|
+
# UnitLogger.unit.info "#{self.class.name}: #{JSON.pretty_unparse(obj)}\n"
|
79
|
+
# puts "#{self.class.name}: #{JSON.pretty_unparse(obj)}\n"
|
80
|
+
end
|
81
|
+
|
82
|
+
def constants
|
83
|
+
@constants || []
|
84
|
+
end
|
85
|
+
|
86
|
+
def has_many
|
87
|
+
relation_find(:type, :has_many)
|
88
|
+
end
|
89
|
+
|
90
|
+
def has_one
|
91
|
+
relation_find(:type, :has_one)
|
92
|
+
end
|
93
|
+
|
94
|
+
def belongs_to
|
95
|
+
relation_find(:type, :belongs_to)
|
96
|
+
end
|
97
|
+
|
98
|
+
def has_and_belongs_to_many
|
99
|
+
relation_find(:type, :has_and_belongs_to_many)
|
100
|
+
end
|
101
|
+
|
102
|
+
def belongs_to_foreign_key(foreign_key)
|
103
|
+
belongs_to.select { |r| r.foreign_key.to_sym == foreign_key.to_sym }.first
|
104
|
+
end
|
105
|
+
|
106
|
+
def relation_find(key, value)
|
107
|
+
relationships.select { |r| r.send(key).to_sym == value }
|
108
|
+
end
|
109
|
+
|
110
|
+
def instance_methods
|
111
|
+
method_find(:instance)
|
112
|
+
end
|
113
|
+
|
114
|
+
def class_methods
|
115
|
+
method_find(:class)
|
116
|
+
end
|
117
|
+
|
118
|
+
def method_find(type)
|
119
|
+
return [] if methods.nil?
|
120
|
+
methods.select { |r| r.type.to_sym == type }
|
121
|
+
end
|
122
|
+
|
123
|
+
def attribute_names
|
124
|
+
attributes.map(&:name)
|
125
|
+
end
|
126
|
+
|
127
|
+
def types_hash
|
128
|
+
types = {}
|
129
|
+
attributes.each do |attr|
|
130
|
+
types[attr.name] = "build_type(#{attr.ruby_type})"
|
131
|
+
end
|
132
|
+
|
133
|
+
type_array = types.map do |name, type|
|
134
|
+
"#{name}: #{type}"
|
135
|
+
end
|
136
|
+
'{ ' + type_array.join(', ') + ' }'
|
137
|
+
end
|
138
|
+
|
139
|
+
def attributes_with_defaults
|
140
|
+
hash = {}
|
141
|
+
attributes.each do |attr|
|
142
|
+
hash[attr.name] = attr.default_value
|
143
|
+
end
|
144
|
+
hash
|
145
|
+
end
|
146
|
+
|
147
|
+
def associations
|
148
|
+
hash = {}
|
149
|
+
relationships.each do |r|
|
150
|
+
hash[r.name] = nil
|
151
|
+
end
|
152
|
+
hash
|
153
|
+
end
|
154
|
+
|
155
|
+
def mock_name(klass_name)
|
156
|
+
klass_name + "Mock"
|
157
|
+
end
|
158
|
+
|
159
|
+
def class_method_not_implemented
|
160
|
+
not_implemented(class_methods)
|
161
|
+
end
|
162
|
+
|
163
|
+
def instance_method_not_implemented
|
164
|
+
not_implemented(instance_methods)
|
165
|
+
end
|
166
|
+
|
167
|
+
def not_implemented(collection)
|
168
|
+
hash = {}
|
169
|
+
collection.each do |meth|
|
170
|
+
hash[meth.name.to_s] = :not_implemented
|
171
|
+
end
|
172
|
+
hash
|
173
|
+
end
|
174
|
+
|
175
|
+
def render(template, mock_append_name)
|
176
|
+
@mock_append_name = mock_append_name
|
177
|
+
ERB.new(template, nil, '-').result(binding)
|
178
|
+
end
|
179
|
+
|
180
|
+
private :relation_find
|
181
|
+
|
182
|
+
def primary_key
|
183
|
+
key_attribute = attributes.select { |attr| attr.primary_key }.first
|
184
|
+
return key_attribute if key_attribute
|
185
|
+
default_primary_key
|
186
|
+
end
|
187
|
+
|
188
|
+
def default_primary_key
|
189
|
+
default_id = Attributes.new(name: 'id', primary_key: true, type: :integer)
|
190
|
+
attributes.unshift(default_id)
|
191
|
+
default_id
|
192
|
+
end
|
193
|
+
|
194
|
+
class Attributes
|
195
|
+
|
196
|
+
attr_reader :name, :type, :precision, :scale, :default_value, :primary_key
|
197
|
+
attr_writer :primary_key
|
198
|
+
def initialize(name:,
|
199
|
+
type:,
|
200
|
+
precision: nil,
|
201
|
+
scale: nil,
|
202
|
+
default_value: nil,
|
203
|
+
primary_key: nil
|
204
|
+
)
|
205
|
+
@name = name
|
206
|
+
@type = type
|
207
|
+
@precision = precision unless precision.nil?
|
208
|
+
@scale = scale unless scale.nil?
|
209
|
+
@default_value = default_value unless default_value.nil?
|
210
|
+
@primary_key = primary_key unless primary_key.nil?
|
211
|
+
end
|
212
|
+
|
213
|
+
def ruby_type
|
214
|
+
ActiveMocker::DBToRubyType.call(type)
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
class Relationships
|
220
|
+
|
221
|
+
attr_reader :name, :class_name, :type, :through, :foreign_key, :join_table
|
222
|
+
|
223
|
+
def initialize(name:,
|
224
|
+
class_name:,
|
225
|
+
type:,
|
226
|
+
through:,
|
227
|
+
foreign_key:,
|
228
|
+
join_table:
|
229
|
+
)
|
230
|
+
@name = name
|
231
|
+
@class_name = class_name
|
232
|
+
@type = type
|
233
|
+
@through = through unless through.nil?
|
234
|
+
@foreign_key = foreign_key unless foreign_key.nil?
|
235
|
+
@join_table = join_table unless join_table.nil?
|
236
|
+
end
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
class Methods
|
241
|
+
|
242
|
+
attr_reader :name, :arguments, :type
|
243
|
+
|
244
|
+
def initialize( name:,
|
245
|
+
arguments:,
|
246
|
+
type:
|
247
|
+
)
|
248
|
+
@name = name
|
249
|
+
@arguments = Arguments.new(arguments)
|
250
|
+
@type = type
|
251
|
+
end
|
252
|
+
|
253
|
+
class Arguments
|
254
|
+
|
255
|
+
attr_reader :arguments
|
256
|
+
|
257
|
+
def initialize(arguments)
|
258
|
+
@arguments = arguments
|
259
|
+
end
|
260
|
+
|
261
|
+
def to_hash
|
262
|
+
@arguments
|
263
|
+
end
|
264
|
+
|
265
|
+
def to_s
|
266
|
+
Reparameterize.call(arguments)
|
267
|
+
end
|
268
|
+
|
269
|
+
def passable
|
270
|
+
Reparameterize.call(arguments, param_list: true)
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|