active_mocker 1.4.2 → 1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -0
  3. data/README.md +35 -29
  4. data/lib/active_mocker.rb +3 -1
  5. data/lib/active_mocker/active_record.rb +27 -5
  6. data/lib/active_mocker/active_record/scope.rb +5 -5
  7. data/lib/active_mocker/active_record/unknown_module.rb +4 -4
  8. data/lib/active_mocker/generate.rb +36 -15
  9. data/lib/active_mocker/loaded_mocks.rb +60 -54
  10. data/lib/active_mocker/mock.rb +24 -0
  11. data/lib/active_mocker/mock/association.rb +6 -0
  12. data/lib/{active_mock → active_mocker/mock}/base.rb +43 -89
  13. data/lib/{active_mock → active_mocker/mock}/collection.rb +14 -7
  14. data/lib/{active_mock → active_mocker/mock}/do_nothing_active_record_methods.rb +7 -1
  15. data/lib/active_mocker/mock/exceptions.rb +25 -0
  16. data/lib/active_mocker/mock/has_and_belongs_to_many.rb +6 -0
  17. data/lib/active_mocker/mock/has_many.rb +60 -0
  18. data/lib/active_mocker/mock/hash_process.rb +19 -0
  19. data/lib/active_mocker/mock/mock_abilities.rb +98 -0
  20. data/lib/{active_mock → active_mocker/mock}/next_id.rb +7 -2
  21. data/lib/{active_mock → active_mocker/mock}/object_inspect.rb +3 -1
  22. data/lib/{active_mock → active_mocker/mock}/queries.rb +32 -29
  23. data/lib/active_mocker/mock/records.rb +62 -0
  24. data/lib/{active_mock → active_mocker/mock}/relation.rb +4 -3
  25. data/lib/active_mocker/mock/template_methods.rb +39 -0
  26. data/lib/active_mocker/mock_template.erb +45 -31
  27. data/lib/active_mocker/model_reader.rb +29 -0
  28. data/lib/active_mocker/model_schema.rb +23 -16
  29. data/lib/active_mocker/model_schema/generate.rb +19 -8
  30. data/lib/active_mocker/railtie.rb +17 -0
  31. data/lib/active_mocker/reparameterize.rb +2 -4
  32. data/lib/active_mocker/rspec_helper.rb +18 -0
  33. data/lib/active_mocker/task.rake +15 -0
  34. data/lib/active_mocker/version.rb +1 -1
  35. metadata +80 -206
  36. data/.gitignore +0 -19
  37. data/.hound.yml +0 -0
  38. data/.travis.yml +0 -5
  39. data/Gemfile +0 -4
  40. data/Rakefile +0 -26
  41. data/active_mocker.gemspec +0 -40
  42. data/lib/active_mock/association.rb +0 -7
  43. data/lib/active_mock/creators.rb +0 -25
  44. data/lib/active_mock/has_and_belongs_to_many.rb +0 -7
  45. data/lib/active_mock/has_many.rb +0 -54
  46. data/lib/active_mock/records.rb +0 -83
  47. data/lib/active_mocker/active_mock.rb +0 -26
  48. data/lib/active_mocker/active_record/const_missing.rb +0 -5
  49. data/lib/active_mocker/class_exists.rb +0 -12
  50. data/lib/active_mocker/const_sets.rb +0 -24
  51. data/lib/active_mocker/mock_task.rb +0 -12
  52. data/sample_app_rails_4/.idea/.generators +0 -8
  53. data/sample_app_rails_4/.idea/.name +0 -1
  54. data/sample_app_rails_4/.idea/.rakeTasks +0 -7
  55. data/sample_app_rails_4/.idea/dataSources.ids +0 -141
  56. data/sample_app_rails_4/.idea/dataSources.xml +0 -36
  57. data/sample_app_rails_4/.idea/dictionaries/zeisler.xml +0 -3
  58. data/sample_app_rails_4/.idea/encodings.xml +0 -5
  59. data/sample_app_rails_4/.idea/inspectionProfiles/Project_Default.xml +0 -20
  60. data/sample_app_rails_4/.idea/inspectionProfiles/profiles_settings.xml +0 -7
  61. data/sample_app_rails_4/.idea/misc.xml +0 -5
  62. data/sample_app_rails_4/.idea/modules.xml +0 -10
  63. data/sample_app_rails_4/.idea/runConfigurations/Development__sample_app_rails_4.xml +0 -28
  64. data/sample_app_rails_4/.idea/runConfigurations/Production__sample_app_rails_4.xml +0 -28
  65. data/sample_app_rails_4/.idea/runConfigurations/spec__sample_app_rails_4.xml +0 -26
  66. data/sample_app_rails_4/.idea/runConfigurations/test__sample_app_rails_4.xml +0 -28
  67. data/sample_app_rails_4/.idea/sample_app_rails_4.iml +0 -262
  68. data/sample_app_rails_4/.idea/scopes/scope_settings.xml +0 -5
  69. data/sample_app_rails_4/.idea/vcs.xml +0 -7
  70. data/sample_app_rails_4/.idea/workspace.xml +0 -619
  71. data/sample_app_rails_4/.rspec +0 -1
  72. data/sample_app_rails_4/.secret +0 -1
  73. data/sample_app_rails_4/Gemfile +0 -13
  74. data/sample_app_rails_4/Guardfile +0 -53
  75. data/sample_app_rails_4/LICENSE +0 -21
  76. data/sample_app_rails_4/README.md +0 -25
  77. data/sample_app_rails_4/README.nitrous.md +0 -20
  78. data/sample_app_rails_4/Rakefile +0 -6
  79. data/sample_app_rails_4/app/assets/images/rails.png +0 -0
  80. data/sample_app_rails_4/app/assets/javascripts/application.js +0 -17
  81. data/sample_app_rails_4/app/assets/javascripts/sessions.js.coffee +0 -3
  82. data/sample_app_rails_4/app/assets/javascripts/static_pages.js.coffee +0 -3
  83. data/sample_app_rails_4/app/assets/javascripts/users.js.coffee +0 -3
  84. data/sample_app_rails_4/app/assets/stylesheets/application.css +0 -13
  85. data/sample_app_rails_4/app/assets/stylesheets/custom.css.scss +0 -246
  86. data/sample_app_rails_4/app/assets/stylesheets/sessions.css.scss +0 -3
  87. data/sample_app_rails_4/app/assets/stylesheets/static_pages.css.scss +0 -3
  88. data/sample_app_rails_4/app/assets/stylesheets/users.css.scss +0 -3
  89. data/sample_app_rails_4/app/controllers/application_controller.rb +0 -6
  90. data/sample_app_rails_4/app/controllers/concerns/.keep +0 -0
  91. data/sample_app_rails_4/app/controllers/microposts_controller.rb +0 -31
  92. data/sample_app_rails_4/app/controllers/relationships_controller.rb +0 -21
  93. data/sample_app_rails_4/app/controllers/sessions_controller.rb +0 -21
  94. data/sample_app_rails_4/app/controllers/static_pages_controller.rb +0 -18
  95. data/sample_app_rails_4/app/controllers/users_controller.rb +0 -80
  96. data/sample_app_rails_4/app/helpers/application_helper.rb +0 -12
  97. data/sample_app_rails_4/app/helpers/sessions_helper.rb +0 -49
  98. data/sample_app_rails_4/app/helpers/static_pages_helper.rb +0 -2
  99. data/sample_app_rails_4/app/helpers/users_helper.rb +0 -10
  100. data/sample_app_rails_4/app/mailers/.keep +0 -0
  101. data/sample_app_rails_4/app/models/.keep +0 -0
  102. data/sample_app_rails_4/app/models/concerns/.keep +0 -0
  103. data/sample_app_rails_4/app/models/micropost.rb +0 -24
  104. data/sample_app_rails_4/app/models/relationship.rb +0 -6
  105. data/sample_app_rails_4/app/models/user.rb +0 -39
  106. data/sample_app_rails_4/app/views/layouts/_footer.html.erb +0 -13
  107. data/sample_app_rails_4/app/views/layouts/_header.html.erb +0 -31
  108. data/sample_app_rails_4/app/views/layouts/_shim.html.erb +0 -3
  109. data/sample_app_rails_4/app/views/layouts/application.html.erb +0 -22
  110. data/sample_app_rails_4/app/views/microposts/_micropost.html.erb +0 -11
  111. data/sample_app_rails_4/app/views/relationships/create.js.erb +0 -2
  112. data/sample_app_rails_4/app/views/relationships/destroy.js.erb +0 -2
  113. data/sample_app_rails_4/app/views/sessions/new.html.erb +0 -19
  114. data/sample_app_rails_4/app/views/shared/_error_messages.html.erb +0 -12
  115. data/sample_app_rails_4/app/views/shared/_feed.html.erb +0 -6
  116. data/sample_app_rails_4/app/views/shared/_feed_item.html.erb +0 -15
  117. data/sample_app_rails_4/app/views/shared/_micropost_form.html.erb +0 -7
  118. data/sample_app_rails_4/app/views/shared/_stats.html.erb +0 -15
  119. data/sample_app_rails_4/app/views/shared/_user_info.html.erb +0 -12
  120. data/sample_app_rails_4/app/views/static_pages/about.html.erb +0 -8
  121. data/sample_app_rails_4/app/views/static_pages/contact.html.erb +0 -6
  122. data/sample_app_rails_4/app/views/static_pages/help.html.erb +0 -8
  123. data/sample_app_rails_4/app/views/static_pages/home.html.erb +0 -34
  124. data/sample_app_rails_4/app/views/static_pages/show.html.erb +0 -0
  125. data/sample_app_rails_4/app/views/users/_follow.html.erb +0 -5
  126. data/sample_app_rails_4/app/views/users/_follow_form.html.erb +0 -9
  127. data/sample_app_rails_4/app/views/users/_unfollow.html.erb +0 -5
  128. data/sample_app_rails_4/app/views/users/_user.html.erb +0 -8
  129. data/sample_app_rails_4/app/views/users/edit.html.erb +0 -27
  130. data/sample_app_rails_4/app/views/users/index.html.erb +0 -10
  131. data/sample_app_rails_4/app/views/users/new.html.erb +0 -24
  132. data/sample_app_rails_4/app/views/users/show.html.erb +0 -24
  133. data/sample_app_rails_4/app/views/users/show_follow.html.erb +0 -30
  134. data/sample_app_rails_4/bin/bundle +0 -3
  135. data/sample_app_rails_4/bin/rails +0 -4
  136. data/sample_app_rails_4/bin/rake +0 -4
  137. data/sample_app_rails_4/bin/rspec +0 -16
  138. data/sample_app_rails_4/config.ru +0 -4
  139. data/sample_app_rails_4/config/application.rb +0 -28
  140. data/sample_app_rails_4/config/boot.rb +0 -4
  141. data/sample_app_rails_4/config/database.yml +0 -27
  142. data/sample_app_rails_4/config/environment.rb +0 -5
  143. data/sample_app_rails_4/config/environments/development.rb +0 -25
  144. data/sample_app_rails_4/config/environments/production.rb +0 -79
  145. data/sample_app_rails_4/config/environments/test.rb +0 -38
  146. data/sample_app_rails_4/config/initializers/active_mocker.rb +0 -12
  147. data/sample_app_rails_4/config/initializers/backtrace_silencers.rb +0 -7
  148. data/sample_app_rails_4/config/initializers/filter_parameter_logging.rb +0 -4
  149. data/sample_app_rails_4/config/initializers/inflections.rb +0 -16
  150. data/sample_app_rails_4/config/initializers/mime_types.rb +0 -5
  151. data/sample_app_rails_4/config/initializers/secret_token.rb +0 -22
  152. data/sample_app_rails_4/config/initializers/session_store.rb +0 -3
  153. data/sample_app_rails_4/config/initializers/wrap_parameters.rb +0 -14
  154. data/sample_app_rails_4/config/locales/en.yml +0 -23
  155. data/sample_app_rails_4/config/routes.rb +0 -17
  156. data/sample_app_rails_4/db/migrate/20130311191400_create_users.rb +0 -10
  157. data/sample_app_rails_4/db/migrate/20130311194153_add_index_to_users_email.rb +0 -5
  158. data/sample_app_rails_4/db/migrate/20130311201841_add_password_digest_to_users.rb +0 -5
  159. data/sample_app_rails_4/db/migrate/20130314184954_add_remember_token_to_users.rb +0 -6
  160. data/sample_app_rails_4/db/migrate/20130315015932_add_admin_to_users.rb +0 -5
  161. data/sample_app_rails_4/db/migrate/20130315175534_create_microposts.rb +0 -11
  162. data/sample_app_rails_4/db/migrate/20130315230445_create_relationships.rb +0 -13
  163. data/sample_app_rails_4/db/schema.rb +0 -51
  164. data/sample_app_rails_4/db/seeds.rb +0 -7
  165. data/sample_app_rails_4/features/signing_in.feature +0 -13
  166. data/sample_app_rails_4/features/step_definitions/authentication_steps.rb +0 -30
  167. data/sample_app_rails_4/features/support/env.rb +0 -59
  168. data/sample_app_rails_4/lib/assets/.keep +0 -0
  169. data/sample_app_rails_4/lib/tasks/.keep +0 -0
  170. data/sample_app_rails_4/lib/tasks/active_mocker.rake +0 -10
  171. data/sample_app_rails_4/lib/tasks/cucumber.rake +0 -65
  172. data/sample_app_rails_4/lib/tasks/sample_data.rake +0 -42
  173. data/sample_app_rails_4/lib/unit_logger.rb +0 -22
  174. data/sample_app_rails_4/public/404.html +0 -27
  175. data/sample_app_rails_4/public/422.html +0 -26
  176. data/sample_app_rails_4/public/500.html +0 -26
  177. data/sample_app_rails_4/public/assets/application-4962059d8f80f9bb096692bacc29c4e8.css +0 -5091
  178. data/sample_app_rails_4/public/assets/application-4962059d8f80f9bb096692bacc29c4e8.css.gz +0 -0
  179. data/sample_app_rails_4/public/assets/application-eeb856e3fe2c8f879c91d0e81d59cb40.js +0 -12952
  180. data/sample_app_rails_4/public/assets/application-eeb856e3fe2c8f879c91d0e81d59cb40.js.gz +0 -0
  181. data/sample_app_rails_4/public/assets/glyphicons-halflings-c806376f05e4ccabe2c5315a8e95667c.png +0 -0
  182. data/sample_app_rails_4/public/assets/glyphicons-halflings-white-62b67d9edee3db90d18833087f848d6e.png +0 -0
  183. data/sample_app_rails_4/public/assets/manifest-802de9eb1c853769101852422b620883.json +0 -1
  184. data/sample_app_rails_4/public/assets/rails-231a680f23887d9dd70710ea5efd3c62.png +0 -0
  185. data/sample_app_rails_4/public/favicon.ico +0 -0
  186. data/sample_app_rails_4/public/robots.txt +0 -5
  187. data/sample_app_rails_4/script/cucumber +0 -10
  188. data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +0 -1038
  189. data/sample_app_rails_4/spec/factories.rb +0 -17
  190. data/sample_app_rails_4/spec/micropost_mock_spec.rb +0 -145
  191. data/sample_app_rails_4/spec/mocks/micropost_mock.rb +0 -153
  192. data/sample_app_rails_4/spec/mocks/relationship_mock.rb +0 -151
  193. data/sample_app_rails_4/spec/mocks/user_mock.rb +0 -215
  194. data/sample_app_rails_4/spec/reload_spec.rb +0 -45
  195. data/sample_app_rails_4/spec/spec_helper.rb +0 -46
  196. data/sample_app_rails_4/spec/user_mock_spec.rb +0 -197
  197. data/sample_app_rails_4/vendor/assets/javascripts/.keep +0 -0
  198. data/sample_app_rails_4/vendor/assets/stylesheets/.keep +0 -0
  199. data/spec/lib/acitve_mock/queriable_spec.rb +0 -207
  200. data/spec/lib/active_mocker/db_to_ruby_type_spec.rb +0 -124
  201. data/spec/lib/active_mocker/generate_spec.rb +0 -40
  202. data/spec/lib/active_mocker/loaded_mocks_spec.rb +0 -167
  203. data/spec/lib/active_mocker/logger_spec.rb +0 -32
  204. data/spec/lib/active_mocker/model_reader_spec.rb +0 -206
  205. data/spec/lib/active_mocker/model_schema/generate_spec.rb +0 -111
  206. data/spec/lib/active_mocker/model_schema_spec.rb +0 -145
  207. data/spec/lib/active_mocker/schema_reader_spec.rb +0 -109
  208. data/spec/lib/model.rb +0 -43
  209. data/spec/lib/person.rb +0 -9
  210. data/spec/lib/readme_spec.rb +0 -199
  211. data/spec/lib/reparameterize_spec.rb +0 -202
  212. data/spec/lib/schema.rb +0 -40
  213. data/spec/unit_logger.rb +0 -24
