Sha256: 9a65863d620f853164d91cbb5df3e75b4e2d5458f3b50fbc7c6062ad430be8e0

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

module Radiosonde::Utils
  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

  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
  module_function :collect_to_hash

  class << self
    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
  end # of class methods
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
radiosonde-0.2.0.beta5 lib/radiosonde/utils.rb
radiosonde-0.2.0.beta4 lib/radiosonde/utils.rb
radiosonde-0.2.0.beta3 lib/radiosonde/utils.rb
radiosonde-0.2.0.beta2 lib/radiosonde/utils.rb
radiosonde-0.2.0.beta lib/radiosonde/utils.rb
radiosonde-0.1.1 lib/radiosonde/utils.rb
radiosonde-0.1.0 lib/radiosonde/utils.rb