Module: Beaver::Utils

Defined in:
lib/beaver/utils.rb

Overview

Sundry utility methods for use by Beaver

Constant Summary collapse

QUOTE =

:nodoc:

'"'
ESCAPE =

:nodoc:

'\\'
EQUAL =

:nodoc:

'='
COLIN =

:nodoc:

':'
TO_SPACE =

:nodoc:

['>']
SPACE =

:nodoc:

' '
COMMA =

:nodoc:

','
LETTER_REGEX =

:nodoc:

/^[a-z]$/i

Class Method Summary collapse

Class Method Details

.deep_matching_tags(matchers, tags) ⇒ Object

Matches tags recursively



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/beaver/utils.rb', line 105

def self.deep_matching_tags(matchers, tags)
  all_tags_matched = nil
  any_arrays_matched = false
  for m in matchers
    if m.is_a? Array
      matched = deep_matching_tags m, tags
      any_arrays_matched = true if matched
    else
      matched = tags.include? m
      all_tags_matched = (matched && all_tags_matched != false) ? true : false
    end
  end
  return (all_tags_matched or any_arrays_matched)
end

.matching_hashes?(a, b) ⇒ Boolean

Recursively compares to Hashes. If all of Hash A is in Hash B, they match.

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/beaver/utils.rb', line 121

def self.matching_hashes?(a,b)
  intersecting_keys = a.keys & b.keys
  if intersecting_keys.any?
    a_values = a.values_at(*intersecting_keys)
    b_values = b.values_at(*intersecting_keys)
    indicies = (0..b_values.size-1)
    indicies.all? do |i|
      if a_values[i].is_a? String
        a_values[i] == b_values[i]
      elsif a_values[i].is_a? Array
        a_values[i] == b_values[i]
      elsif a_values[i].is_a?(Regexp) and b_values[i].is_a?(String)
        a_values[i] =~ b_values[i]
      elsif a_values[i].is_a?(Hash) and b_values[i].is_a?(Hash)
        matching_hashes? a_values[i], b_values[i]
      else
        false
      end
    end
  else
    false
  end
end

.parse_date(date) ⇒ Object

Parse a string (from a command-line arg) into a Date object



146
147
148
149
150
151
152
# File 'lib/beaver/utils.rb', line 146

def self.parse_date(date)
  case date
    when /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/ then Date.parse(date)
    when /^-\d+$/ then Date.today + date.to_i
    else nil
  end
end

.str_to_hash(str) ⇒ Object

Converts a string representation of a Hash into YAML, then into a Hash. This is targeted towards the Parameters value in Rails logs. It is assumed that every key is a represented as a String in the logs. All keys, except for numeric keys, will be converted to Symbols.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/beaver/utils.rb', line 21

def self.str_to_hash(str)
  s = ''
  indent = 0
  state = :pre_key
  i = 0
  str.each_char do |c|
    i += 1
    case c
      when QUOTE
        case state
          when :pre_key
            s << (SPACE * indent)
            s << COLIN if str[i,1] =~ LETTER_REGEX
            state = :key
            next
          when :key
            s << COLIN << SPACE
            state = :pre_val
            next
          when :pre_val, :escape
            state = :val
            next
          when :val
            state = :pre_key
            s << "\n"
            next
        end
      when LBRACE
        case state
          # Hash as a value, starting a new indent level
          when :pre_val
            state = :pre_key
            indent += 2
            s << "\n"# << (SPACE * indent)
            next
          when :pre_key
            next
        end
      when RBRACE
        case state
          when :pre_key
            indent -= 2 if indent > 0
        end
      when LBRACKET
        case state
          when :pre_val
            state = :val_array
        end
      when RBRACKET
        case state
          when :val_array
            state = :val_array_end
        end
      when ESCAPE
        if state == :val
          state = :escape
          next
        end
    end

    case state
      when :key, :val, :val_array
        s << c
      when :escape
        s << c
        state = :val
      when :val_array_end
        s << c << "\n"
        state = :pre_key
    end
  end
  YAML.load s
end

.tablize(rows) ⇒ Object

Returns an array of arrays of strings with all the columns padded the same length.



96
97
98
99
100
101
102
# File 'lib/beaver/utils.rb', line 96

def self.tablize(rows)
  max_sizes = rows.inject([0]*rows.first.size) do |sizes, vals|
    vals.map! &:to_s
    vals.each_with_index { |val, i| sizes[i] = val.size if val.size > sizes[i] }; sizes
  end
  rows.map { |vals| vals.each_with_index.map { |val, i| val.ljust(max_sizes[i]) } }
end