-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstringray.rb
More file actions
163 lines (139 loc) · 4.02 KB
/
stringray.rb
File metadata and controls
163 lines (139 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'stringray/includes'
class StringRay < Array
Version = 3
@@whitespace = nil
@@delemiters = nil
##
# @see StringRay::Includes#enumerate
# @see StringRay::Includes.whitespace=
# Controls how +StringRay::Includes#enumerate+ deals with whitespace by default.
#
# @param [Symbol] whitespace How to handle whitespace - :attach_before,
# :standalone, or :attach_after
def self.whitespace= whitespace
@@whitespace = whitespace
end
def self.whitespace
@@whitespace ||= :attach_before
end
##
# @see StringRay::Includes#enumerate
# @see StringRay::Includes.delemiters=
# Controls how +StringRay::Includes#enumerate+ deals with delemiters by default.
#
# @param [Symbol] delemiters How to handle delemiters - :attach_before,
# :standalone, or :attach_after
def self.delemiters= delemiters
@@delemiters = delemiters
end
def self.delemiters
@@delemiters ||= :attach_before
end
# @see StringRay::Word.new
def self.Word word; Word.new word; end
##
# @see StringRay::Includes#to_stray
# @see #whitespace
# @see #delemiters
# Enumerates a string, returning an array plain +String+s.
#
# @param [Hash] options A hash of options
# @yield [word] Allows each word in the string to be operated on after it is
# processed
# @yieldparam [String] word The last processed word
# @return [Array[String]] An array of words
# @since 1
def enumerate options = {}, &block
# TODO: Can we clean this up, into a simple #inject call? I bet so.
# TODO: This really should return an Enumerator object. Seriously.
mapped = []
attach_before_next = []
self.each do |element|
case element
when Delimiter
case options[:delemiters] || StringRay::delemiters
when :standalone
mapped << [element]
when :attach_after
attach_before_next << element
else
if attach_before_next.empty?
if mapped.last
mapped.last << element
else
attach_before_next << element
end
else
attach_before_next << element
end
end
when Whitespace
case options[:whitespace] || StringRay::whitespace
when :standalone
mapped << [element]
when :attach_after
attach_before_next << element
else
if attach_before_next.empty?
if mapped.last
mapped.last << element
else
attach_before_next << element
end
else
attach_before_next << element
end
end
when Word
if not attach_before_next.empty?
mapped << [attach_before_next, element].flatten
attach_before_next = []
else
mapped << [element]
end
end
end
if not attach_before_next.empty?
mapped << [Word.new] unless mapped.last
(mapped.last << attach_before_next).flatten!
end
mapped.map do |arr|
string = arr.map{|w|w.to_s}.join
yield string if block_given?
string
end
end
##
# A wrapper class for strings that are 'words' in and of themselves,
# composed of 'word characters'.
class Word < String
def inspect
"(#{self})"
end
end
# @see StringRay::Whitespace.new
def self.Whitespace whitespace; Whitespace.new whitespace; end
##
# A wrapper class for strings that are 'whitespace' composed of 'whitespace
# characters'.
class Whitespace < String
Characters = [" ", "\t", "\n"]
def inspect
"#{self}"
end
end
# @see StringRay::Delimiter.new
def self.Delimiter delimiter; Delimiter.new delimiter; end
##
# A wrapper class for strings that are 'delimiters' composed of 'delimiter
# characters'.
class Delimiter < String
Characters = ['-', ',', '.', '?', '!', ':', ';', '/', '\\', '|']
def inspect
"<#{self}>"
end
end
def inspect
"\"#{self.map(&:inspect).join ''}\""
end
end