1 require 'stringray/core_ext'
2 require 'stringray/includes'
4 class StringRay < Array
11 # @see StringRay::Includes#enumerate
12 # @see StringRay::Includes.whitespace=
13 # Controls how +StringRay::Includes#enumerate+ deals with whitespace by default.
15 # @param [Symbol] whitespace How to handle whitespace - :attach_before,
16 # :standalone, or :attach_after
17 def self.whitespace= whitespace
18 @@whitespace = whitespace
22 @@whitespace ||= :attach_before
26 # @see StringRay::Includes#enumerate
27 # @see StringRay::Includes.delemiters=
28 # Controls how +StringRay::Includes#enumerate+ deals with delemiters by default.
30 # @param [Symbol] delemiters How to handle delemiters - :attach_before,
31 # :standalone, or :attach_after
32 def self.delemiters= delemiters
33 @@delemiters = delemiters
37 @@delemiters ||= :attach_before
40 # @see StringRay::Word.new
41 def self.Word word; Word.new word; end
44 # @see StringRay::Includes#to_stray
47 # Enumerates a string, returning an array plain +String+s.
49 # @param [Hash] options A hash of options
50 # @yield [word] Allows each word in the string to be operated on after it is
52 # @yieldparam [String] word The last processed word
53 # @return [Array[String]] An array of words
55 def enumerate options = {}, &block
56 # TODO: Can we clean this up, into a simple #inject call? I bet so.
57 # TODO: This really should return an Enumerator object. Seriously.
59 attach_before_next = []
61 self.each do |element|
64 case options[:delemiters] || StringRay::delemiters
68 attach_before_next << element
70 if attach_before_next.empty?
72 mapped.last << element
74 attach_before_next << element
77 attach_before_next << element
82 case options[:whitespace] || StringRay::whitespace
86 attach_before_next << element
88 if attach_before_next.empty?
90 mapped.last << element
92 attach_before_next << element
95 attach_before_next << element
100 if not attach_before_next.empty?
101 mapped << [attach_before_next, element].flatten
102 attach_before_next = []
110 if not attach_before_next.empty?
111 mapped << [Word.new] unless mapped.last
112 (mapped.last << attach_before_next).flatten!
116 string = arr.map{|w|w.to_s}.join
117 yield string if block_given?
123 # A wrapper class for strings that are 'words' in and of themselves,
124 # composed of 'word characters'.
131 # @see StringRay::Whitespace.new
132 def self.Whitespace whitespace; Whitespace.new whitespace; end
135 # A wrapper class for strings that are 'whitespace' composed of 'whitespace
137 class Whitespace < String
138 Characters = [" ", "\t", "\n"]
145 # @see StringRay::Delimiter.new
146 def self.Delimiter delimiter; Delimiter.new delimiter; end
149 # A wrapper class for strings that are 'delimiters' composed of 'delimiter
151 class Delimiter < String
152 Characters = ['-', ',', '.', '?', '!', ':', ';', '/', '\\', '|']
160 "\"#{self.map(&:inspect).join ''}\""