Sha256: 94df05b4c399d368d77c8a8d40d4b2b964165c98ee0cd6899a4fd46396e1c06d

Contents?: true

Size: 1.63 KB

Versions: 10

Compression:

Stored size: 1.63 KB

Contents

# @see https://github.com/opennorth/pupa-ruby#opencivicdata-compatibility

module Pupa::Model
  # This unfortunately won't cause the behavior of any model that has already
  # included `Pupa::Model` to change.
  class << self
    def append_features(base)
      if base.instance_variable_defined?("@_dependencies")
        base.instance_variable_get("@_dependencies") << self
        return false
      else
        return false if base < self
        @_dependencies.each { |dep| base.send(:include, dep) }
        super
        base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
        base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
        base.class_eval do # XXX
          set_callback(:save, :before) do |object|
            object._type = object._type.camelize.demodulize.underscore
          end
        end
      end
    end
  end
end

# `set_callback` is called by `class_eval` in `ActiveSupport::Concern`. Without
# monkey-patching `ActiveSupport::Concern`, we can either iterate `ObjectSpace`,
# implement something like ActiveSupport's `DescendantsTracker` for inclusion
# instead of inheritance, or go back to `Pupa::Model` being a superclass instead
# of a mixin to take advantage of `DescendantsTracker` itself.
#
# Instead of adding a callback, we can override `to_h` when `persist` is `true`.
ObjectSpace.each_object(Class) do |base|
  if base != Sequel::Model && base.include?(Pupa::Model) # Sequel::Model will error on #include?
    base.class_eval do
      set_callback(:save, :before) do |object|
        object._type = object._type.camelize.demodulize.underscore
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pupa-0.1.10 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.9 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.8 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.7 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.6 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.5 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.4 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.3 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.2 lib/pupa/refinements/opencivicdata.rb
pupa-0.1.1 lib/pupa/refinements/opencivicdata.rb