clean-mock 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/clean-mock/base.rb +23 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65e82efc44b05437a2a4cf194c21e7033a4d520a776325d9058719f380befc99
|
4
|
+
data.tar.gz: dd0d2f1f5244c26861b79da0873d09d168353090c25efed9b99bf49ed1eb885f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b82714d1d2103cb269decd5f032723132a3129ad64315578d77253428ad41930d5c7aa1eaf5d052f9e24654affb00286d12f63a0c7c7f55224ffeb54f188ad6
|
7
|
+
data.tar.gz: 91b29dbc810dc6936aa7a8b180a4bb5b8ae43a8656165f6604cdc697a759528bedb5046aec9bbe67db251c2cd34ebb5488bbf4a65acab61bf48f7ba47635d5ec
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/clean-mock/base.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class CleanMock
|
2
|
-
attr_reader :
|
2
|
+
attr_reader :model
|
3
3
|
|
4
4
|
@@fetched ||= {}
|
5
5
|
@@mock_data ||= {}
|
@@ -7,7 +7,7 @@ class CleanMock
|
|
7
7
|
@@classes ||= {}
|
8
8
|
|
9
9
|
class << self
|
10
|
-
# defined mocked
|
10
|
+
# defined mocked model
|
11
11
|
def define name, opts={}, &block
|
12
12
|
@@mock_data[name] = [block, opts]
|
13
13
|
end
|
@@ -16,15 +16,15 @@ class CleanMock
|
|
16
16
|
build(*args).attributes.select{ |k,v| v.present? }
|
17
17
|
end
|
18
18
|
|
19
|
-
# create a new
|
19
|
+
# create a new model, no save
|
20
20
|
def build *args
|
21
|
-
new(*args).
|
21
|
+
new(*args).model
|
22
22
|
end
|
23
23
|
|
24
24
|
# save if possible
|
25
25
|
def create *args
|
26
|
-
build(*args).tap do |
|
27
|
-
|
26
|
+
build(*args).tap do |model|
|
27
|
+
model.save if model.respond_to?(:save)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -42,9 +42,9 @@ class CleanMock
|
|
42
42
|
@kind = args.shift
|
43
43
|
@traits = args
|
44
44
|
|
45
|
-
block, mock_opts = @@mock_data[@kind] || raise(ArgumentError, 'Mock
|
45
|
+
block, mock_opts = @@mock_data[@kind] || raise(ArgumentError, 'Mock model "%s" not defined' % @kind)
|
46
46
|
|
47
|
-
@
|
47
|
+
@model =
|
48
48
|
case mock_opts[:class]
|
49
49
|
when FalseClass
|
50
50
|
# define :foo, class: false
|
@@ -66,25 +66,25 @@ class CleanMock
|
|
66
66
|
mock_opts[:class].new
|
67
67
|
end
|
68
68
|
|
69
|
-
if @
|
70
|
-
instance_exec @
|
69
|
+
if @model
|
70
|
+
instance_exec @model, opts, &block
|
71
71
|
else
|
72
|
-
@
|
72
|
+
@model = instance_exec opts, &block
|
73
73
|
end
|
74
74
|
|
75
75
|
raise 'Trait [%s] not found' % @traits.join(', ') if @traits.first
|
76
76
|
end
|
77
77
|
|
78
|
-
# block to execute and modify
|
78
|
+
# block to execute and modify model
|
79
79
|
def trait name, &block
|
80
80
|
if @traits.delete(name)
|
81
|
-
instance_exec(@
|
81
|
+
instance_exec(@model, &block)
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
85
|
# define or overlod current instance method
|
86
86
|
def func name, &block
|
87
|
-
@
|
87
|
+
@model.define_singleton_method(name, &block)
|
88
88
|
end
|
89
89
|
|
90
90
|
# simple sequence generator by name
|
@@ -93,4 +93,13 @@ class CleanMock
|
|
93
93
|
@@sequence[name] ||= start || 0
|
94
94
|
@@sequence[name] += 1
|
95
95
|
end
|
96
|
+
|
97
|
+
# helper to create and link model
|
98
|
+
# create :org -> @model.org_id = mock.create(org).id
|
99
|
+
def create name, field=nil
|
100
|
+
field ||= name.to_s.singularize + '_id'
|
101
|
+
new_model = CleanMock.create(name)
|
102
|
+
@model.send('%s=' % field, new_model.id)
|
103
|
+
new_model
|
104
|
+
end
|
96
105
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clean-mock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Clean and efficient ruby testing object creation helper/mocking library
|
14
14
|
with interface similar to FactoryBot
|