active_mocker 2.4.0.pre1 → 2.4.0.pre2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d76ea3471b67a416239349d655e5e3e9061e2cf3
4
- data.tar.gz: 1f10949f52554aa237ae3cf60ed7e7b56cc0c2fd
3
+ metadata.gz: 9696a956e1bea0e39fab858b45697d76cef725a7
4
+ data.tar.gz: e71122a7b31abd042d9ad33d6bfba2d5afc16a0b
5
5
  SHA512:
6
- metadata.gz: 357f966e759449cf9ae50fba27ac6f319ddee7d06f719a3c5311c33666f6b385ed0291238c2f516b20d3baf9d171b96c6ae0030865839ff94379b0039882423e
7
- data.tar.gz: f056acf32872ddcb7ec7c3f349241c7a93c459b8abf70cc14b3da61de3d1f0f5e982c4d97ddd33bc69d6ba9775f99ebdf38dd5f7b8e49bafb064eec0eef0cdc2
6
+ metadata.gz: 18bb1cf82a31fc4a39726f3eda57c8bd1466ae608c58f03f012774b2e2fafc2b58351c6d48bc8f623c7271553c9d934115e83d4a517afc17bf1fabf7b7cbe3e8
7
+ data.tar.gz: 2155bcea9852bb130941a57fd0736b0bd4bdc856902627afbb4f33356b6c317acd6ba6907b489201ecb04914a3e407dd4b9744ee520c23398d193c47f1a417ec
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## 2.4.0.pre2 - 2016-10-17
5
+ ### Enhancement
6
+ - ActiveRecord Enum support
7
+ Compatible with ActiveRecord versions 5 and 4. Generating using different version of ActiveRecord creates different mock files.
8
+
4
9
  ## 2.4.0.pre1 - 2016-10-13
5
10
  ### Enhancement
6
11
  - Option to delete all records before each example
@@ -21,7 +26,7 @@ All notable changes to this project will be documented in this file.
21
26
  ```ruby
22
27
  ActiveMocker::LoadedMocks.features.enable(:timestamps)
23
28
  ```
24
-
29
+
25
30
  ## 2.3.3 - 2016-10-13
26
31
  ### Enhancement
27
32
  - Auto stubbing of ActiveRecord::RecordNotFound with requiring "active_mocker/rspec_helper"