@@ -1,4 +1,5 @@
1
- module ActiveMock
1
+ module ActiveMocker
2
+ module Mock
2
3
  class NextId
3
4
 
4
5
  def initialize(records)
@@ -7,12 +8,16 @@ class NextId
7
8
 
8
9
  def next
9
10
  return 1 if max_record.nil?
10
- max_record.id.succ if max_record.id.is_a?(Numeric)
11
+ return max_record.id.succ if max_record.id.is_a?(Numeric)
12
+ raise IdNotNumber
11
13
  end
12
14
 
15
+ private
16
+
13
17
  def max_record
14
18
  @max_record ||= @records.max { |a, b| a.id <=> b.id }
15
19
  end
16
20
 
21
+ end
17
22
  end
18
23
  end
@@ -1,4 +1,5 @@
1
- module ActiveMock
1
+ module ActiveMocker
2
+ module Mock
2
3
  class ObjectInspect
3
4
 
4
5
  def initialize(class_name, attributes)
@@ -37,5 +38,6 @@ class ObjectInspect
37
38
  end
38
39
  end
39
40
 
41
+ end
40
42
  end
41
43
  end
@@ -1,16 +1,8 @@
1
- module ActiveMock
1
+ module ActiveMocker
2
+ module Mock
2
3
 
