Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/extend/array.rb

Overview

Reopen the core ruby Array class and add the below methods to it.

Case Sensitivity rules for [ALL] the below methods that are added to the core Ruby string class.

For case insensitive behaviour make sure you downcase both the string object and the parameter strings (or strings within other parameter objects, like arrays and hashes).

Instance Method Summary collapse

Instance Method Details

#alphanumeric_unionString

The returned string is a result of a union (join) of all the (expected) string array elements followed by the deletion of all non alphanumeric characters.

Disambiguating the String for Cross Platform Use

This behaviour is typically used for transforming text that is about to be signed or digested (hashed). Removing all the non alpha-numeric characters disambiguates the string.

An example is the exclusion of line ending characters which in Windows are different from Linux.

This disambiguation means that signing functions will return the same result on widely variant platfoms like Windows vs CoreOS.

Returns:

  • (String)

    Returns the alphanumeric union of the strings within this array.

Raises:

  • (ArgumentError)

    if the array is nil or empty. Also an error will be thrown if the array contains objects that cannot be naturally converted to a string.



38
39
40
41
# File 'lib/utils/extend/array.rb', line 38

def alphanumeric_union
  raise ArgumentError, "Cannot do alphanumeric union on an empty array." if self.empty?
  return self.join.to_alphanumeric
end

#before_and_after(begin_delimeter, end_delimeter) ⇒ Object

Get the text [in between] this and that delimeter [exclusively]. Exclusively means the returned text [does not] include either of the matched delimeters (although an unmatched instance of [this] delimeter may appear in the in-between text).


Multiple Delimiters


When multiple delimiters exist, the text returned is in between the

[a] - first occurrence of [this] delimeter AND the
[b] - 1st occurrence of [that] delimeter [AFTER] the 1st delimiter

Instances of [that] delimiter occurring before [this] are ignored. The text could contain [this] delimeter instances but is guaranteed not to contain a [that] delimeter.


Parameters


this_delimiter : begin delimeter (not included in returned string)
that_delimiter : end delimeter (not included in returned string)

Exceptions


An exception (error) will be thrown if

=> any nil (or empties) exist in the input parameters
=> [this] delimeter does not appear in the in_string
=> [that] delimeter does not appear after [this] one


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
# File 'lib/utils/extend/array.rb', line 96

def before_and_after begin_delimeter, end_delimeter

  Throw.if_nil_or_empty_strings [ self, begin_delimeter, end_delimeter ]

  before_after_lines = []
  in_middle_bit = false

  self.each do |candidate_line|

    is_middle_boundary = !in_middle_bit && candidate_line.downcase.include?(begin_delimeter.downcase)
    if is_middle_boundary
      in_middle_bit = true
      next
    end

    unless in_middle_bit
      before_after_lines.push candidate_line
      next
    end

    #--
    #-- Now we are definitely in the middle bit.
    #-- Let's check for the middle end delimeter
    #--
    if candidate_line.downcase.include? end_delimeter.downcase
      in_middle_bit = false
    end

  end

  return before_after_lines

end

#log_linesObject

Log the array using our logging mixin by printing every array item into its own log line. In most cases we (the array) are a list of strings, however if not, each item’s to_string method is invoked and the result printed using one log line.

The INFO log level is used to log the lines - if this is not

appropriate create a (level) parameterized log lines method.


51
52
53
54
55
56
57
58
# File 'lib/utils/extend/array.rb', line 51

def log_lines

  self.each do |line|
    clean_line = line.to_s.chomp.gsub("\\n","")
    log.info(x) { line } if clean_line.length > 0
  end

end

#middlle_bit(begin_delimeter, end_delimeter) ⇒ Object



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
# File 'lib/utils/extend/array.rb', line 131

def middlle_bit begin_delimeter, end_delimeter

  Throw.if_nil_or_empty_strings [ self, begin_delimeter, end_delimeter ]

  middle_lines = []
  in_middle_bit = false

  self.each do |candidate_line|

    is_middle_boundary = !in_middle_bit && candidate_line.downcase.include?(begin_delimeter.downcase)
    if is_middle_boundary
      in_middle_bit = true
      next
    end

    end_of_middle = in_middle_bit && candidate_line.downcase.include?(end_delimeter.downcase)
    return middle_lines if end_of_middle
    
    #--
    #-- We are definitely in the middle bit.
    #--
    middle_lines.push(candidate_line) if in_middle_bit

  end

  unreachable_str = "This point should be unreachable unless facts are ended."
  raise RuntimeError.new unreachable_str

end