Class: StructureDigest::Digest
- Inherits:
-
Object
- Object
- StructureDigest::Digest
- Defined in:
- lib/structure_digest/digest.rb
Instance Method Summary collapse
- #add_validation(shorthand, &validateFn) ⇒ Object
- #append_to_tree(tree, parts) ⇒ Object
-
#initialize(opts = {}) ⇒ Digest
constructor
A new instance of Digest.
- #injest_yml_files(file_paths) ⇒ Object
- #pretty_print(io, tree, level = 0) ⇒ Object
- #shorthand ⇒ Object
- #validate(hash) ⇒ Object
- #validators_for(p) ⇒ Object
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 |
Instance Method Details
#add_validation(shorthand, &validateFn) ⇒ Object
18 19 20 21 22 |
# File 'lib/structure_digest/digest.rb', line 18 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
69 70 71 72 |
# File 'lib/structure_digest/digest.rb', line 69 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
9 10 11 12 13 14 15 16 |
# File 'lib/structure_digest/digest.rb', line 9 def injest_yml_files(file_paths) file_paths.each do |p| y = YAML.load_file(p) gather_paths(y, paths) end @abstract_paths = paths.map(&:abstract).uniq self end |
#pretty_print(io, tree, level = 0) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/structure_digest/digest.rb', line 54 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 ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/structure_digest/digest.rb', line 39 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
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/structure_digest/digest.rb', line 24 def validate(hash) paths = [] 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
35 36 37 |
# File 'lib/structure_digest/digest.rb', line 35 def validators_for(p) validators[p.abstract.serialize] || [] end |