3
4
  module Queries
4
5
 
5
- def self.included(klass)
6
- @included = klass
7
- end
8
-
9
- def self.included_klass
10
- return Relation if @included.name == 'ActiveMocker::Base'
11
- @included
12
- end
13
-
14
6
  class Find
15
7
 
16
8
  def initialize(record)
@@ -28,47 +20,47 @@ module ActiveMock
28
20
 
29
21
  class WhereNotChain
30
22
 
31
- def initialize(collection)
32
- @collection = collection
23
+ def initialize(collection, parent_class)
24
+ @collection = collection
25
+ @parent_class = parent_class
33
26
  end
34
27
 
35
28
  def not(options={})
36
- Queries.included_klass.new(@collection.reject do |record|
29
+ @parent_class.call(@collection.reject do |record|
37
30
  Find.new(record).is_of(options)
38
31
  end)
39
32
  end
40
33
  end
41
34
 
42
- def delete_all
43
- all.map(&:delete)
35
+ def delete_all(options=nil)
36
+ if options.nil?
37
+ to_a.map(&:delete)
38
+ return to_a.clear
39
+ end
40
+ where(options).map { |r| r.delete }.count
44
41
  end
45
42
 
46
43
  def destroy_all
47
44
  delete_all
48
45
  end
49
46
 
50
- def all
51
- Queries.included_klass.new( to_a || [] )
52
- end
53
-
54
47
  def where(options=nil)
55
- return WhereNotChain.new(all) if options.nil?
56
- Queries.included_klass.new(all.select do |record|
48
+ return WhereNotChain.new(all, method(:new_relation)) if options.nil?
49
+ new_relation(to_a.select do |record|
57
50
  Find.new(record).is_of(options)
58
51
  end)
59
52
  end
60
53
 
61
54
  def find(ids)
62
- ids_array = [*ids]
63
- results = ids_array.map do |id|
55
+ results = [*ids].map do |id|
64
56
  where(id: id).first
65
57
  end
66
- return Queries.included_klass.new(results) if ids.class == Array
58
+ return new_relation(results) if ids.class == Array
67
59
  results.first
68
60
  end
69
61
 
70
62
  def update_all(options)
71
- all.each{ |i| i.update(options)}
63
+ all.each { |i| i.update(options) }
72
64
  end
73
65
 
74
66
  def find_by(options = {})
@@ -82,7 +74,7 @@ module ActiveMock
82
74
  end
83
75
 
84
76
  def limit(num)
85
- Queries.included_klass.new(all.take(num))
77
+ new_relation(all.take(num))
86
78
  end
87
79
 
88
80
  def sum(key)
@@ -97,7 +89,7 @@ module ActiveMock
97
89
  end
98
90
 
99
91
  def minimum(key)
100
- values_by_key(key).min_by{|i| i }
92
+ values_by_key(key).min_by { |i| i }
101
93
  end
102
94
 
103
95
  def maximum(key)
@@ -105,11 +97,15 @@ module ActiveMock
105
97
  end
106
98
 
107
99
  def order(key)
108
- Queries.included_klass.new(all.sort_by { |item| item.send(key) })
100
+ new_relation(all.sort_by { |item| item.send(key) })
109
101
  end
110
102
 
111
103
  def reverse_order
112
- Queries.included_klass.new(to_a.reverse)
104
+ new_relation(to_a.reverse)
105
+ end
106
+
107
+ def all
108
+ new_relation(to_a || [])
113
109
  end
114
110
 
115
111
  private
@@ -118,6 +114,13 @@ module ActiveMock
118
114
  all.map { |obj| obj.send(key) }
119
115
  end
120
116
 
117
+ def new_relation(collection)
118
+ duped = self.dup
119
+ duped.collection = collection
120
+ duped
121
+ end
122
+
121
123
  end
122
124
 
125
+ end
123
126
  end
@@ -0,0 +1,62 @@
1
+ module ActiveMocker
2
+ module Mock
3
+ class Records
4
+
5
+ extend Forwardable
6
+ def_delegators :records, :<<, :count, :length, :to_a
7
+
8
+ attr_reader :records, :record_index
9
+ private :records, :record_index
10
+ def initialize(records = [])
11
+ @records ||= records
12
+ @record_index ||= {}
13
+ end
14
+
15
+ def insert(record)
16
+ record.attributes[:id] ||= next_id
17
+ validate_unique_id(record)
18
+ add_to_record_index(record)
19
+ records << record
20
+ end
21
+
22
+ def delete(record)
23
+ raise RecordNotFound, 'Record has not been created.' if new_record?(record)
24
+ record_index.delete("#{record.id}")
25
+ index = records.index(record)
26
+ records.delete_at(index)
27
+ end
28
+
29
+ def exists?(record)
30
+ records.include?(record)
31
+ end
32
+
33
+ def new_record?(record)
34
+ !exists?(record)
35
+ end
36
+
37
+ def persisted?(id)
38
+ records.map(&:id).include?(id)
39
+ end
40
+
41
+ def reset
42
+ record_index.clear
43
+ records.clear
44
+ end
45
+
46
+ private
47
+
48
+ def next_id
49
+ NextId.new(records).next
50
+ end
51
+
52
+ def add_to_record_index(record)
53
+ record_index.merge!({record.id.to_s => records.length})
54
+ end
55
+
56
+ def validate_unique_id(record)
57
+ raise IdError, "Duplicate ID found for record #{record.inspect}" if record_index.has_key?(record.id.to_s)
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -1,8 +1,9 @@
1
- module ActiveMock
2
-
1
+ module ActiveMocker
2
+ module Mock
3
3
  class Relation < Collection
4
+
4
5
  include Queries
5
6
 
6
7
  end
7
-
8
+ end
8
9
  end
@@ -0,0 +1,39 @@
1
+ module ActiveMocker
2
+ module Mock
3
+ module TemplateMethods
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def attributes
12
+ raise Mock::Unimplemented
13
+ end
14
+
15
+ def types
16
+ raise Mock::Unimplemented
17
+ end
18
+
19
+ def associations
20
+ raise Mock::Unimplemented
21
+ end
22
+
23
+ def mocked_class
24
+ raise Mock::Unimplemented
25
+ end
26
+
27
+ def attribute_names
28
+ raise Mock::Unimplemented
29
+ end
30
+
31
+ def primary_key
32
+ raise Mock::Unimplemented
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -1,10 +1,16 @@
1
- require 'active_mocker/active_mock'
1
+ require 'active_mocker/mock'
2
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 + @mock_append_name %> < ActiveMock::Base
5
- <% constants.each do |constant| %>
4
+ class <%= class_name + @mock_append_name %> < ActiveMocker::Mock::Base
5
+ <% constants.each do |constant| -%>
6
6
  <%= constant.first %> = <%= constant.last.inspect %>
7
7
  <% end -%>
8
+ <% modules[:included].each do |constant| -%>
9
+ prepend <%= constant %>
10
+ <% end -%>
11
+ <% modules[:extended].each do |constant| -%>
12
+ extend <%= constant %>
13
+ <% end -%>
8
14
 
9
15
  class << self
10
16
 
@@ -13,29 +19,17 @@ class <%= class_name + @mock_append_name %> < ActiveMock::Base
13
19
  end
14
20
 
15
21
  def types
16
- @types ||= <%= types_hash %>
22
+ @types ||= ActiveMocker::Mock::HashProcess.new(<%= types_hash %>, method(:build_type))
17
23
  end
18
24
 
19
25
  def associations
20
26
  @associations ||= <%= associations %>
21
27
  end
22
28
 
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
29
  def mocked_class
32
30
  '<%= class_name %>'
33
31
  end
34
32
 
35
- def column_names
36
- attribute_names
37
- end
38
-
39
33
  def attribute_names
40
34
  @attribute_names ||= <%= attribute_names %>
41
35
  end
@@ -90,7 +84,7 @@ class <%= class_name + @mock_append_name %> < ActiveMock::Base
90
84
  <% association = relation_find(:name, meth.name).first -%>
91
85
  <% if association -%>
92
86
  association = classes('<%= association.class_name %>').try(:create,attributes, &block)
93
- write_association(:<%= meth.name %>, nil) unless association.nil?
87
+ write_association(:<%= meth.name %>, association) unless association.nil?
94
88
  <% end -%>
95
89
  end
96
90
  alias_method :create_<%= meth.name %>!, :create_<%= meth.name %>
@@ -115,7 +109,7 @@ class <%= class_name + @mock_append_name %> < ActiveMock::Base
115
109
  def create_<%= meth.name %>(attributes={}, &block)
116
110
  <% association = relation_find(:name, meth.name).first -%>
117
111
  <% if association -%>
118
- write_association(:<%= meth.name %>,classes('<%= association.class_name %>').create(attributes, &block)) if classes('<%= association.class_name %>')
112
+ write_association(:<%= meth.name %>, classes('<%= association.class_name %>').new(attributes, &block)) if classes('<%= association.class_name %>')
119
113
  <% end -%>
120
114
  end
121
115
  alias_method :create_<%= meth.name %>!, :create_<%= meth.name %>
@@ -124,40 +118,60 @@ class <%= class_name + @mock_append_name %> < ActiveMock::Base
124
118
  <%= '# has_many' unless has_many.empty? -%>
125
119
  <% has_many.each do |meth| %>
126
120
  def <%= meth.name %>
127
- @associations[:<%= meth.name %>] ||= ActiveMock::HasMany.new([],'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>'))
121
+ @associations[:<%= meth.name %>] ||= ActiveMocker::Mock::HasMany.new([],'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>'))
128
122
  end
