Skip to content

Commit

Permalink
has_value and has_collection now accept option :extend
Browse files Browse the repository at this point in the history
  • Loading branch information
mustmodify committed Feb 15, 2013
1 parent 24448e8 commit 5222d8c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
31 changes: 23 additions & 8 deletions lib/valuable.rb
Expand Up @@ -157,10 +157,13 @@ def defaults
def has_value(name, options={})
Valuable::Utils.check_options_validity(self.class.name, name, options)

options[:extend] = [options[:extend]].flatten.compact

name = name.to_sym
_attributes[name] = options

create_accessor_for(name)

create_accessor_for(name, options[:extend])

create_question_for(name) if options[:klass] == :boolean
create_negative_question_for(name, options[:negative]) if options[:klass] == :boolean && options[:negative]

Expand Down Expand Up @@ -200,12 +203,23 @@ def sudo_alias( alias_name, method_name )
end
end

# creates a simple accessor method named after the attribute
def create_accessor_for(name)
# creates an accessor method named after the
# attribute... can be used as a chained setter,
# as in:
#
# whitehouse.windows(5).doors(4).oval_rooms(1)
#
# If NOT used as a setter, returns the value,
# extended by the modules listed in the second
# parameter.
def create_accessor_for(name, extensions)
define_method name do |*args|

if args.length == 0
attributes[name]
if args.length == 0
attributes[name].tap do |out|
extensions.each do |extension|
out.extend( extension )
end
end
else
send("#{name}=", *args)
self
Expand Down Expand Up @@ -270,10 +284,11 @@ def has_collection(name, options = {})
options[:item_klass] = options[:klass] if options[:klass]
options[:klass] = :collection
options[:default] = []
options[:extend] = [options[:extend]].flatten.compact

_attributes[name] = options

create_accessor_for(name)
create_accessor_for(name, options[:extend])
create_setter_for(name)

sudo_alias options[:alias], name if options[:alias]
Expand Down
2 changes: 1 addition & 1 deletion lib/valuable/utils.rb
Expand Up @@ -116,7 +116,7 @@ def klass_options
end

def known_options
[:klass, :default, :negative, :alias, :parse_with]
[:klass, :default, :negative, :alias, :parse_with, :extend]
end

# this helper raises an exception if the options passed to has_value
Expand Down
30 changes: 30 additions & 0 deletions test/extending_test.rb
@@ -0,0 +1,30 @@
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'test/unit'
require 'valuable.rb'

module BookCollection
end

module PirateFormatter
def to_pirate
"#{self}, ARRRGGGhhhhh!"
end
end

class Series < Valuable
has_collection :books, :extend => BookCollection
has_value :name, :extend => PirateFormatter
end

class ExtendingTest < Test::Unit::TestCase
def test_that_collections_are_extended
assert Series.new.books.is_a?(BookCollection)
end

def test_that_values_are_extended
assert_equal 'Walk The Plank, ARRRGGGhhhhh!', Series.new(:name => 'Walk The Plank').name.to_pirate
end

end

0 comments on commit 5222d8c

Please sign in to comment.