Class: WithModel::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/with_model/model.rb,
lib/with_model/model/dsl.rb

Overview

In general, direct use of this class should be avoided. Instead use either the high-level API or low-level API.

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, superclass: ActiveRecord::Base) ⇒ Model

Returns a new instance of Model.

Parameters:

  • name (Symbol)

    The constant name to assign the model class to.

  • superclass (defaults to: ActiveRecord::Base)

    The superclass for the created class. Either a Class having ActiveRecord::Base as an ancestor, a String naming one, or a callable returning one. A String or callable is resolved afresh for every example, which is what allows another with_model model - whose constant does not exist when this line is read - to be the superclass.



27
28
29
30
31
32
33
34
35
# File 'lib/with_model/model.rb', line 27

def initialize(name, superclass: ActiveRecord::Base)
  @name = name.to_sym
  @model_block = nil
  @table_block = nil
  @table_options = {}
  @table_specified = false
  @skip_table = false
  @superclass_spec = superclass
end

Instance Attribute Details

#model_block=(value) ⇒ Object (writeonly)

Sets the attribute model_block

Parameters:

  • value

    the value to set the attribute model_block to.



19
20
21
# File 'lib/with_model/model.rb', line 19

def model_block=(value)
  @model_block = value
end

#table_block=(value) ⇒ Object (writeonly)

Sets the attribute table_block

Parameters:

  • value

    the value to set the attribute table_block to.



19
20
21
# File 'lib/with_model/model.rb', line 19

def table_block=(value)
  @table_block = value
end

#table_options=(value) ⇒ Object (writeonly)

Sets the attribute table_options

Parameters:

  • value

    the value to set the attribute table_options to.



19
20
21
# File 'lib/with_model/model.rb', line 19

def table_options=(value)
  @table_options = value
end

Instance Method Details

#createObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/with_model/model.rb', line 56

def create
  @superclass = resolve_superclass
  @table = nil
  table.create
  @model = Class.new(@superclass) do
    extend WithModel::Methods
  end
  stubber.stub_const @model
  setup_model
end

#destroyObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/with_model/model.rb', line 67

def destroy
  # Test runners tear down even when setup raised, so `create` may not have
  # reached the point of building the model. Nothing was stubbed and nothing
  # wrote rows, so there is nothing to undo.
  return unless @model

  # Before `unstub_const`: a teardown that identifies this model's own rows
  # can only do so while the class still has its name.
  table.teardown(@model)
  stubber.unstub_const
  cleanup_descendants_tracking
  reset_dependencies_cache
  WithModel::DescendantsTracker.clear([@model])
  @model = nil
end

#specify_table(options, block) ⇒ Object

Records what WithModel::Model::DSL#table was asked for, including the fact that it was asked for at all.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/with_model/model.rb', line 39

def specify_table(options, block)
  @table_specified = true

  if options == false
    raise ArgumentError, "table does not take a block when its first argument is falsy" if block

    @skip_table = true
  else
    @table_options = options
    @table_block = block
  end
end

#table_specified?Boolean

Whether a table was specified at all. A table call with no arguments counts, so this cannot be inferred from the options and block alone.

Returns:

  • (Boolean)


54
# File 'lib/with_model/model.rb', line 54

def table_specified? = @table_specified