Class: StructureDigest::Digest

Inherits:
Object
  • Object
show all
Defined in:
lib/structure_digest/digest.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Digest

Returns a new instance of Digest.



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

def initialize(opts={})
  @tree = opts[:tree] || false
end

Class Method Details

.diff(h1, h2) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/structure_digest/digest.rb', line 9

def self.diff(h1,h2)
  paths1 = []
  paths2 = []
  gather_paths(h1, paths1)
  paths1 = paths1.map(&:serialize)
  gather_paths(h2, paths2)
  paths2 = paths2.map(&:serialize)

  added = (paths2 - paths1)
  removed = (paths1 - paths2)
  (added + removed).sort.map do |p|
    if removed.include?(p)
      "- #{p}"
    elsif added.include?(p)
      "+ #{p}"
    end
  end
end

Instance Method Details

#add_validation(shorthand, &validateFn) ⇒ Object



37
38
39
40
41
# File 'lib/structure_digest/digest.rb', line 37

def add_validation(shorthand, &validateFn)
  raise "isn't applicable for core paths of schema" unless @abstract_paths.include? SchemaParts::AbstractPath.from_shorthand(shorthand)
  validators[shorthand] ||= []
  validators[shorthand] << validateFn
end

#append_to_tree(tree, parts) ⇒ Object



88
89
90
91
# File 'lib/structure_digest/digest.rb', line 88

def append_to_tree(tree, parts)
  return if parts.empty?
  append_to_tree(tree[parts.first.serialize] || (tree[parts.first.serialize] = {}), parts.drop(1))
end

#injest_yml_files(file_paths) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/structure_digest/digest.rb', line 28

def injest_yml_files(file_paths)
  file_paths.each do |p|
    y = YAML.load_file(p)
    Digest.gather_paths(y, paths)
  end
  @abstract_paths = paths.map(&:abstract).uniq
  self
end

#pretty_print(io, tree, level = 0) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/structure_digest/digest.rb', line 73

def pretty_print(io, tree, level=0)
  tree.keys.sort.each do |k|
    v = tree[k]
    io << '  '*level + k
    if v.keys.empty?
      io << "\n"
    elsif v.keys.size == 1
      pretty_print(io, v, level)
    else
      io << "\n"
      pretty_print(io, v, level+1)
    end
  end
end

#shorthandObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/structure_digest/digest.rb', line 58

def shorthand
  if @tree
    root = {}
    @abstract_paths.each do |apath|
      append_to_tree(root, apath.parts)
    end
    sio = StringIO.new
    pretty_print(sio, root)
    sio.rewind
    sio.read.chomp
  else
    @abstract_paths.map(&:serialize).uniq.sort.join("\n")
  end
end

#validate(hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/structure_digest/digest.rb', line 43

def validate(hash)
  paths = []
  Digest.gather_paths(hash, paths)
  paths.all? do |p|
    print '.'
    @abstract_paths.any?{|my_p| my_p.accepts(p) } && validators_for(p).all?{|v| v.call(p.last[:value]) }
  end.tap do
    puts
  end
end

#validators_for(p) ⇒ Object



54
55
56
# File 'lib/structure_digest/digest.rb', line 54

def validators_for(p)
  validators[p.abstract.serialize] || []
end