129
123
 
130
124
  def <%= meth.name %>=(val)
131
- @associations[:<%= meth.name %>] ||= ActiveMock::HasMany.new(val,'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>'))
125
+ @associations[:<%= meth.name %>] ||= ActiveMocker::Mock::HasMany.new(val,'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>'))
132
126
  end
133
127
  <% end -%>
134
128
  <%= '# has_and_belongs_to_many' unless has_and_belongs_to_many.empty? -%>
135
129
  <% has_and_belongs_to_many.each do |meth| %>
136
130
  def <%= meth.name %>
137
- @associations[:<%= meth.name %>] ||= ActiveMock::HasAndBelongsToMany.new
131
+ @associations[:<%= meth.name %>] ||= ActiveMocker::Mock::HasAndBelongsToMany.new([])
138
132
  end
139
133
 
140
134
  def <%= meth.name %>=(val)
141
- @associations[:<%= meth.name %>] = ActiveMock::HasAndBelongsToMany.new(val)
135
+ @associations[:<%= meth.name %>] = ActiveMocker::Mock::HasAndBelongsToMany.new(val)
142
136
  end
143
137
  <% end -%>
144
138
 
139
+ module Scopes
140
+
141
+ <% scope_methods.each do |method| -%>
142
+ def <%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %>
143
+ ActiveMocker::LoadedMocks.find('<%= class_name %>').send(:call_mock_method, '<%= method.name %>', <%= method.arguments.passable %>)
144
+ end
145
+
146
+ <% end -%>
147
+ end
148
+
149
+ extend Scopes
150
+
151
+ class ScopeRelation < ActiveMocker::Mock::Association
152
+ include <%= class_name + @mock_append_name %>::Scopes
153
+ end
154
+
155
+ private
156
+
157
+ def self.new_relation(collection)
158
+ <%= class_name + @mock_append_name %>::ScopeRelation.new(collection)
159
+ end
160
+
161
+ public
162
+
145
163
  ##################################
