Sha256: b1ddf606e5a377be4bbc85568315516b309b418ad8a678e2c3d69412658e140a

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

module VestalVersions
  # Allows for easy application-wide configuration of options passed into the +versioned+ method.
  module Configuration
    # The VestalVersions module is extended by VestalVersions::Configuration, allowing the
    # +configure method+ to be used as follows in a Rails initializer:
    #
    #   VestalVersions.configure do |config|
    #     config.class_name = "MyCustomVersion"
    #     config.dependent = :destroy
    #   end
    #
    # Each variable assignment in the +configure+ block corresponds directly with the options
    # available to the +versioned+ method. Assigning common options in an initializer can keep your
    # models tidy.
    #
    # If an option is given in both an initializer and in the options passed to +versioned+, the
    # value given in the model itself will take precedence.
    def configure
      yield Configuration
    end

    class << self
      # Simply stores a hash of options given to the +configure+ block.
      def options
        @options ||= {}
      end

      # If given a setter method name, will assign the first argument to the +options+ hash with
      # the method name (sans "=") as the key. If given a getter method name, will attempt to
      # a value from the +options+ hash for that key. If the key doesn't exist, defers to +super+.
      def method_missing(symbol, *args)
        if (method = symbol.to_s).sub!(/\=$/, '')
          options[method.to_sym] = args.first
        else
          options.fetch(method.to_sym, super)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 4 rubygems

Version Path
nickjones-vestal_versions-1.0.3 lib/vestal_versions/configuration.rb
bitfluent-vestal_versions-1.1.0 lib/vestal_versions/configuration.rb
brianjlandau-vestal_versions-1.3.0 lib/vestal_versions/configuration.rb
vestal_versions-1.0.2 lib/vestal_versions/configuration.rb
vestal_versions-1.0.1 lib/vestal_versions/configuration.rb
vestal_versions-1.0.0 lib/vestal_versions/configuration.rb