class StructureDigest::Digest
Public Class Methods
diff(h1,h2)
click to toggle source
# 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
new(opts={})
click to toggle source
# File lib/structure_digest/digest.rb, line 5 def initialize(opts={}) @tree = opts[:tree] || false end
Private Class Methods
gather_paths(node, solutions=[], partial_solution=SchemaParts::Path.new)
click to toggle source
# File lib/structure_digest/digest.rb, line 104 def self.gather_paths(node, solutions=[], partial_solution=SchemaParts::Path.new) if Array === node if node.empty? solutions << (partial_solution.add_part(SchemaParts::Value.new(node))) else node.each.with_index do |e, i| self.gather_paths(e, solutions, partial_solution.add_part(SchemaParts::ArrayDereference.new(i))) end end elsif Hash === node if node.empty? solutions << (partial_solution.add_part(SchemaParts::Value.new(node))) else node.each do |k,v| self.gather_paths(v, solutions, partial_solution.add_part(SchemaParts::HashDereference.new(k))) end end else solutions << (partial_solution.add_part(SchemaParts::Value.new(node))) end nil end
Public Instance Methods
add_validation(shorthand, &validateFn)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
shorthand()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/structure_digest/digest.rb, line 54 def validators_for(p) validators[p.abstract.serialize] || [] end
Private Instance Methods
deserialize(orig_shorthand)
click to toggle source
# File lib/structure_digest/digest.rb, line 127 def deserialize(orig_shorthand) SchemaParts::AbstractPath.from_shorthand(orig_shorthand) end
paths()
click to toggle source
# File lib/structure_digest/digest.rb, line 95 def paths @paths ||= [] end
validators()
click to toggle source
# File lib/structure_digest/digest.rb, line 99 def validators @validators ||= {} @validators end