Sha256: 952d827b2876c2b6d605152fe490b6f038a0d5b7efc1eaf2c84a1fa9c389d4cc

Contents?: true

Size: 1.07 KB

Versions: 30

Compression:

Stored size: 1.07 KB

Contents

module Stove
  module Mixin::Optionable
    def self.included(base)
      base.send(:extend, ClassMethods)
    end

    def self.extended(base)
      base.send(:extend, ClassMethods)
    end

    module ClassMethods
      #
      # This is a magical method. It does three things:
      #
      # 1. Defines a class method getter and setter for the given option
      # 2. Defines an instance method that delegates to the class method
      # 3. (Optionally) sets the initial value
      #
      # @param [String, Symbol] name
      #   the name of the option
      # @param [Object] initial
      #   the initial value to set (optional)
      #
      def option(name, initial = UNSET_VALUE)
        define_singleton_method(name) do |value = UNSET_VALUE|
          if value == UNSET_VALUE
            instance_variable_get("@#{name}")
          else
            instance_variable_set("@#{name}", value)
          end
        end

        define_method(name) { self.class.send(name) }

        unless initial == UNSET_VALUE
          send(name, initial)
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 3 rubygems

Version Path
chef-stove-7.1.6 lib/stove/mixins/optionable.rb
stove-7.1.6 lib/stove/mixins/optionable.rb
chef-stove-7.1.5 lib/stove/mixins/optionable.rb
stove-7.1.5 lib/stove/mixins/optionable.rb
chef-stove-7.1.1 lib/stove/mixins/optionable.rb
stove-7.1.0 lib/stove/mixins/optionable.rb
stove-7.0.1 lib/stove/mixins/optionable.rb
stove-7.0.0 lib/stove/mixins/optionable.rb
stove-6.1.1 lib/stove/mixins/optionable.rb
stove-6.1.0 lib/stove/mixins/optionable.rb
stove-6.0.0 lib/stove/mixins/optionable.rb
stove-5.2.0 lib/stove/mixins/optionable.rb
stove-5.1.0 lib/stove/mixins/optionable.rb
stove-5.0.0 lib/stove/mixins/optionable.rb
stove-4.1.1 lib/stove/mixins/optionable.rb
stove-4.1.0 lib/stove/mixins/optionable.rb
stove-4.0.0 lib/stove/mixins/optionable.rb
stove-3.2.8 lib/stove/mixins/optionable.rb
wood-stove-3.2.9000 lib/stove/mixins/optionable.rb
stove-3.2.7 lib/stove/mixins/optionable.rb