From 48150e8171a4062f784d3bfd3ba6e9f9dee2c8d8 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Thu, 11 Jun 2009 14:42:17 -0800 Subject: [PATCH] Moving a chunk of functionality into Stringray::Includes --- lib/stringray.rb | 272 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 137 insertions(+), 135 deletions(-) diff --git a/lib/stringray.rb b/lib/stringray.rb index 5ff956f..fdf7ead 100644 --- a/lib/stringray.rb +++ b/lib/stringray.rb @@ -7,8 +7,8 @@ module StringRay @@delemiters = nil ## - # @see #enumerate - # Controls how +#enumerate+ deals with whitespace. + # @see StringRay::Includes#enumerate + # Controls how +StringRay::Includes#enumerate+ deals with whitespace. # # @param [Symbol] whitespace How to handle whitespace - :attach_before, # :standalone, or :attach_after @@ -21,8 +21,8 @@ module StringRay end ## - # @see #enumerate - # Controls how +#enumerate+ deals with delemiters. + # @see StringRay::Includes#enumerate + # Controls how +StringRay::Includes#enumerate+ deals with delemiters. # # @param [Symbol] delemiters How to handle delemiters - :attach_before, # :standalone, or :attach_after @@ -34,133 +34,6 @@ module StringRay @@delemiters ||= :attach_before end - ## - # Splits a string into an array of +StringRay+ container objects (+Word+, - # +Whitespace+, and +Delimiter+). - # - # @yield [element] Allows each 'element' of the string to be operated on - # after it is processed - # @yieldparam [Word, Whitespace, Delimiter] element The last processed - # string 'element' - # @return [Array[Word, Whitespace, Delimiter]] An array of +StringRay+ - # container objects - # @since 2 - def to_stray - ray = [] - new_element = lambda do |element| - yield ray.last if block_given? unless ray.empty? - ray << element - end - - self.scan(/./um) do |char| - if Delimiter::Characters.include? char - new_element[Delimiter.new(char)] - - elsif Whitespace::Characters.include? char - if ray.last.is_a? Whitespace - ray.last << char - else - new_element[Whitespace.new(char)] - end - - else - if ray.last.is_a? Word - ray.last << char - else - new_element[Word.new(char)] - end - - end - end - - if block_given? - yield ray.last - self - else - ray - end - end - - ## - # @see #to_stray - # @see #whitespace - # @see #delemiters - # Enumerates a string, similar to +#to_stray+, but returning an array of - # plain +String+s instead of container objects. - # - # @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 - mapped = [] - attach_before_next = [] - - self.to_stray 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 - - # @deprecated - alias_method :each_word, :enumerate # @see StringRay::Word.new def Word word; Word.new word; end @@ -203,11 +76,140 @@ module StringRay end # This is mixed into any class including +StringRay+. It exposes - # +::make_enumerable!+ to said class. - module Extendables + # +::make_enumerable!+ to said class, and exposes +#to_stray+ and + # +#enumerate+ to said class's instances. + module Includes + + ## + # Splits a string into an array of +StringRay+ container objects (+Word+, + # +Whitespace+, and +Delimiter+). + # + # @yield [element] Allows each 'element' of the string to be operated on + # after it is processed + # @yieldparam [Word, Whitespace, Delimiter] element The last processed + # string 'element' + # @return [StringRay[Word, Whitespace, Delimiter]] An array of +StringRay+ + # container objects + # @since 2 + def to_stray + ray = [] + new_element = lambda do |element| + yield ray.last if block_given? unless ray.empty? + ray << element + end + + self.scan(/./um) do |char| + if Delimiter::Characters.include? char + new_element[Delimiter.new(char)] + + elsif Whitespace::Characters.include? char + if ray.last.is_a? Whitespace + ray.last << char + else + new_element[Whitespace.new(char)] + end + + else + if ray.last.is_a? Word + ray.last << char + else + new_element[Word.new(char)] + end + + end + end + + if block_given? + yield ray.last + self + else + ray + end + end + + ## + # @see #to_stray + # @see StringRay#whitespace + # @see StringRay#delemiters + # Enumerates a string, similar to +#to_stray+, but returning an array of + # plain +String+s instead of a +StringRay+ container object. + # + # @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 + mapped = [] + attach_before_next = [] + + self.to_stray 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 + + # @deprecated + alias_method :each_word, :enumerate ## - # This overrides +String#each+ with +StringRay#enumerate+, thus allowing + # This overrides +String#each+ with +#enumerate+, thus allowing # us to include +Enumerable+. Be careful, this breaks lots of existing # code which depends on the old methodology of +String#each+! The # overridden +String#each+ functionality will be exposed as @@ -227,6 +229,6 @@ module StringRay end def self.included klass - klass.send :extend, Extendables + klass.send :extend, Includes end end -- 2.11.4.GIT