Sha256: 555e6c2457004223246eb4af7152dcc4784fd53f091ee68ba13f0af3b627479e

Contents?: true

Size: 1.91 KB

Versions: 79

Compression:

Stored size: 1.91 KB

Contents

module ActiveRecord
  # A plugin framework for wrapping attribute values before they go in and unwrapping them after they go out of the database.
  # This was intended primarily for YAML wrapping of arrays and hashes, but this behavior is now native in the Base class.
  # So for now this framework is laying dormant until a need pops up.
  module Wrappings #:nodoc:
    module ClassMethods #:nodoc:
      def wrap_with(wrapper, *attributes)
        [ attributes ].flat.each { |attribute| wrapper.wrap(attribute) }
      end
    end

    def self.included(base)
      base.extend(ClassMethods)
    end

    class AbstractWrapper #:nodoc:
      def self.wrap(attribute, record_binding) #:nodoc:
        %w( before_save after_save after_initialize ).each do |callback|
          eval "#{callback} #{name}.new('#{attribute}')", record_binding
        end
      end

      def initialize(attribute) #:nodoc:
        @attribute = attribute
      end

      def save_wrapped_attribute(record) #:nodoc:
        if record.attribute_present?(@attribute)
          record.send(
            "write_attribute", 
            @attribute, 
            wrap(record.send("read_attribute", @attribute))
          )
        end
      end

      def load_wrapped_attribute(record) #:nodoc:
        if record.attribute_present?(@attribute)
          record.send(
            "write_attribute", 
            @attribute, 
            unwrap(record.send("read_attribute", @attribute))
          )
        end
      end
  
      alias_method :before_save, :save_wrapped_attribute #:nodoc:
      alias_method :after_save, :load_wrapped_attribute #:nodoc:
      alias_method :after_initialize, :after_save #:nodoc:

      # Overwrite to implement the logic that'll take the regular attribute and wrap it.
      def wrap(attribute) end
  
      # Overwrite to implement the logic that'll take the wrapped attribute and unwrap it.
      def unwrap(attribute) end
    end
  end
end

Version data entries

79 entries across 79 versions & 6 rubygems

Version Path
jstorimer-deep-test-2.0.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
jstorimer-deep-test-1.4.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
jstorimer-deep-test-1.3.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
jstorimer-deep-test-1.2.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
jstorimer-deep-test-1.1.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
jstorimer-deep-test-1.0.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
jstorimer-deep-test-0.2.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
jstorimer-deep-test-0.1.0 sample_rails_project/vendor/rails/activerecord/lib/active_record/wrappings.rb
activerecord-1.15.4 lib/active_record/wrappings.rb
activerecord-1.15.3 lib/active_record/wrappings.rb
activerecord-1.15.0 lib/active_record/wrappings.rb
activerecord-1.15.2 lib/active_record/wrappings.rb
activerecord-1.15.1 lib/active_record/wrappings.rb
activerecord-1.15.5 lib/active_record/wrappings.rb
activerecord-1.15.6 lib/active_record/wrappings.rb
activerecord_authorails-1.0.0 lib/active_record/wrappings.rb
backlog-0.0.0 vendor/rails/activerecord/lib/active_record/wrappings.rb
backlog-0.0.1 vendor/rails/activerecord/lib/active_record/wrappings.rb
backlog-0.0.2 vendor/rails/activerecord/lib/active_record/wrappings.rb
backlog-0.0.4 vendor/rails/activerecord/lib/active_record/wrappings.rb