146
- # Model Methods getter/setters #
164
+ # Model Methods #
147
165
  ##################################
148
166
 
149
167
  <% instance_methods.each do |method| %>
150
- def <%= method.name %>(<%= method.arguments %>)
151
- block = model_instance_methods['<%= method.name %>']
152
- self.class.is_implemented(block, '#<%= method.name %>')
153
- block.call(*[<%= method.arguments.passable %>])
168
+ def <%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %>
169
+ call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
154
170
  end
155
171
  <% end -%>
156
172
  <% class_methods.each do |method| %>
157
- def self.<%= method.name %>(<%= method.arguments %>)
158
- block = model_class_methods['<%= method.name %>']
159
- is_implemented(block, '::<%= method.name %>')
160
- block.call(*[<%= method.arguments.passable %>])
173
+ def self.<%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %>
174
+ call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
161
175
  end
162
176
  <% end -%>
163
177
 
@@ -25,6 +25,12 @@ module ActiveMocker
25
25
  begin
26
26
  m = Module.new
27
27
  m.module_eval(read_file, file_path)
28
+ rescue SyntaxError => e
29
+ Logger.error "ActiveMocker :: Error loading Model: #{model_name} \n \t\t\t\t\t\t\t\t`#{e}` \n"
30
+ puts "ActiveMocker :: Error loading Model: #{model_name} \n \t\t\t\t\t\t\t\t`#{e}` \n"
31
+ Logger.error "\t\t\t\t\t\t\t\t #{file_path}\n"
32
+ puts "\t\t\t\t\t\t\t\t #{file_path}\n"
33
+ failure = true
28
34
  rescue Exception => e
