Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Options can be set on the Type base
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Mar 25, 2012
1 parent 97767ff commit d432ac2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/omniconfig/type/base.rb
Expand Up @@ -4,13 +4,24 @@ module Type
# as the inheritance is checked when {Structure#define} is being
# called.
class Base
# Every type can have arbitrary options added to it that the loaders
# might use for some reason. This is the options hash that can be used
# to add or modify options.
attr_reader :options

# Initializes the type and returns an instance of it.
#
# @return [Object] Instantiated type.
def self.instance
self.new
end

# Initialize a type, passing in the given options as options to the
# type.
def initialize(opts=nil)
@options = opts || {}
end

# This just returns this instance. This is useful because there is also
# a class method {instance} which can be called which will initialize the
# class and return a new instance. Therefore, you can just call `instance`
Expand All @@ -21,15 +32,6 @@ def instance
self
end

# Every type can have arbitrary options added to it that the loaders
# might use for some reason. This is the options hash that can be used
# to add or modify options.
#
# @return [Hash]
def options
@options ||= {}
end

# By default a type will simply return the raw value as the
# converted result.
def value(raw)
Expand Down
5 changes: 5 additions & 0 deletions spec/omniconfig/type/base_spec.rb
Expand Up @@ -4,6 +4,11 @@
describe OmniConfig::Type::Base do
let(:instance) { described_class.new }

it "should set options from initialization" do
instance = described_class.new(:foo => :bar)
instance.options[:foo].should == :bar
end

it "should just let values pass through" do
value = Object.new
instance.value(value).should eql(value)
Expand Down

0 comments on commit d432ac2

Please sign in to comment.