Module: Crimp

Defined in:
lib/crimp.rb,
lib/crimp/version.rb

Constant Summary collapse

VERSION =
'0.1.2'.freeze

Class Method Summary collapse

Class Method Details

.convert(obj) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/crimp.rb', line 15

def self.convert(obj)
  case obj
  when Array
    parse_array obj
  when Hash
    parse_hash obj
  when String
    obj
  else
    to_string obj
  end
end

.hash_to_array(hash) ⇒ Object



28
29
30
31
32
# File 'lib/crimp.rb', line 28

def self.hash_to_array(hash)
  [].tap do |a|
    hash.each { |k, v| a << pair_to_string(k, v) }
  end
end

.pair_to_string(k, v) ⇒ Object



34
35
36
# File 'lib/crimp.rb', line 34

def self.pair_to_string(k, v)
  "#{stringify k}=>#{stringify v}"
end

.parse_array(array) ⇒ Object



38
39
40
# File 'lib/crimp.rb', line 38

def self.parse_array(array)
  array.map { |e| stringify(e) }.sort
end

.parse_hash(hash) ⇒ Object



42
43
44
# File 'lib/crimp.rb', line 42

def self.parse_hash(hash)
  stringify hash_to_array(hash)
end

.signature(obj) ⇒ Object



5
6
7
# File 'lib/crimp.rb', line 5

def self.signature(obj)
  Digest::MD5.hexdigest stringify(obj)
end

.stringify(obj) ⇒ Object



9
10
11
# File 'lib/crimp.rb', line 9

def self.stringify(obj)
  convert(obj).tap { |o| return o.class == String ? o : to_string(o) }
end

.to_string(obj) ⇒ Object



46
47
48
# File 'lib/crimp.rb', line 46

def self.to_string(obj)
  "#{obj}#{obj.class}"
end