Skip to content

Commit

Permalink
sketched out configurations without config types
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkerbot committed Nov 21, 2010
1 parent be8785b commit 083e3de
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 242 deletions.
43 changes: 18 additions & 25 deletions lib/configurable/class_methods.rb
Expand Up @@ -6,19 +6,19 @@ module Configurable

# Hash of default config types (bool, integer, float, string).
DEFAULT_CONFIG_TYPES = {
:bool => ConfigTypes::BooleanType,
:integer => ConfigTypes::IntegerType,
:float => ConfigTypes::FloatType,
:string => ConfigTypes::StringType,
:nest => ConfigTypes::NestType,
:obj => ConfigTypes::ObjectType
:bool => ConfigClasses::BooleanConfig,
:integer => ConfigClasses::IntegerConfig,
:float => ConfigClasses::FloatConfig,
:string => ConfigClasses::StringConfig,
:nest => ConfigClasses::Nest,
:obj => ConfigClasses::Config
}

# ClassMethods extends classes that include Configurable and provides methods
# for declaring configurations.
module ClassMethods
include ConfigClasses
include ConfigTypes
include ConfigClasses

# A hash of (key, Config) pairs tracking configs defined on self. See the
# configs method for all configs declared across all ancestors.
Expand Down Expand Up @@ -149,12 +149,16 @@ def config(key, default=nil, attrs={}, &block)
nest_class = guess_nest_class(default, block)

attrs[:default] = nest_class ? nest_class.new : default
attrs[:type] = guess_config_type(attrs).new(attrs)
attrs[:desc] = guess_config_desc(attrs, Lazydoc.register_caller)

config_class = attrs[:class] || guess_config_class(attrs)
attrs[:list] = default.kind_of?(Array) unless attrs.has_key?(:list)

config_class = guess_config_type(attrs)
config = define_config(key, attrs, config_class)


if attrs[:list]
config.extend List
end

if nest_class
const_name = attrs[:const_name] || guess_nest_const_name(config)
unless const_defined?(const_name)
Expand Down Expand Up @@ -232,7 +236,7 @@ def define_config_type(name, config_type)
end

def config_type(name, *matchers, &caster)
config_type = StringType.subclass(*matchers).cast(&caster)
config_type = StringConfig.subclass(*matchers).cast(&caster)
const_name = guess_config_type_const_name(name)

unless const_defined?(const_name)
Expand All @@ -257,7 +261,7 @@ def remove_config_type(name)
#
# ==== Implementation Note
#
# ConfigTypes are undefined by setting the key to nil in the registry.
# ConfigClasses are undefined by setting the key to nil in the registry.
# Deleting the config_type is not sufficient because the registry needs to
# convey to self and subclasses to not inherit the config_type from
# ancestors.
Expand Down Expand Up @@ -354,7 +358,7 @@ def guess_config_type_by_value(value) # :nodoc:
end
end

ObjectType
Config
end

def guess_config_type(attrs) # :nodoc:
Expand All @@ -367,17 +371,6 @@ def guess_config_type(attrs) # :nodoc:
end
end

def guess_config_class(attrs) # :nodoc:
case attrs[:default]
when Array
List
when Configurable
Nest
else
Config
end
end

def guess_config_desc(base_attrs, comment) # :nodoc:
Hash.new do |hash, key|
comment.resolve
Expand Down
4 changes: 4 additions & 0 deletions lib/configurable/config_classes.rb
@@ -1,3 +1,7 @@
require 'configurable/config_classes/config'
require 'configurable/config_classes/list'
require 'configurable/config_classes/nest'
require 'configurable/config_classes/string_config'
require 'configurable/config_classes/boolean_config'
require 'configurable/config_classes/integer_config'
require 'configurable/config_classes/float_config'
@@ -1,6 +1,6 @@
module Configurable
module ConfigTypes
class BooleanType < StringType
module ConfigClasses
class BooleanConfig < StringConfig
matches TrueClass, FalseClass

# Casts the input to a boolean ie:
Expand Down
54 changes: 44 additions & 10 deletions lib/configurable/config_classes/config.rb
@@ -1,11 +1,49 @@
require 'configurable/config_types'

module Configurable
module ConfigClasses
# ConfigClasses are used by ConfigHash to delegate get/set configs on a
# receiver and to map configs between user interfaces.
class Config
include ConfigTypes
class << self
attr_reader :matchers

