string_formatter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.autotest ADDED
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ === 1.0.0 / 2011-01-11
2
+
3
+ * First (test) release! Yay!
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/string_formatter.rb
7
+ test/test_string_formatter.rb
data/README.txt ADDED
@@ -0,0 +1,92 @@
1
+ = string_formatter
2
+
3
+ * http://github.com/mark/string_formatter
4
+
5
+ == DESCRIPTION:
6
+
7
+ A gem for creating strf-style methods in any Ruby object.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Creates strf-style methods
12
+
13
+ == SYNOPSIS:
14
+
15
+ class PersonFormatter < StringFormatter
16
+
17
+ f { |p| p.first_name }
18
+ F { |p| p.first_name.upcase }
19
+ l { |p| p.last_name }
20
+ pipe { |p| 'PIPE' }
21
+
22
+ end
23
+
24
+ class UpcaseFormatter < StringFormatter
25
+
26
+ f { |p| p.first_name.upcase }
27
+ l { |p| p.last_name.upcase }
28
+
29
+ end
30
+
31
+ class Person
32
+ attr_accessor :first_name, :last_name
33
+
34
+ define_format_string :strf, :with => PersonFormatter
35
+ define_format_string :strfup, :with => UpcaseFormatter
36
+
37
+ def initialize(*names)
38
+ @first_name, @last_name = names
39
+ end
40
+
41
+ end
42
+
43
+ p = Person.new("Bob", "Smith")
44
+
45
+ p.strf('%l, %f %|')
46
+ # => "Smith, Bob PIPE"
47
+
48
+ p.strfup('%l, %f')
49
+ # => "SMITH, BOB"
50
+
51
+
52
+ == REQUIREMENTS:
53
+
54
+ * None
55
+
56
+ == INSTALL:
57
+
58
+ * gem install string_formatter
59
+
60
+ == DEVELOPERS:
61
+
62
+ After checking out the source, run:
63
+
64
+ $ rake newb
65
+
66
+ This task will install any missing dependencies, run the tests/specs,
67
+ and generate the RDoc.
68
+
69
+ == LICENSE:
70
+
71
+ (The MIT License)
72
+
73
+ Copyright (c) 2011 Mark Josef
74
+
75
+ Permission is hereby granted, free of charge, to any person obtaining
76
+ a copy of this software and associated documentation files (the
77
+ 'Software'), to deal in the Software without restriction, including
78
+ without limitation the rights to use, copy, modify, merge, publish,
79
+ distribute, sublicense, and/or sell copies of the Software, and to
80
+ permit persons to whom the Software is furnished to do so, subject to
81
+ the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be
84
+ included in all copies or substantial portions of the Software.
85
+
86
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
89
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
90
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
91
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
92
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ # Hoe.plugin :compiler
7
+ # Hoe.plugin :gem_prelude_sucks
8
+ # Hoe.plugin :inline
9
+ # Hoe.plugin :racc
10
+ # Hoe.plugin :rubyforge
11
+
12
+ Hoe.spec 'string_formatter' do
13
+ # HEY! If you fill these out in ~/.hoe_template/Rakefile.erb then
14
+ # you'll never have to touch them again!
15
+ # (delete this comment too, of course)
16
+
17
+ developer('Mark Josef', 'McPhage@gmail.com')
18
+
19
+ end
20
+
21
+ # vim: syntax=ruby
@@ -0,0 +1,132 @@
1
+ class StringFormatter
2
+
3
+ ################
4
+ # #
5
+ # Declarations #
6
+ # #
7
+ ################
8
+
9
+ VERSION = "1.0.0"
10
+
11
+ #############
12
+ # #
13
+ # Constants #
14
+ # #
15
+ #############
16
+
17
+ STRING_FORMAT_REGEX = /[^%]+|%./
18
+
19
+ SPECIAL_CHARACTER_FORMATS = {
20
+ 'ampersand' => '&',
21
+ 'at' => '@',
22
+ 'backslash' => '\\',
23
+ 'backtick' => '`',
24
+ 'bang' => '!',
25
+ 'caret' => '^',
26
+ 'cash' => '$',
27
+ 'colon' => ':',
28
+ 'comma' => ',',
29
+ 'dash' => '-',
30
+ 'double_quote' => '"',
31
+ 'eight' => '8',
32
+ 'equals' => '=',
33
+ 'five' => '5',
34
+ 'four' => '4',
35
+ 'hash' => '#',
36
+ 'left_bracket' => '[',
37
+ 'left_chevron' => '<',
38
+ 'left_paren' => '(',
39
+ 'left_stache' => '{',
40
+ 'nine' => '9',
41
+ 'one' => '1',
42
+ 'percent' => '%', # Defining this method is not recommended
43
+ 'period' => '.',
44
+ 'pipe' => '|',
45
+ 'plus' => '+',
46
+ 'quote' => "'",
47
+ 'right_bracket' => ']',
48
+ 'right_chevron' => '>',
49
+ 'right_paren' => ')',
50
+ 'right_stache' => '}',
51
+ 'semicolon' => ';',
52
+ 'seven' => '7',
53
+ 'six' => '6',
54
+ 'slash' => '/',
55
+ 'splat' => '*',
56
+ 'three' => '3',
57
+ 'tilde' => '~',
58
+ 'two' => '2',
59
+ 'underscore' => '_',
60
+ 'what' => '?',
61
+ 'zero' => '0'
62
+ }
63
+
64
+ ###############
65
+ # #
66
+ # Constructor #
67
+ # #
68
+ ###############
69
+
70
+ def initialize(object, format_string)
71
+ @object = object
72
+ @format_string = format_string
73
+ end
74
+
75
+ #################
76
+ # #
77
+ # Class Methods #
78
+ # #
79
+ #################
80
+
81
+ def self.escape_character(object, char)
82
+ escape_method = @escapes[char]
83
+ escape_method ? escape_method[object] : char
84
+ end
85
+
86
+ def self.method_missing(meth, *args, &block)
87
+ str = meth.to_s
88
+ if str.length == 1
89
+ @escapes ||= {}
90
+ @escapes[str] = block
91
+ elsif SPECIAL_CHARACTER_FORMATS[str]
92
+ @escapes ||= {}
93
+ @escapes[ SPECIAL_CHARACTER_FORMATS[str] ] = block
94
+ else
95
+ super
96
+ end
97
+ end
98
+
99
+ ####################
100
+ # #
101
+ # Instance Methods #
102
+ # #
103
+ ####################
104
+
105
+ def to_s
106
+ split_string = @format_string.scan STRING_FORMAT_REGEX
107
+
108
+ split_string.map { |str| translate_component(str) }.join
109
+ end
110
+
111
+ def translate_component(string)
112
+ if string[0...1] == '%'
113
+ char = string[1..-1]
114
+ self.class.escape_character(@object, char)
115
+ else
116
+ string
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ class Module
123
+
124
+ def define_format_string(formatter_method, options = {})
125
+ formatter_class = options[:with]
126
+
127
+ define_method formatter_method do |format_string|
128
+ formatter_class.new(self, format_string).to_s
129
+ end
130
+ end
131
+
132
+ end
@@ -0,0 +1,8 @@
1
+ require "test/unit"
2
+ require "string_formatter"
3
+
4
+ class TestStringFormatter < Test::Unit::TestCase
5
+ def test_sanity
6
+ flunk "write tests or I will kneecap you"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: string_formatter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Mark Josef
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-11 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: hoe
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 47
30
+ segments:
31
+ - 2
32
+ - 8
33
+ - 0
34
+ version: 2.8.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: A gem for creating strf-style methods in any Ruby object.
38
+ email:
39
+ - McPhage@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - History.txt
46
+ - Manifest.txt
47
+ - README.txt
48
+ files:
49
+ - .autotest
50
+ - History.txt
51
+ - Manifest.txt
52
+ - README.txt
53
+ - Rakefile
54
+ - lib/string_formatter.rb
55
+ - test/test_string_formatter.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/mark/string_formatter
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --main
63
+ - README.txt
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: string_formatter
87
+ rubygems_version: 1.4.2
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A gem for creating strf-style methods in any Ruby object.
91
+ test_files:
92
+ - test/test_string_formatter.rb