Class: ViewModel::TestHelpers::ARVMBuilder::Spec

Inherits:
Struct
  • Object
show all
Defined in:
lib/view_model/test_helpers/arvm_builder.rb,
lib/view_model/test_helpers/arvm_builder.rb

Overview

Building an ARVM requires three blocks, to define schema, model and viewmodel. Support providing these either in an spec argument or as a dsl-style builder.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, model:, viewmodel:) ⇒ Spec

Returns a new instance of Spec.



11
12
13
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 11

def initialize(schema:, model:, viewmodel:)
  super(schema, model, viewmodel)
end

Instance Attribute Details

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



9
10
11
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 9

def model
  @model
end

#schemaObject

Returns the value of attribute schema

Returns:

  • (Object)

    the current value of schema



9
10
11
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 9

def schema
  @schema
end

#viewmodelObject

Returns the value of attribute viewmodel

Returns:

  • (Object)

    the current value of viewmodel



9
10
11
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 9

def viewmodel
  @viewmodel
end

Instance Method Details

#merge(schema: nil, model: nil, viewmodel: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/view_model/test_helpers/arvm_builder.rb', line 15

def merge(schema: nil, model: nil, viewmodel: nil)
  this_schema    = self.schema
  this_model     = self.model
  this_viewmodel = self.viewmodel

  Spec.new(
    schema: ->(t) do
      this_schema.(t)
      schema&.(t)
    end,
    model: ->(m) do
      m.class_eval(&this_model)
      model.try { |b| m.class_eval(&b) }
    end,
    viewmodel: ->(v) do
      v.class_eval(&this_viewmodel)
      viewmodel.try { |b| v.class_eval(&b) }
    end)
end