def inherited(base) # :nodoc:
unless base.instance_variable_defined?(:@matchers)
base.instance_variable_set(:@matchers, matchers.dup)
end
end

def subclass(*matchers, &caster)
subclass = Class.new(self)
subclass.matches(*matchers)
subclass.cast(&caster)
subclass
end

def cast(&block)
define_method(:cast, &block) if block
self
end

def uncast(&block)
define_method(:uncast, &block) if block
self
end

def errors(&block)
define_method(:errors, &block) if block
self
end

def matches(*matchers)
@matchers = matchers
self
end

def matches?(value)
matchers.any? {|matcher| matcher === value }
end
end
matches()

# The config key, used as a hash key for access.
attr_reader :key
Expand All @@ -23,9 +61,6 @@ class Config
# The default config value.
attr_reader :default

# The config type for self (defaults to an ObjectType)
attr_reader :type

# A hash of information used to render self in various contexts.
attr_reader :desc

Expand All @@ -37,7 +72,6 @@ def initialize(key, attrs={})
check_name(@name)

@default = attrs[:default]
@type = attrs[:type] || ObjectType.new
check_default(@default)

@reader = (attrs[:reader] || name).to_sym
Expand All @@ -58,13 +92,13 @@ def get(receiver)
def set(receiver, value)
receiver.send(writer, value)
end

def cast(input)
type.cast(input)
input
end

def uncast(value)
type.uncast(value)
value
end

# Returns an inspect string.
Expand Down
@@ -1,6 +1,6 @@
module Configurable
module ConfigTypes
class FloatType < StringType
module ConfigClasses
class FloatConfig < StringConfig
matches Float

def cast(input)
Expand Down
@@ -1,6 +1,6 @@
module Configurable
module ConfigTypes
class IntegerType < StringType
module ConfigClasses
class IntegerConfig < StringConfig
matches Integer

def cast(input)
Expand Down
12 changes: 1 addition & 11 deletions lib/configurable/config_classes/list.rb
@@ -1,16 +1,6 @@
module Configurable
module ConfigClasses

class List < Config

def initialize(key, attrs={})
unless attrs.has_key?(:default)
attrs[:default] = []
end

super
end

module List
def cast(values)
results = []
values.each {|value| results << super(value) }
Expand Down
9 changes: 9 additions & 0 deletions lib/configurable/config_classes/nest.rb
Expand Up @@ -3,6 +3,7 @@ module ConfigClasses

# Represents a config where the input is expected to be Configurable.
class Nest < Config
matches Configurable

def configurable
@default
Expand Down Expand Up @@ -38,6 +39,14 @@ def set(receiver, value)
end
end

def cast(input)
configurable.class.configs.import(input)
end

def uncast(value)
configurable.class.configs.export(value)
end

protected

def check_default(default) # :nodoc:
Expand Down
@@ -1,6 +1,6 @@
module Configurable
module ConfigTypes
class StringType < ObjectType
module ConfigClasses
class StringConfig < Config
matches String

def cast(input)
Expand Down
6 changes: 0 additions & 6 deletions lib/configurable/config_types.rb

This file was deleted.

27 changes: 0 additions & 27 deletions lib/configurable/config_types/nest_type.rb

This file was deleted.

58 changes: 0 additions & 58 deletions lib/configurable/config_types/object_type.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/configurable/conversions.rb
Expand Up @@ -27,7 +27,7 @@ def to_parser(*args, &block)
:key => config.key,
:nest_keys => nest_keys,
:default => config.default,
:callback => lambda {|value| config.type.cast(value) }
:callback => lambda {|value| config.cast(value) }
}

attrs = guess_attrs.merge(config.desc).merge(config_attrs)
Expand Down
@@ -1,13 +1,13 @@
require File.expand_path('../../../test_helper', __FILE__)
require 'configurable/config_types'
require 'configurable/config_classes'

class BooleanTypeTest < Test::Unit::TestCase
include Configurable::ConfigTypes
class BooleanConfigTest < Test::Unit::TestCase
include Configurable::ConfigClasses

attr_reader :type

def setup
@type = BooleanType.new
@type = BooleanConfig.new(:key)
end

#
Expand Down

0 comments on commit 083e3de

Please sign in to comment.