Sha256: 03cff3c395693d501e36cb9e3ad6c5203f3b4cb0ad889a184bf83832f964137f

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

module Trestle
  module Configurable
    extend ActiveSupport::Concern

    def initialize
      self.class.defaults.each do |name, default|
        options[name] = default
      end
    end

    def configure(&block)
      yield self if block_given?
      self
    end

    def options
      @options ||= {}
    end

    def inspect
      "#<#{self.class.name || "Anonymous(Trestle::Configurable)"}>"
    end

    module ClassMethods
      def defaults
        @defaults ||= {}
      end

      def option(name, default=nil, opts={})
        name = name.to_sym

        define_method("#{name}=") do |value|
          options[name] = value
        end

        define_method(name) do |*args|
          value = options[name]

          if value.respond_to?(:call) && opts[:evaluate] != false
            value.call(*args)
          else
            value
          end
        end

        defaults[name] = default
      end
    end

    module Open
    protected
      def respond_to_missing(name, include_all=false)
        true
      end

      def method_missing(name, *args, &block)
        if name =~ /(.*)\=$/
          key, value = $1, args.first
          options[key.to_sym] = value
        else
          options[name.to_sym] ||= self.class.new
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
trestle-0.8.7 lib/trestle/configurable.rb
trestle-0.8.6 lib/trestle/configurable.rb
trestle-0.8.5 lib/trestle/configurable.rb
trestle-0.8.4 lib/trestle/configurable.rb
trestle-0.8.3 lib/trestle/configurable.rb
trestle-0.8.2 lib/trestle/configurable.rb
trestle-0.8.0 lib/trestle/configurable.rb