Class: WithModel::Model
- Inherits:
-
Object
- Object
- WithModel::Model
- 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
-
#model_block ⇒ Object
writeonly
Sets the attribute model_block.
-
#table_block ⇒ Object
writeonly
Sets the attribute table_block.
-
#table_options ⇒ Object
writeonly
Sets the attribute table_options.
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
-
#initialize(name, superclass: ActiveRecord::Base) ⇒ Model
constructor
A new instance of Model.
-
#specify_table(options, block) ⇒ Object
Records what DSL#table was asked for, including the fact that it was asked for at all.
-
#table_specified? ⇒ Boolean
Whether a table was specified at all.
Constructor Details
#initialize(name, superclass: ActiveRecord::Base) ⇒ Model
Returns a new instance of Model.
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
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
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
19 20 21 |
# File 'lib/with_model/model.rb', line 19 def (value) @table_options = value end |
Instance Method Details
#create ⇒ Object
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 |
#destroy ⇒ Object
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(, block) @table_specified = true if == false raise ArgumentError, "table does not take a block when its first argument is falsy" if block @skip_table = true else @table_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.
54 |
# File 'lib/with_model/model.rb', line 54 def table_specified? = @table_specified |