Skip to content

Commit

Permalink
Added Config::Field class
Browse files Browse the repository at this point in the history
This will handle tracking extra fields for the archive table
  • Loading branch information
tpett committed Mar 16, 2012
1 parent f9a93ae commit 4f8e708
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/undestroy/config.rb
Expand Up @@ -63,3 +63,4 @@ def self.reset_catalog

end

require 'undestroy/config/field'
16 changes: 16 additions & 0 deletions lib/undestroy/config/field.rb
@@ -0,0 +1,16 @@
class Undestroy::Config::Field

attr_accessor :name, :type, :raw_value

def initialize(name, type, value=nil, &block)
self.name = name
self.type = type
self.raw_value = block || value || raise(ArgumentError, "Must pass a value or block")
end

def value(*args)
raw_value.is_a?(Proc) ? raw_value.call(*args) : raw_value
end

end

76 changes: 76 additions & 0 deletions test/unit/config/field_test.rb
@@ -0,0 +1,76 @@
require 'assert'

class Undestroy::Config::Field::Test

class Base < Undestroy::Test::Base
desc 'Undestroy::Config::Field class'
subject { @subject_class }

setup do
@subject_class = Undestroy::Config::Field
end
end

class BasicInstance < Base
desc 'basic instance'
subject { @subject_class.new :foo, :foo, 'foo' }

should have_accessors(:name, :type, :raw_value)

end

class InitMethod < Base
desc 'initialize method'

should "have 2 required params" do
assert_raises(ArgumentError) { subject.new }
assert_not_raises { subject.new :foo, :bar, 'val' }
assert_not_raises { subject.new(:foo, :bar) { 'val' } }
end

should "store name in name attr" do
obj = subject.new :field, :string, 'val'
assert_equal :field, obj.name
end

should "store type in type attr" do
obj = subject.new :field, :string, 'val'
assert_equal :string, obj.type
end

should "store value in raw_value attr if present" do
obj = subject.new :field, :string, 'val'
assert_equal 'val', obj.raw_value
end

should "store optional block in raw_value" do
block = proc { "foo" }
obj = subject.new :field, :string, 'val', &block
assert_equal block, obj.raw_value
end

should "raise if a block and a value are not passed" do
assert_raises(ArgumentError) { subject.new :field, :string }
end
end

class ValueMethod < Base
desc 'value method'

should "return raw_value if not callable" do
obj = subject.new :field, :string, 'val'
assert_equal 'val', obj.value
end

should "evaluate raw_value with passed args" do
block = proc { |args| args }
block2 = proc { |arg1, arg2| [arg1, arg2] }
obj = subject.new :field, :string, &block
obj2 = subject.new :field, :string, &block2
assert_equal 1, obj.value(1)
assert_equal [1, 2], obj2.value(1, 2)
end
end

end

0 comments on commit 4f8e708

Please sign in to comment.