Class: Shiftable::Collection
- Inherits:
-
Module
- Object
- Module
- Shiftable::Collection
- Defined in:
- lib/shiftable/collection.rb
Overview
Inheriting from Module is a powerful pattern. If you like it checkout the debug_logging gem!
Defined Under Namespace
Modules: ShiftCollectionModulizer
Instance Method Summary collapse
-
#extended(base) ⇒ Object
NOTE: Possible difference in how inheritance works when using extend vs include with Shiftable::Collection.new.
-
#included(base) ⇒ Object
NOTE: Possible difference in how inheritance works when using extend vs include with Shiftable::Collection.new.
-
#initialize(belongs_to:, has_many:, polymorphic: nil, method_prefix: nil, before_shift: nil, wrapper: nil) ⇒ Collection
constructor
associations: belongs_to, has_many options: method_prefix, before_shift.
Constructor Details
#initialize(belongs_to:, has_many:, polymorphic: nil, method_prefix: nil, before_shift: nil, wrapper: nil) ⇒ Collection
associations: belongs_to, has_many options: method_prefix, before_shift
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/shiftable/collection.rb', line 19 def initialize(belongs_to:, has_many:, polymorphic: nil, method_prefix: nil, before_shift: nil, wrapper: nil) # Ruby's Module initializer doesn't take any arguments super() @signature = ModSignature.new( # For the following, imagine you are a Space Federation, each Spaceship in the fleet belongs_to you, # i.e. the federation has_many spaceships. # But you lose the war, and your nemesis commandeers all your ships! associations: { # The name of the belongs_to association, defined on the shifting model, e.g. Spaceship # Normally a camel-cased, symbolized, version of the class name. # In the case where Spaceship belongs_to: :space_federation, this is :space_federation. belongs_to: belongs_to.to_s.to_sym, # The name of the has_many association, defined on the shift_to/shift_from model, e.g. SpaceFederation. # Normally a camel-cased, symbolized, version of the class name. # In the case where SpaceFederation has_many: :spaceships, this is :spaceships. has_many: has_many.to_s.to_sym }, options: { polymorphic: polymorphic, method_prefix: method_prefix, # will prevent the save if it returns false # allows for any custom logic to be run, such as setting attributes, prior to the shift (save). before_shift: before_shift, # wrapper: { # all: ->() { klass.transaction_wrapper { yield } }, # each: ->() { klass.transaction_wrapper { yield } }, # } wrapper: wrapper }, type: polymorphic ? :pcx : :cx ) end |
Instance Method Details
#extended(base) ⇒ Object
NOTE: Possible difference in how inheritance works when using extend vs include
with Shiftable::Collection.new
55 56 57 58 |
# File 'lib/shiftable/collection.rb', line 55 def extended(base) shift_cx_modulizer = ShiftCollectionModulizer.to_mod(@signature.add_base(base)) base.singleton_class.send(:prepend, shift_cx_modulizer) end |
#included(base) ⇒ Object
NOTE: Possible difference in how inheritance works when using extend vs include
with Shiftable::Collection.new
62 63 64 65 |
# File 'lib/shiftable/collection.rb', line 62 def included(base) shift_cx_modulizer = ShiftCollectionModulizer.to_mod(@signature.add_base(base)) base.send(:prepend, shift_cx_modulizer) end |