29
35
  Logger.error "ActiveMocker :: Error loading Model: #{model_name} \n \t\t\t\t\t\t\t\t`#{e}` \n"
30
36
  Logger.error "\t\t\t\t\t\t\t\t #{file_path}\n"
@@ -34,6 +40,10 @@ module ActiveMocker
34
40
  return false
35
41
  end
36
42
 
43
+ def real
44
+ model_name.classify.constantize
45
+ end
46
+
37
47
  def read_file
38
48
  file_reader.read(file_path)
39
49
  end
@@ -46,6 +56,16 @@ module ActiveMocker
46
56
  klass.methods(false)
47
57
  end
48
58
 
59
+ def scopes
60
+ klass.get_named_scopes
61
+ end
62
+
63
+ def scopes_with_arguments
64
+ scopes.map do |name, proc|
65
+ {name => proc.parameters, :proc => proc}
66
+ end
67
+ end
68
+
49
69
  def class_methods_with_arguments
50
70
  class_methods.map do |m|
51
71
  {m => klass.method(m).parameters }
@@ -111,6 +131,15 @@ module ActiveMocker
111
131
  const
112
132
  end
113
133
 
134
+ def modules
135
+ {included: process_module_names(klass._included),
136
+ extended: process_module_names(klass._extended)}
137
+ end
138
+
139
+ def process_module_names(names)
140
+ names.reject { |m| /#{klass.inspect}/ =~ m.name }.map(&:inspect)
141
+ end
142
+
114
143
  end
115
144
 
116
145
  end