Module: Rooftop::Coercions
- Defined in:
- lib/rooftop/coercions.rb,
lib/rooftop/coercions/title_coercion.rb,
lib/rooftop/coercions/author_coercion.rb,
lib/rooftop/coercions/parent_coercion.rb
Defined Under Namespace
Modules: AuthorCoercion, ClassMethods, ParentCoercion, TitleCoercion
Class Method Summary collapse
Instance Method Summary collapse
-
#coercions ⇒ Object
Instance method to get the class’s coercions.
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rooftop/coercions.rb', line 3 def self.included(base) # Include Rooftop::HookCalls to allow us to push things into a list of hooks in the right order base.include Rooftop::HookCalls base.extend ClassMethods # Add the call to the :after_find hook to the list of hook calls, to be processed later. # This is where we iterate over our previously established list of coercions, and call each # in turn base.send(:add_to_hook, :after_find, ->(r){ r.coercions.each do |field,coercion| if r.respond_to?(field) r.send("#{field}=",coercion.call(r.send(field))) end end }) base.send(:before_save, ->(r) { r.coercions.each do |field,coercion| r.send(:"restore_#{field}!") unless r.new? end }) end |
Instance Method Details
#coercions ⇒ Object
Instance method to get the class’s coercions
40 41 42 |
# File 'lib/rooftop/coercions.rb', line 40 def coercions self.class.instance_variable_get(:"@coercions") || {} end |