Module: Radiosonde::Utils

Included in:
Client, Exporter
Defined in:
lib/radiosonde/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.collect_to_hash(collection, *key_attrs) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/radiosonde/utils.rb', line 16

def collect_to_hash(collection, *key_attrs)
  opts = key_attrs.last.kind_of?(Hash) ? key_attrs.pop : {}
  hash = {}

  collection.each do |item|
    if block_given?
      key = yield(item)
    else
      key = key_attrs.map {|k| item.send(k) }
      key = key.first if key_attrs.length == 1
    end

    if opts[:has_many]
      hash[key] ||= []
      hash[key] << item
    else
      hash[key] = item
    end
  end

  return hash
end

.diff(obj1, obj2, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/radiosonde/utils.rb', line 41

def diff(obj1, obj2, options = {})
  diffy = Diffy::Diff.new(
    obj1.pretty_inspect,
    obj2.pretty_inspect,
    :diff => '-u'
  )

  out = diffy.to_s(options[:color] ? :color : :text).gsub(/\s+\z/m, '')
  out.gsub!(/^/, options[:indent]) if options[:indent]
  out
end

Instance Method Details

#matched?(name, include_r, exclude_r) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/radiosonde/utils.rb', line 2

def matched?(name, include_r, exclude_r)
  result = true

  if exclude_r
    result &&= name !~ exclude_r
  end

  if include_r
    result &&= name =~ include_r
  end

  result
end