Skip to content

Commit 0b98554

Browse files
committed
Rearranged the project, added tests
1 parent c6361d8 commit 0b98554

File tree

6 files changed

+113
-54
lines changed

6 files changed

+113
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'rake/testtask'
2+
3+
Rake::TestTask.new do |t|
4+
t.libs.push %w(lib spec)
5+
t.pattern = 'spec/*_spec.rb'
6+
end

lib/formattable.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Formattable
2+
3+
module ClassMethods
4+
5+
def define_format_string(formatter_method, options = {}, &block)
6+
formatter_class = options.fetch(:with) { StringFormatter }
7+
8+
define_method formatter_method do |format_string|
9+
formatter_class.new(self, format_string).to_s
10+
end
11+
end
12+
13+
end
14+
15+
def self.included(base)
16+
base.send :extend, ClassMethods
17+
end
18+
19+
end
Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative 'formattable'
2+
13
class StringFormatter
24

35
#############
@@ -9,49 +11,50 @@ class StringFormatter
911
STRING_FORMAT_REGEX = /[^%]+|%./
1012

1113
SPECIAL_CHARACTER_FORMATS = {
12-
'ampersand' => '&',
13-
'at' => '@',
14-
'backslash' => '\\',
15-
'backtick' => '`',
16-
'bang' => '!',
17-
'caret' => '^',
18-
'cash' => '$',
19-
'colon' => ':',
20-
'comma' => ',',
21-
'dash' => '-',
22-
'double_quote' => '"',
23-
'eight' => '8',
24-
'equals' => '=',
25-
'five' => '5',
26-
'four' => '4',
27-
'hash' => '#',
28-
'left_bracket' => '[',
29-
'left_chevron' => '<',
30-
'left_paren' => '(',
31-
'left_stache' => '{',
32-
'nine' => '9',
33-
'one' => '1',
34-
'percent' => '%', # Defining this method is not recommended
35-
'period' => '.',
36-
'pipe' => '|',
37-
'plus' => '+',
38-
'quote' => "'",
14+
'ampersand' => '&',
15+
'at' => '@',
16+
'backslash' => '\\',
17+
'backtick' => '`',
18+
'bang' => '!',
19+
'caret' => '^',
20+
'cash' => '$',
21+
'colon' => ':',
22+
'comma' => ',',
23+
'dash' => '-',
24+
'double_quote' => '"',
25+
'eight' => '8',
26+
'equals' => '=',
27+
'five' => '5',
28+
'four' => '4',
29+
'hash' => '#',
30+
'left_bracket' => '[',
31+
'left_chevron' => '<',
32+
'left_paren' => '(',
33+
'left_brace' => '{',
34+
'nine' => '9',
35+
'one' => '1',
36+
# Defining this method is not recommended:
37+
'percent' => '%',
38+
'period' => '.',
39+
'pipe' => '|',
40+
'plus' => '+',
41+
'quote' => "'",
3942
'right_bracket' => ']',
4043
'right_chevron' => '>',
41-
'right_paren' => ')',
42-
'right_stache' => '}',
43-
'semicolon' => ';',
44-
'seven' => '7',
45-
'six' => '6',
46-
'slash' => '/',
47-
'splat' => '*',
48-
'three' => '3',
49-
'tilde' => '~',
50-
'two' => '2',
51-
'underscore' => '_',
52-
'what' => '?',
53-
'zero' => '0'
54-
}
44+
'right_paren' => ')',
45+
'right_brace' => '}',
46+
'semicolon' => ';',
47+
'seven' => '7',
48+
'six' => '6',
49+
'slash' => '/',
50+
'splat' => '*',
51+
'three' => '3',
52+
'tilde' => '~',
53+
'two' => '2',
54+
'underscore' => '_',
55+
'what' => '?',
56+
'zero' => '0'
57+
}.freeze
5558

5659
###############
5760
# #
@@ -87,7 +90,7 @@ def self.method_missing(meth, *args, &block)
8790
super
8891
end
8992
end
90-
93+
9194
####################
9295
# #
9396
# Instance Methods #
@@ -110,15 +113,3 @@ def translate_component(string)
110113
end
111114

112115
end
113-
114-
class Module
115-
116-
def define_format_string(formatter_method, options = {})
117-
formatter_class = options[:with]
118-
119-
define_method formatter_method do |format_string|
120-
formatter_class.new(self, format_string).to_s
121-
end
122-
end
123-
124-
end

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'rubygems'
2+
require 'minitest/spec'
3+
require 'minitest/autorun'
4+
5+
require 'string_formatter'

spec/string_formatter_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'spec_helper'
2+
3+
describe StringFormatter do
4+
5+
class PersonFormatter < StringFormatter
6+
f { |p| p.first_name }
7+
F { |p| p.first_name.upcase }
8+
l { |p| p.last_name }
9+
10+
pipe { |p| 'PIPE' }
11+
end
12+
13+
class UpcaseFormatter < StringFormatter
14+
f { |p| p.first_name.upcase }
15+
l { |p| p.last_name.upcase }
16+
end
17+
18+
Person = Struct.new(:first_name, :last_name) do
19+
include Formattable
20+
21+
define_format_string :strf, :with => PersonFormatter
22+
define_format_string :strfup, :with => UpcaseFormatter
23+
end
24+
25+
it "should generate the right string format" do
26+
p = Person.new("Bob", "Smith")
27+
28+
p.strfup('%l, %f').must_equal "SMITH, BOB"
29+
end
30+
31+
it "should handle special characters" do
32+
p = Person.new("Bob", "Smith")
33+
34+
p.strf('%l, %f %|').must_equal "Smith, Bob PIPE"
35+
end
36+
37+
end

0 commit comments

Comments
 (0)