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 (Class) (defaults to: ActiveRecord::Base)

    The superclass for the created class. Should have ActiveRecord::Base as an ancestor.



19
20
21
22
23
24
25
# File 'lib/with_model/model.rb', line 19

def initialize(name, superclass: ActiveRecord::Base)
  @name = name.to_sym
  @model_block = nil
  @table_block = nil
  @table_options = {}
  @superclass = 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.



14
15
16
# File 'lib/with_model/model.rb', line 14

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.



14
15
16
# File 'lib/with_model/model.rb', line 14

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.



14
15
16
# File 'lib/with_model/model.rb', line 14

def table_options=(value)
  @table_options = value
end

Instance Method Details

#createObject



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

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

#destroyObject



36
37
38
39
40
41
42
# File 'lib/with_model/model.rb', line 36

def destroy
  stubber.unstub_const
  cleanup_descendants_tracking
  reset_dependencies_cache
  table.destroy
  @model = nil
end