Sha256: ba64ac6fa12d54e58126b39a387fe43087ee329c2d272f324c27e191f71577be

Contents?: true

Size: 1.81 KB

Versions: 29

Compression:

Stored size: 1.81 KB

Contents

# Shamelessly stolen from Mongoid!
module Dynamoid #:nodoc
  module Config

    # Encapsulates logic for setting options.
    module Options

      # Get the defaults or initialize a new empty hash.
      #
      # @example Get the defaults.
      #   options.defaults
      #
      # @return [ Hash ] The default options.
      #
      # @since 0.2.0
      def defaults
        @defaults ||= {}
      end

      # Define a configuration option with a default.
      #
      # @example Define the option.
      #   Options.option(:persist_in_safe_mode, :default => false)
      #
      # @param [ Symbol ] name The name of the configuration option.
      # @param [ Hash ] options Extras for the option.
      #
      # @option options [ Object ] :default The default value.
      #
      # @since 0.2.0
      def option(name, options = {})
        defaults[name] = settings[name] = options[:default]

        class_eval <<-RUBY
          def #{name}
            settings[#{name.inspect}]
          end

          def #{name}=(value)
            settings[#{name.inspect}] = value
          end

          def #{name}?
            #{name}
          end
          
          def reset_#{name}
            settings[#{name.inspect}] = defaults[#{name.inspect}]
          end
        RUBY
      end

      # Reset the configuration options to the defaults.
      #
      # @example Reset the configuration options.
      #   config.reset
      #
      # @return [ Hash ] The defaults.
      #
      # @since 0.2.0
      def reset
        settings.replace(defaults)
      end

      # Get the settings or initialize a new empty hash.
      #
      # @example Get the settings.
      #   options.settings
      #
      # @return [ Hash ] The setting options.
      #
      # @since 0.2.0
      def settings
        @settings ||= {}
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 5 rubygems

Version Path
dynamoid-1.3.4 lib/dynamoid/config/options.rb
dynamoid-1.3.3 lib/dynamoid/config/options.rb
dynamoid-1.3.2 lib/dynamoid/config/options.rb
dynamoid-1.3.1 lib/dynamoid/config/options.rb
dynamoid-1.3.0 lib/dynamoid/config/options.rb
synamoid-1.2.1 lib/dynamoid/config/options.rb
dynamoid-1.2.1 lib/dynamoid/config/options.rb
dynamoid-1.2.0 lib/dynamoid/config/options.rb
dynamoid-edge-1.1.2 lib/dynamoid/config/options.rb
dynamoid-edge-1.1.1 lib/dynamoid/config/options.rb
dynamoid-edge-1.1.0 lib/dynamoid/config/options.rb
dynamoid-1.1.0 lib/dynamoid/config/options.rb
dynamoid-1.0.0 lib/dynamoid/config/options.rb
dynamoid-moda-0.7.2 lib/dynamoid/config/options.rb
dynamoid-moda-0.7.1 lib/dynamoid/config/options.rb
dynamoid-0.7.1 lib/dynamoid/config/options.rb
dynamoid-0.7.0 lib/dynamoid/config/options.rb
dynamoid-0.6.1 lib/dynamoid/config/options.rb
dynamoid-0.6.0 lib/dynamoid/config/options.rb
adept_dynamoid-0.6.0 lib/dynamoid/config/options.rb