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 +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +2 -1
- data/lib/active_mocker.rb +2 -0
- data/lib/active_mocker/attribute.rb +2 -0
- data/lib/active_mocker/attribute_types/enum.rb +64 -0
- data/lib/active_mocker/mock/base.rb +1 -1
- data/lib/active_mocker/mock_creator.rb +52 -3
- data/lib/active_mocker/mock_template/_attributes.erb +3 -3
- data/lib/active_mocker/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9696a956e1bea0e39fab858b45697d76cef725a7
|
|
4
|
+
data.tar.gz: e71122a7b31abd042d9ad33d6bfba2d5afc16a0b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
@@ -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 :
|
|
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
|
-
@
|
|
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] =
|
|
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
|
-
|
|
4
|
+
<%= meth.attribute_reader %>
|
|
5
5
|
end
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
def <%= meth.name %>=(val)
|
|
9
|
-
|
|
9
|
+
<%= meth.attribute_writer %>
|
|
10
10
|
end
|
|
11
|
-
<% end -%>
|
|
11
|
+
<% 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.
|
|
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-
|
|
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
|