Skip to content

Commit

Permalink
Refactoring around multi-valued attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdub committed Jun 4, 2013
1 parent 88894dd commit a045c03
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 33 deletions.
27 changes: 18 additions & 9 deletions lib/clamp/attribute/declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ module Declaration
def define_accessors_for(attribute, &block)
define_reader_for(attribute)
define_default_for(attribute)
define_writer_for(attribute, &block)
define_multi_writer_for(attribute) if attribute.multivalued?
if attribute.multivalued?
define_appender_for(attribute, &block)
define_multi_writer_for(attribute)
else
define_simple_writer_for(attribute, &block)
end
end

def define_reader_for(attribute)
Expand All @@ -24,20 +28,25 @@ def define_default_for(attribute)
end
end

def define_writer_for(attribute, &block)
def define_simple_writer_for(attribute, &block)
define_method(attribute.write_method) do |value|
if block
value = instance_exec(value, &block)
end
attribute.of(self)._write(value)
value = instance_exec(value, &block) if block
attribute.of(self).set(value)
end
end

def define_appender_for(attribute, &block)
define_method(attribute.append_method) do |value|
value = instance_exec(value, &block) if block
attribute.of(self)._append(value)
end
end

def define_multi_writer_for(attribute)
define_method(attribute.multi_write_method) do |values|
define_method(attribute.write_method) do |values|
attribute.of(self).set([])
Array(values).each do |value|
attribute.of(self).write(value)
attribute.of(self).take(value)
end
end
end
Expand Down
10 changes: 3 additions & 7 deletions lib/clamp/attribute/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ def default_method
end

def write_method
if multivalued?
"append_to_#{attribute_name}"
else
"#{attribute_name}="
end
"#{attribute_name}="
end

def multi_write_method
def append_method
if multivalued?
"#{attribute_name}="
"append_to_#{attribute_name}"
end
end

Expand Down
28 changes: 17 additions & 11 deletions lib/clamp/attribute/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,28 @@ def _read
end
end

# default implementation of write_method
def _write(value)
if attribute.multivalued?
current_values = get || []
set(current_values + [value])
else
set(value)
end
# default implementation of append_method
def _append(value)
current_values = get || []
set(current_values + [value])
end

# default implementation of write_method for multi-valued attributes
def _replace(values)
set([])
Array(values).each { |value| take(value) }
end

def read
command.send(attribute.read_method)
end

def write(value)
command.send(attribute.write_method, value)
def take(value)
if attribute.multivalued?
command.send(attribute.append_method, value)
else
command.send(attribute.write_method, value)
end
end

def default_from_environment
Expand All @@ -64,7 +70,7 @@ def default_from_environment
# Set the parameter value if it's environment variable is present
value = ENV[attribute.environment_variable]
begin
write(value)
take(value)
rescue ArgumentError => e
command.send(:signal_usage_error, "$#{attribute.environment_variable}: #{e.message}")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/clamp/option/parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def parse_options
value = option.extract_value(switch, remaining_arguments)

begin
option.of(self).write(value)
option.of(self).take(value)
rescue ArgumentError => e
signal_usage_error "option '#{switch}': #{e.message}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/clamp/parameter/parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def parse_parameters
self.class.parameters.each do |parameter|
begin
parameter.consume(remaining_arguments).each do |value|
parameter.of(self).write(value)
parameter.of(self).take(value)
end
rescue ArgumentError => e
signal_usage_error "parameter '#{parameter.name}': #{e.message}"
Expand Down
4 changes: 2 additions & 2 deletions spec/clamp/option/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@

end

describe "#write_method" do
describe "#append_method" do

it "is derived from the attribute_name" do
option.write_method.should == "append_to_header_list"
option.append_method.should == "append_to_header_list"
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/clamp/parameter/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@

end

describe "#write_method" do
describe "#append_method" do

it "is derived from the attribute_name" do
parameter.write_method.should == "append_to_file_list"
parameter.append_method.should == "append_to_file_list"
end

end
Expand Down

0 comments on commit a045c03

Please sign in to comment.