Sha256: 436be661fb2a7bf8ade7ab11202aa6ce741f5edbc1e0f64f961f6e3b86a1d63f

Contents?: true

Size: 911 Bytes

Versions: 2

Compression:

Stored size: 911 Bytes

Contents

class Module

  unless method_defined?(:set)

    # Sets an option to the given value. If the value is a proc,
    # the proc will be called every time the option is accessed.
    #
    # CREDIT: ? (Sinatra)

    def set(option, value=self, &block)
      raise ArgumentError if block && value != self
      value = block if block
      if value.kind_of?(Proc)
        if value.arity == 1
          yield self
        else
          (class << self; self; end).module_eval do
            define_method(option, &value)
            define_method("#{option}?"){ !!__send__(option) }
            define_method("#{option}="){ |val| set(option, Proc.new{val}) }
          end
        end
      elsif value == self
        option.each{ |k,v| set(k, v) }
      elsif respond_to?("#{option}=")
        __send__("#{option}=", value)
      else
        set(option, Proc.new{value})
      end
      self
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/module/set.rb
facets-2.8.3 lib/core/facets/module/set.rb