1 require 'stringray/includes'
3 class StringRay < Array
10 # @see StringRay::Includes#enumerate
11 # @see StringRay::Includes.whitespace=
12 # Controls how +StringRay::Includes#enumerate+ deals with whitespace by default.
14 # @param [Symbol] whitespace How to handle whitespace - :attach_before,
15 # :standalone, or :attach_after
16 def self.whitespace= whitespace
17 @@whitespace = whitespace
21 @@whitespace ||= :attach_before
25 # @see StringRay::Includes#enumerate
26 # @see StringRay::Includes.delemiters=
27 # Controls how +StringRay::Includes#enumerate+ deals with delemiters by default.
29 # @param [Symbol] delemiters How to handle delemiters - :attach_before,
30 # :standalone, or :attach_after
31 def self.delemiters= delemiters
32 @@delemiters = delemiters
36 @@delemiters ||= :attach_before
39 # @see StringRay::Word.new
40 def self.Word word; Word.new word; end
43 # @see StringRay::Includes#to_stray
46 # Enumerates a string, returning an array plain +String+s.
48 # @param [Hash] options A hash of options
49 # @yield [word] Allows each word in the string to be operated on after it is
51 # @yieldparam [String] word The last processed word
52 # @return [Array[String]] An array of words
54 def enumerate options = {}, &block
55 # TODO: Can we clean this up, into a simple #inject call? I bet so.
56 # TODO: This really should return an Enumerator object. Seriously.
58 attach_before_next = []
60 self.each do |element|
63 case options[:delemiters] || StringRay::delemiters
67 attach_before_next << element
69 if attach_before_next.empty?
71 mapped.last << element
73 attach_before_next << element
76 attach_before_next << element
81 case options[:whitespace] || StringRay::whitespace
85 attach_before_next << element
87 if attach_before_next.empty?
89 mapped.last << element
91 attach_before_next << element
94 attach_before_next << element
99 if not attach_before_next.empty?
100 mapped << [attach_before_next, element].flatten
101 attach_before_next = []
109 if not attach_before_next.empty?
110 mapped << [Word.new] unless mapped.last
111 (mapped.last << attach_before_next).flatten!
115 string = arr.map{|w|w.to_s}.join
116 yield string if block_given?
122 # A wrapper class for strings that are 'words' in and of themselves,
123 # composed of 'word characters'.
130 # @see StringRay::Whitespace.new
131 def self.Whitespace whitespace; Whitespace.new whitespace; end
134 # A wrapper class for strings that are 'whitespace' composed of 'whitespace
136 class Whitespace < String
137 Characters = [" ", "\t", "\n"]
144 # @see StringRay::Delimiter.new
145 def self.Delimiter delimiter; Delimiter.new delimiter; end
148 # A wrapper class for strings that are 'delimiters' composed of 'delimiter
150 class Delimiter < String
151 Characters = ['-', ',', '.', '?', '!', ':', ';', '/', '\\', '|']
159 "\"#{self.map(&:inspect).join ''}\""