Module: Must::StructInfo::Browser

Extended by:
Browser
Includes:
Classify
Included in:
Browser
Defined in:
lib/must/struct_info.rb

Instance Method Summary collapse

Methods included from Classify

#class?, #classify

Instance Method Details

#compact(obj) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/must/struct_info.rb', line 17

def compact(obj)
  case obj
  when Hash
    obj.empty? ? Hash : Hash[*obj.first.map{|i| compact(i)}]
  when Array
    obj.empty? ? Array : [compact(obj.first)]
  else
    classify(obj)
  end
end

#same?(src, dst) ⇒ Boolean



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
# File 'lib/must/struct_info.rb', line 39

def same?(src, dst)
  case src
  when Hash
    return true if dst == Hash
    dst.must(Hash)
    return true if src.empty?
    return true if dst.empty?
    key1, val1 = src.first
    key2, val2 = dst.first
    key1.must(key2)
    val1.must(val2)

  when Array
    return true if dst == Array
    dst.must(Array)
    return true if src.empty?
    return true if dst.empty?
    src.first.must(dst.first)

  else
    # 1.must.struct(2)
    # 1.must.struct(Integer)
    dst_class = classify(dst)
    return true if classify(src).ancestors.include?(dst_class)

    # Fixnum.must.struct(2)
    return true if class?(src) and src.ancestors.include?(dst_class)

    return false
  end
  return true

rescue Must::Invalid
  return false
end

#types(obj) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/must/struct_info.rb', line 28

def types(obj)
  case obj
  when Hash
    ([Hash] + (obj.first || []).map{|i| types(i)}.flatten).uniq
  when Array
    ([Array] + obj.map{|i| types(i)}.flatten).uniq
  else
    [classify(obj)]
  end
end