Class: CleanMock
- Inherits:
-
Object
- Object
- CleanMock
- Defined in:
- lib/clean-mock/base.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
- .attributes_for(*args) ⇒ Object
-
.build(*args) ⇒ Object
create a new model without save.
-
.create(*args) ⇒ Object
save if responds to save.
-
.define(name, opts = {}, &block) ⇒ Object
define a mocked model factory.
-
.fetch(*args, &block) ⇒ Object
create only once - cached on args identity.
Instance Method Summary collapse
-
#after_create(&block) ⇒ Object
callback fired after ‘create` / `fetch` saves the model (not on `build`).
-
#create(name, field = nil) ⇒ Object
helper to create and link model create :org -> @model.org_id = mock.create(org).id.
- #create_mock ⇒ Object
-
#func(name, &block) ⇒ Object
define or overload current instance method.
-
#initialize(*args) ⇒ CleanMock
constructor
A new instance of CleanMock.
-
#sequence(name = nil, start = nil) ⇒ Object
simple sequence generator by name.
-
#trait(name, &block) ⇒ Object
block to execute and modify model.
Constructor Details
#initialize(*args) ⇒ CleanMock
Returns a new instance of CleanMock.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/clean-mock/base.rb', line 38 def initialize *args opts = args.last.is_a?(Hash) ? args.pop : {} @kind = args.shift @traits = args block, mock_opts = @@mock_data[@kind] || raise( ArgumentError, 'Mock model "%s" not defined. Known: %s' % [@kind, @@mock_data.keys.join(', ')] ) @model = case mock_opts[:class] when false # define :foo, class: false nil when nil # define :foo @kind.to_s.classify.constantize.new when Symbol # define :foo, class: foo name = mock_opts[:class].to_s.classify if Object.const_defined?(name) name.constantize.new else Object.const_set(name, Class.new).new end else # define :foo, class: FooBar mock_opts[:class].new end if @model instance_exec @model, opts, &block else @model = instance_exec opts, &block end raise 'Trait [%s] not found' % @traits.join(', ') if @traits.first end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/clean-mock/base.rb', line 4 def model @model end |
Class Method Details
.attributes_for(*args) ⇒ Object
16 17 18 |
# File 'lib/clean-mock/base.rb', line 16 def attributes_for *args build(*args).attributes.select{ |k,v| v.present? } end |
.build(*args) ⇒ Object
create a new model without save
21 22 23 |
# File 'lib/clean-mock/base.rb', line 21 def build *args new(*args).model end |
.create(*args) ⇒ Object
save if responds to save
26 27 28 |
# File 'lib/clean-mock/base.rb', line 26 def create *args new(*args).create_mock end |
.define(name, opts = {}, &block) ⇒ Object
define a mocked model factory
12 13 14 |
# File 'lib/clean-mock/base.rb', line 12 def define name, opts={}, &block @@mock_data[name] = [block, opts] end |
.fetch(*args, &block) ⇒ Object
create only once - cached on args identity
31 32 33 |
# File 'lib/clean-mock/base.rb', line 31 def fetch *args, &block @@fetched[args] ||= create(*args, &block) end |
Instance Method Details
#after_create(&block) ⇒ Object
callback fired after ‘create` / `fetch` saves the model (not on `build`)
114 115 116 |
# File 'lib/clean-mock/base.rb', line 114 def after_create &block @after_create = block end |
#create(name, field = nil) ⇒ Object
helper to create and link model create :org -> @model.org_id = mock.create(org).id
100 101 102 103 104 105 |
# File 'lib/clean-mock/base.rb', line 100 def create name, field = nil field ||= name.to_s.singularize + '_id' new_model = CleanMock.create(name) @model.send('%s=' % field, new_model.id) new_model end |
#create_mock ⇒ Object
107 108 109 110 111 |
# File 'lib/clean-mock/base.rb', line 107 def create_mock @model.save if @model.respond_to?(:save) @after_create.call if @after_create @model end |
#func(name, &block) ⇒ Object
define or overload current instance method
87 88 89 |
# File 'lib/clean-mock/base.rb', line 87 def func name, &block @model.define_singleton_method(name, &block) end |
#sequence(name = nil, start = nil) ⇒ Object
simple sequence generator by name
92 93 94 95 96 |
# File 'lib/clean-mock/base.rb', line 92 def sequence name=nil, start=nil name ||= :seq @@sequence[name] ||= start || 0 @@sequence[name] += 1 end |
#trait(name, &block) ⇒ Object
block to execute and modify model
80 81 82 83 84 |
# File 'lib/clean-mock/base.rb', line 80 def trait name, &block if @traits.delete(name) instance_exec(@model, &block) end end |