Skip to content

Commit

Permalink
Split out text value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Dec 13, 2011
1 parent 048f0d9 commit 997568e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/semiformal/input.rb
Expand Up @@ -19,7 +19,7 @@ def html_id
"#{@prefix}_#{name}"
end

def string_value
def to_s
@value.to_s
end

Expand Down
2 changes: 1 addition & 1 deletion lib/semiformal/renderer.rb
Expand Up @@ -35,7 +35,7 @@ def input(name)
label = content_tag(:label, name.to_s.titleize, :for => html_id)
input_html = tag(:input, :type => 'text',
:name => input.param_name,
:value => input.string_value,
:value => input.to_s,
:id => html_id)
content_tag(:li, label + input_html)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/semiformal/resource.rb
@@ -1,4 +1,5 @@
require 'semiformal/input'
require 'semiformal/text_value'

module Semiformal
# Defines accepted parameters, conversions, and generated input names for
Expand Down Expand Up @@ -32,7 +33,7 @@ def to_key
def input(input_name)
Input.new(:name => input_name,
:prefix => name,
:value => target.send(input_name))
:value => TextValue.new(target.send(input_name)))
end

private
Expand Down
15 changes: 15 additions & 0 deletions lib/semiformal/text_value.rb
@@ -0,0 +1,15 @@
module Semiformal
class TextValue
def initialize(value)
@value = value
end

def to_s
@value.to_s
end

def ==(other)
to_s == other.to_s
end
end
end
4 changes: 2 additions & 2 deletions spec/semiformal/input_spec.rb
Expand Up @@ -14,8 +14,8 @@
build_input(:prefix => "user", :name => "address").html_id.should == "user_address"
end

it "has a #string_value" do
build_input(:value => 10).string_value.should == "10"
it "has a #to_s" do
build_input(:value => 10).to_s.should == "10"
end

it "is #== with the same name, prefix, and value" do
Expand Down
2 changes: 1 addition & 1 deletion spec/semiformal/resource_spec.rb
Expand Up @@ -54,7 +54,7 @@

it "sets the value" do
target.title = "Hello"
subject.string_value.should == "Hello"
subject.to_s.should == "Hello"
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions spec/semiformal/text_value_spec.rb
@@ -0,0 +1,17 @@
require 'spec_helper'
require 'semiformal/text_value'

describe Semiformal::TextValue do
it "converts #to_s" do
value = Semiformal::TextValue.new(:value)
value.to_s.should == "value"
end

it "is #== with the same value" do
Semiformal::TextValue.new("test").should == Semiformal::TextValue.new("test")
end

it "isn't #== with a different value" do
Semiformal::TextValue.new("test").should_not == Semiformal::TextValue.new("other")
end
end

0 comments on commit 997568e

Please sign in to comment.