data/README.md CHANGED
@@ -362,7 +362,7 @@ See [Documentation](http://rdoc.info/github/zeisler/active_mocker/master/ActiveM
362
362
  * attributes
363
363
  * update
364
364
  * save/save!
365
- * write_attribute/read_attribute - (protected, can be used within modules)
365
+ * write_attribute/read_attribute
366
366
  * delete
367
367
  * new_record?
368
368
  * persisted?
@@ -373,6 +373,7 @@ See [Documentation](http://rdoc.info/github/zeisler/active_mocker/master/ActiveM
373
373
  * slice
374
374
  * attribute_alias?
375
375
  * alias_attributes
376
+ * touch
376
377
 
377
378
  **has_one/belongs_to/has_many**
378
379
 
data/lib/active_mocker.rb CHANGED
@@ -28,3 +28,5 @@ require "active_mocker/mock_creator"
28
28
  require "active_mocker/error_object"
29
29
  require "active_mocker/display_errors"
30
30
  require "active_mocker/generate"
31
+ require "active_mocker/attribute_types/enum"
32
+ require "active_mocker/attribute"
@@ -0,0 +1,2 @@
1
+ ActiveRecordSchemaScrapper::Attribute.attribute :attribute_writer, String
2
+ ActiveRecordSchemaScrapper::Attribute.attribute :attribute_reader, String
@@ -0,0 +1,64 @@
1
+ module ActiveMocker
2
+ module AttributeTypes
3
+ class Enum < Virtus::Attribute
4
+ class << self
5
+ def build(db_value_type:, table_name:, attribute:, enums:, ignore_value: false)
6
+ klass = Class.new(ActiveMocker::AttributeTypes::Enum)
7
+ klass.table_name = table_name.to_sym
8
+ klass.attribute = attribute.to_sym
9
+ klass.ignore_value = ignore_value
10
+ enums = if enums.is_a?(Array)
11
+ enums.each_with_object({}).with_index { |(k, h), i| h[k] = i }
12
+ else
13
+ enums
14
+ end
15
+ klass.key_type = String
16
+ klass.db_value_type = db_value_type
17
+ klass.enums = Hash[enums.map do |k, v|
18
+ [Virtus::Attribute.build(klass.key_type).coerce(k),
19
+ Virtus::Attribute.build(klass.db_value_type).coerce(v)]
20
+ end]
21
+ klass
22
+ end
23
+
24
+ def to_s
25
+ "ActiveMocker::AttributeTypes::Enum.build(ignore_value: #{ignore_value}, db_value_type: #{db_value_type}, table_name: :#{table_name}, attribute: :#{attribute}, enums: #{enums.inspect})"
26
+ end
27
+
28
+ attr_accessor :enums, :table_name, :attribute, :db_value_type, :key_type, :ignore_value
29
+ end
30
+
31
+ def coerce(key)
32
+ return if key.nil?
33
+ coerced_key = key_type.coerce(key)
34
+ if key && self.class.enums.key?(coerced_key)
35
+ if self.class.ignore_value
36
+ coerced_key
37
+ else
38
+ get_value(key)
39
+ end
40
+ else
41
+ raise ArgumentError, "'#{coerced_key}' is not a valid #{self.class.attribute}"
42
+ end
43
+ end
44
+
45
+ def get_key(value)
46
+ self.class.enums.invert[db_value_type.coerce(value)]
47
+ end
48
+
49
+ def get_value(key)
50
+ self.class.enums[key_type.coerce(key)]
51
+ end
52
+
53
+ def key_type
54
+ Virtus::Attribute.build(self.class.key_type)
55
+ end
56
+
57
+ def db_value_type
58
+ Virtus::Attribute.build(self.class.db_value_type)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ ActiveRecordSchemaScrapper::Attributes.register_type(name: :enum, klass: String)
@@ -362,7 +362,7 @@ module ActiveMocker
362
362
  @associations[attr.to_sym] = value
363
363
  end
364
364
 
365
- protected :read_attribute, :write_attribute, :read_association, :write_association
365
+ protected :read_association, :write_association
366
366
  end
367
367
 
368
368
  include PropertiesGetterAndSetter
@@ -217,25 +217,74 @@ module ActiveMocker
217
217
 
218
218
  module Attributes
219
219
  def attributes
220
- @attribute ||= begin
220
+ @attributes ||= begin
221
221
  a = schema_scrapper.attributes.to_a
222
222
  a << primary_key unless a.any? { |aa| aa.name == "id" }
223
+ a.map(&method(:process_attr))
223
224
  a
224
225
  end
225
226
  end
227
+
228
+ def process_attr(attr)
229
+ enums = enums(attr.name)
230
+ attr.default = Virtus::Attribute.build(attr.type).coerce(attr.default)
231
+ attr.attribute_writer = "write_attribute(:#{attr.name}, val)"
232
+ attr.attribute_reader = "read_attribute(:#{attr.name})"
233
+
234
+ unless enums.empty?
235
+ enum_type = ActiveMocker::AttributeTypes::Enum.build(
236
+ enums: enums,
237
+ table_name: table_name,
238
+ attribute: attr.name,
239
+ db_value_type: attr.type,
240
+ )
241
+ if ActiveRecord::VERSION::MAJOR == 5
242
+ enum_type.ignore_value = true
243
+ attr.type = enum_type
244
+ if attr.default
245
+ attr.default = Virtus::Attribute.build(enum_type).get_key(attr.default)
246
+ end
247
+ elsif ActiveRecord::VERSION::MAJOR == 4
248
+ attr.attribute_writer = "@#{attr.name}_enum_type ||= Virtus::Attribute.build(#{enum_type})\nwrite_attribute(:#{attr.name}, @#{attr.name}_enum_type.coerce(val))"
249
+ attr.attribute_reader = "@#{attr.name}_enum_type ||= Virtus::Attribute.build(#{enum_type})\n@#{attr.name}_enum_type.get_key(read_attribute(:#{attr.name}))"
250
+ if attr.default
251
+ attr.default = Virtus::Attribute.build(attr.type).coerce(attr.default)
252
+ end
253
+ end
254
+ attr
255
+ end
256
+ end
257
+
258
+ def enums(attribute)
259
+ @enums ||= begin
260
+ raw_enums = class_introspector
261
+ .class_macros
262
+ .select { |hash| hash.key?(:enum) }
263
+ if raw_enums
264
+ raw_enums
265
+ .map { |hash| hash[:enum].flatten.first }
266
+ .each_with_object({}) { |v, h| h.merge!(v) }
267
+ else
268
+ {}
269
+ end
270
+ end
271
+
272
+ @enums.fetch(attribute.to_sym, {})
273
+ end
226
274
  end
227
275
 
228
276
  module ClassMethods
229
277
  include Attributes
230
278
 
231
279
  def attributes_with_defaults
280
+ types_hash
232
281
  attributes.each_with_object({}) do |attr, hash|
233
- hash[attr.name] = Virtus::Attribute.build(attr.type).coerce(attr.default)
282
+ hash[attr.name] = attr.default
234
283
  end
235
284
  end
236
285
 
237
286
  def types_hash
238
- attributes.each_with_object(HashNewStyle.new) do |attr, types|
287
+ @types_hash ||= attributes.each_with_object(HashNewStyle.new) do |attr, types|
239
288
  types[attr.name] = attr.type.to_s
240
289
  end.inspect
241
290
  end
@@ -1,11 +1,11 @@
1
1
  # _attributes.erb
2
2
  <% attributes.each do |meth| -%>
3
3
  def <%= meth.name %>
4
- read_attribute(:<%= meth.name %>)
4
+ <%= meth.attribute_reader %>
5
5
  end
6
6
 
7
7
 
8
8
  def <%= meth.name %>=(val)
9
- write_attribute(:<%= meth.name %>, val)
9
+ <%= meth.attribute_writer %>
10
10
  end
11
- <% end -%>
11
+ <% end -%>
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ActiveMocker
3
- VERSION = "2.4.0.pre1"
3
+ VERSION = "2.4.0.pre2"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_mocker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0.pre1
4
+ version: 2.4.0.pre2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-14 00:00:00.000000000 Z
11
+ date: 2016-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -184,6 +184,8 @@ files:
184
184
  - LICENSE.txt
185
185
  - README.md
186
186
  - lib/active_mocker.rb
187
+ - lib/active_mocker/attribute.rb
188
+ - lib/active_mocker/attribute_types/enum.rb
187
189
  - lib/active_mocker/config.rb
188
190
  - lib/active_mocker/deprecated_components/mock_abilities.rb
189
191
  - lib/active_mocker/deprecated_components/rspec.rb