cheddar 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cheddar.rb +111 -23
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e8151013fa487bc106e835cac6b8ac23e4e13e9
|
4
|
+
data.tar.gz: f4de428cc0e68549f76f6e1a79a067f1311c64a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c4e72de10182b35e8d05ca5135c1a1201d16d29bd35774842ddbb150b6de1ec140c0dd5dc5641fccc302b78d1ebeabcc62dcc10f58748a9035f9d1b7172d055
|
7
|
+
data.tar.gz: 757311abc64a95866954612f23eca49e1330829bea585ab9c7dda82e30efe87e73b81e2f03f49b5725523b67aa215f2b90d868253360577c598898727b61293a
|
data/lib/cheddar.rb
CHANGED
@@ -1,66 +1,140 @@
|
|
1
|
+
#
|
2
|
+
# The +Cheddar+ class converts human input strings to numbers.
|
3
|
+
#
|
1
4
|
class Cheddar
|
2
|
-
|
3
5
|
attr_accessor :enabled_dialects
|
4
6
|
|
5
|
-
def initialize
|
7
|
+
def initialize #:notnew: nothing special going on here
|
6
8
|
@mapping = {}
|
7
9
|
@enabled_dialects = []
|
8
10
|
end
|
9
11
|
|
12
|
+
#
|
13
|
+
# Get the list of currently available dialects
|
14
|
+
#
|
10
15
|
def available_dialects
|
11
|
-
|
16
|
+
mapping.keys
|
12
17
|
end
|
13
18
|
|
19
|
+
#
|
20
|
+
# Add human_to_number method to the String class
|
21
|
+
#
|
14
22
|
def cheddarize
|
15
23
|
String.send(:define_method, :human_to_number) do
|
16
24
|
Cheddar.parse(self)
|
17
25
|
end
|
18
26
|
end
|
19
27
|
|
28
|
+
# Define a word to number mapping for the current dialect
|
29
|
+
#
|
30
|
+
# :call-seq:
|
31
|
+
# define(word, number) -> number
|
32
|
+
#
|
33
|
+
# ==== Parameters
|
34
|
+
#
|
35
|
+
# * +word+ - string to be mapped to number
|
36
|
+
# * +number+ - Number to return when an occurrence of string is found
|
37
|
+
#
|
20
38
|
def define(word, number)
|
21
|
-
|
22
|
-
|
39
|
+
mapping[current_dialect] ||= {}
|
40
|
+
mapping[current_dialect][word] = number
|
23
41
|
end
|
24
42
|
|
43
|
+
# Set the current dialect being operated on when using the define method
|
44
|
+
# and optionally pass in a block to define the dialect
|
45
|
+
#
|
46
|
+
# :call-seq:
|
47
|
+
# dialect(dialect_name) -> {'word' => value...}
|
48
|
+
# dialect(dialect_name){ block } -> {'word' => value...}
|
49
|
+
#
|
50
|
+
# ==== Parameters
|
51
|
+
#
|
52
|
+
# * +dialect_name+ - symbol for dialect being worked on
|
53
|
+
#
|
54
|
+
# ==== Example
|
55
|
+
#
|
56
|
+
# ch = Cheddar.new
|
57
|
+
# ch.dialect(:example) do |d|
|
58
|
+
# d.define 'e', 20
|
59
|
+
# end
|
60
|
+
# ch.parse('1 e') #=> 20
|
61
|
+
#
|
25
62
|
def dialect(dia)
|
26
|
-
|
63
|
+
self.current_dialect = dia
|
27
64
|
yield self.class.instance if block_given?
|
28
|
-
|
65
|
+
mapping[dia]
|
29
66
|
end
|
30
67
|
|
68
|
+
#
|
69
|
+
# convert the given string to it's numeric equivalent
|
70
|
+
#
|
71
|
+
# ==== Parameters
|
72
|
+
#
|
73
|
+
# * +string+ - string to be parsed as a number
|
74
|
+
#
|
31
75
|
def parse(string)
|
32
76
|
str = string.downcase
|
33
77
|
|
34
|
-
|
35
|
-
|
36
|
-
unless num_name.is_a? Regexp
|
37
|
-
str.gsub!(Regexp.new("(^|\s)#{num_name.to_s}($|\s)"), "* #{value}")
|
38
|
-
else
|
39
|
-
str.gsub!(num_name, "* #{value}")
|
40
|
-
end
|
41
|
-
end
|
78
|
+
enabled_dialects.each do |d|
|
79
|
+
mapping[d].each { |num_name, value| apply_mapping(num_name, value, str) }
|
42
80
|
end
|
43
81
|
|
44
82
|
str.gsub!(/[^\d\*\.]/, '')
|
45
83
|
|
46
|
-
eval(str)
|
84
|
+
Kernel.eval(str)
|
47
85
|
end
|
48
86
|
|
87
|
+
#
|
88
|
+
# Add human_to_number method to the String class using the Cheddar singleton
|
89
|
+
#
|
49
90
|
def self.cheddarize
|
50
|
-
|
91
|
+
instance.cheddarize
|
51
92
|
end
|
52
93
|
|
94
|
+
# Get/Configure the Cheddar instance
|
95
|
+
#
|
96
|
+
# :call-seq:
|
97
|
+
# Cheddar.config -> instance
|
98
|
+
# Cheddar.config { block } -> instance
|
99
|
+
#
|
100
|
+
# ==== Example
|
101
|
+
#
|
102
|
+
# Cheddar.config do |c|
|
103
|
+
# ch.dialect(:example) do |d|
|
104
|
+
# d.define 'e', 20
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
#
|
53
108
|
def self.config
|
54
|
-
yield
|
55
|
-
|
109
|
+
yield instance if block_given?
|
110
|
+
instance
|
56
111
|
end
|
57
112
|
|
113
|
+
#
|
114
|
+
# Get the singleton instance of cheddar applied by cheddarize
|
115
|
+
#
|
58
116
|
def self.instance
|
59
117
|
@instance ||= new
|
60
118
|
end
|
61
119
|
|
120
|
+
#
|
121
|
+
# Parse using the singleton instance
|
122
|
+
#
|
62
123
|
def self.parse(string)
|
63
|
-
|
124
|
+
instance.parse(string)
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
attr_reader :mapping
|
130
|
+
attr_accessor :current_dialect
|
131
|
+
|
132
|
+
def apply_mapping(num_name, value, str)
|
133
|
+
if num_name.is_a? Regexp
|
134
|
+
str.gsub!(num_name, "* #{value}")
|
135
|
+
else
|
136
|
+
str.gsub!(Regexp.new("(^|\s)#{num_name}($|\s)"), "* #{value}")
|
137
|
+
end
|
64
138
|
end
|
65
139
|
end
|
66
140
|
|
@@ -69,13 +143,16 @@ Cheddar.config do |c|
|
|
69
143
|
c.dialect(:en_us) do |d|
|
70
144
|
d.define :hundred, 100
|
71
145
|
d.define :thousand, 1000
|
72
|
-
%w(m b tr quadr quint sext sept oct non dec undec duodec tredec quattuordec
|
146
|
+
%w(m b tr quadr quint sext sept oct non dec undec duodec tredec quattuordec
|
147
|
+
quindec sexdec septemdec octodec novemdec vigint).reduce(1000) do |m, p|
|
73
148
|
d.define "#{p}illion", 1000 * m
|
74
149
|
end
|
75
150
|
end
|
76
151
|
|
77
152
|
c.dialect(:slang_us) do |d|
|
78
|
-
%w(bacon bills bread buck cabbage cash cheddar cheese clams dolla dollar
|
153
|
+
%w(bacon bills bread buck cabbage cash cheddar cheese clams dolla dollar
|
154
|
+
dough green greenback kale lettuce loot moolah paper potato
|
155
|
+
potatoes scratch scrip).each { |p| d.define p, 1 }
|
79
156
|
%w(benjamin c-note jackson twankie).each { |p| d.define p, 100 }
|
80
157
|
d.define 'dead presidents', 1
|
81
158
|
d.define 'long green', 1
|
@@ -86,6 +163,17 @@ Cheddar.config do |c|
|
|
86
163
|
d.define 'big ones', 1000
|
87
164
|
end
|
88
165
|
|
89
|
-
c.
|
166
|
+
c.dialect(:slang_gb) do |d|
|
167
|
+
%w(alan cherry maggie nicker nugget pound quid sov).each do |p|
|
168
|
+
d.define p, 1
|
169
|
+
end
|
170
|
+
%w(plenty purple score).each { |p| d.define p, 20 }
|
171
|
+
%w(bullseye mcgarret nifty pinky thrifty).each { |p| d.define p, 50 }
|
172
|
+
%w(cenny century longun oneer).each { |p| d.define p, 100 }
|
173
|
+
%w(bag gorilla grand large rio).each { |p| d.define p, 1000 }
|
174
|
+
d.define 'pony', 25
|
175
|
+
end
|
176
|
+
|
177
|
+
c.enabled_dialects = [:en_us, :slang_us, :slang_gb]
|
90
178
|
|
91
179
|
end
|
metadata
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cheddar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenny Parnell
|
8
8
|
- Jonathan Roes
|
9
|
+
- Alex Forbes-Reed
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2014-
|
13
|
+
date: 2014-10-15 00:00:00.000000000 Z
|
13
14
|
dependencies: []
|
14
|
-
description:
|
15
|
+
description: |2
|
16
|
+
Convert human readable strings to numbers.
|
17
|
+
Also handles gangster terminology.
|
15
18
|
email: k.parnell@gmail.com
|
16
19
|
executables: []
|
17
20
|
extensions: []
|
@@ -43,3 +46,4 @@ signing_key:
|
|
43
46
|
specification_version: 4
|
44
47
|
summary: Little library to convert human readable strings to numbers.
|
45
48
|
test_files: []
|
49
|
+
has_rdoc:
|