Module: StringRay

Defined in:
lib/stringray.rb

Defined Under Namespace

Modules: Extendables Classes: Delimiter, Whitespace, Word

Constant Summary collapse

VERSION =
2
@@whitespace =
nil
@@delemiters =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delemitersObject



31
32
33
# File 'lib/stringray.rb', line 31

def self.delemiters
  @@delemiters ||= :attach_before
end

.delemiters=(delemiters) ⇒ Object

Controls how #enumerate deals with delemiters.

Parameters:

  • delemiters (Symbol)

    How to handle delemiters - :attach_before, :standalone, or :attach_after

See Also:



27
28
29
# File 'lib/stringray.rb', line 27

def self.delemiters= delemiters
  @@delemiters = delemiters
end

.included(klass) ⇒ Object



224
225
226
# File 'lib/stringray.rb', line 224

def self.included klass
  klass.send :extend, Extendables
end

.whitespaceObject



17
18
19
# File 'lib/stringray.rb', line 17

def self.whitespace
  @@whitespace ||= :attach_before
end

.whitespace=(whitespace) ⇒ Object

Controls how #enumerate deals with whitespace.

Parameters:

  • whitespace (Symbol)

    How to handle whitespace - :attach_before, :standalone, or :attach_after

See Also:



13
14
15
# File 'lib/stringray.rb', line 13

def self.whitespace= whitespace
  @@whitespace = whitespace
end

Instance Method Details

#Delimiter(delimiter) ⇒ Object

See Also:

  • StringRay::Delimiter#new


190
# File 'lib/stringray.rb', line 190

def Delimiter delimiter; Delimiter.new delimiter; end

#enumerate(options = {}) {|word| ... } ⇒ Array[String] Also known as: each_word

Enumerates a string, similar to #to_stray, but returning an array of plain Strings instead of container objects.

Parameters:

  • options (Hash) (defaults to: {})

    A hash of options

Yields:

  • (word)

    Allows each word in the string to be operated on after it is processed

Yield Parameters:

  • word (String)

    The last processed word

Returns:

  • (Array[String])

    An array of words

See Also:

Since:

  • 1



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
# File 'lib/stringray.rb', line 95

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

#to_stray {|element| ... } ⇒ Array[Word, Whitespace, Delimiter]

Splits a string into an array of StringRay container objects (Word, Whitespace, and Delimiter).

Yields:

  • (element)

    Allows each ‘element’ of the string to be operated on after it is processed

Yield Parameters:

Returns:

Since:

  • 2



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
# File 'lib/stringray.rb', line 46

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

#Whitespace(whitespace) ⇒ Object

See Also:

  • StringRay::Whitespace#new


176
# File 'lib/stringray.rb', line 176

def Whitespace whitespace; Whitespace.new whitespace; end

#Word(word) ⇒ Object

See Also:

  • StringRay::Word#new


164
# File 'lib/stringray.rb', line 164

def Word word; Word.new word; end