Skip to content

Commit

Permalink
color keyword support in String#to_color
Browse files Browse the repository at this point in the history
Example: 'dark_text'.to_color -> UIColor.darkTextColor
  • Loading branch information
scizo committed Jul 10, 2012
1 parent 8e02769 commit 2f0ebb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions motion/core/string.rb
Expand Up @@ -34,6 +34,11 @@ def underscore
end

def to_color
# First check if it is a color keyword
keyword_selector = "#{self.camelize(false)}Color"
return UIColor.send(keyword_selector) if UIColor.respond_to? keyword_selector

# Next attempt to convert from hex
hex_color = self.gsub("#", "")
case hex_color.size
when 3
Expand Down
14 changes: 14 additions & 0 deletions spec/motion/core/string_spec.rb
Expand Up @@ -90,6 +90,20 @@
end
end

describe "a string with a color keyword (blue, red, lightText)" do
it "should return the corresponding color" do
'blue'.to_color.should == UIColor.blueColor
end

it "should accept camelCase" do
'lightText'.to_color.should == UIColor.lightTextColor
end

it "should accept snake_case" do
'dark_gray'.to_color.should == UIColor.darkGrayColor
end
end

describe "A UIColor should not be created from an invalid String wuth" do


Expand Down

0 comments on commit 2f0ebb8

Please sign in to comment.