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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/lib/clean-mock/base.rb +23 -14
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1306823d00dc26a9c0aa3c26e0d5596e45dcb2d7968a49430b5dca26a06161bb
4
- data.tar.gz: f200b3a4cb2a3f12ac4fa9c07ce20328be4d5abbdeddab65f81215ac46878f1b
3
+ metadata.gz: 65e82efc44b05437a2a4cf194c21e7033a4d520a776325d9058719f380befc99
4
+ data.tar.gz: dd0d2f1f5244c26861b79da0873d09d168353090c25efed9b99bf49ed1eb885f
5
5
  SHA512:
6
- metadata.gz: c289dbf175cd53ef97a84ac44f85de9b5315ebba0f057f2c8b243124efadf816e24020beaf85fb0367657fb014fc2f84f72f992eb0b8780f50fa4cf5d684655e
7
- data.tar.gz: 21d616edc288a3edb4e477bdf371ab8884e89232ea2f6cc4a7928042b0ee8df5db6bb46e413b386e77d5ef31758d8cf68911670df7b512d43ae30acd7969a0a3
6
+ metadata.gz: 6b82714d1d2103cb269decd5f032723132a3129ad64315578d77253428ad41930d5c7aa1eaf5d052f9e24654affb00286d12f63a0c7c7f55224ffeb54f188ad6
7
+ data.tar.gz: 91b29dbc810dc6936aa7a8b180a4bb5b8ae43a8656165f6604cdc697a759528bedb5046aec9bbe67db251c2cd34ebb5488bbf4a65acab61bf48f7ba47635d5ec
data/.version CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,5 +1,5 @@
1
1
  class CleanMock
2
- attr_reader :object
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 object
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 object, no save
19
+ # create a new model, no save
20
20
  def build *args
21
- new(*args).object
21
+ new(*args).model
22
22
  end
23
23
 
24
24
  # save if possible
25
25
  def create *args
26
- build(*args).tap do |object|
27
- object.save if object.respond_to?(:save)
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 object "%s" not defined' % @kind)
45
+ block, mock_opts = @@mock_data[@kind] || raise(ArgumentError, 'Mock model "%s" not defined' % @kind)
46
46
 
47
- @object =
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 @object
70
- instance_exec @object, opts, &block
69
+ if @model
70
+ instance_exec @model, opts, &block
71
71
  else
72
- @object = instance_exec opts, &block
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 object
78
+ # block to execute and modify model
79
79
  def trait name, &block
80
80
  if @traits.delete(name)
81
- instance_exec(@object, &block)
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
- @object.define_singleton_method(name, &block)
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.1.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-03 00:00:00.000000000 Z
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