Top Level Namespace
Instance Method Summary collapse
- #aargh(message, return_value = nil) ⇒ Object
- #check_rules(r, filename) ⇒ Object
- #dump_result(output, doc, error_return) ⇒ Object
- #expression_array_errors(expression_array) ⇒ Object
- #identifier?(item) ⇒ Boolean
- #list_schemas ⇒ Object
- #load_schema(schema_name) ⇒ Object
- #load_source(input) ⇒ Object
- #load_source_hash(input) ⇒ Object
- #load_verified(input, schema_name) ⇒ Object
- #make_schemer(schema, reading) ⇒ Object
- #match_item(item, rules) ⇒ Object
-
#merge_any(r) ⇒ Object
Values in any are used if there is no existing value.
-
#merge_rules(full, overwriting) ⇒ Object
sets: name: nodes/edges: # Merge here.
- #save_verified(output, doc, error_return, schema_name) ⇒ Object
- #split_expression_string(expression_string) ⇒ Object
Instance Method Details
#aargh(message, return_value = nil) ⇒ Object
11 12 13 14 15 |
# File 'lib/common.rb', line 11 def aargh(, return_value = nil) = (.map &(:to_s)).join("\n") if .is_a? Array $stderr.puts return_value end |
#check_rules(r, filename) ⇒ Object
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/subsets.rb', line 47 def check_rules(r, filename) ok = true c = {} r.fetch('sets', []).each do |setrules| name = setrules.delete('name') if c.key?(name) aargh("#{filename} duplicate set name: #{name}") ok = false next end cats = {} %i[any nodes edges].each do |category| fr = {} setrules.fetch(category.to_s, []).each do |fieldrules| frn = fieldrules['name'] res = fr.fetch(frn, []) res.concat(fieldrules['rules'].map { |str| Regexp.new(str) }) fr[frn] = res rescue StandardError => e aargh("#{filename} #{name} #{category} #{frn} rule error:\n#{e}") ok = false end cats[category] = fr end c[name] = cats end r['sets'] = c c = {} r.fetch('expressions', []).each do |ne| name = ne.delete('name') if c.key?(name) aargh("#{filename} duplicate expression name: #{name}") ok = false next end ea = split_expression_string(ne['expression']) errs = expression_array_errors(ea) unless errs.nil? aargh("#{filename} expressions #{name}:\n#{errs.join("\n")}") ok = false end c[name] = ea end r['expressions'] = c ok end |
#dump_result(output, doc, error_return) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/common.rb', line 65 def dump_result(output, doc, error_return) if output.nil? $stdout.puts doc else fp = Pathname.new output fp.open('w') do |f| f.puts doc end end 0 rescue StandardError => e aargh([ e, "Failed to write output: #{output || 'stdout'}" ], error_return) end |
#expression_array_errors(expression_array) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/subsets.rb', line 30 def expression_array_errors(expression_array) errors = [] previous_item_type = :symbol unless expression_array.empty? errors << 'Expression must start with an identifier' unless identifier?(expression_array.first) errors << 'Expression must end with an identifier' unless identifier?(expression_array.last) end expression_array.each_with_index do |item, index| current_item_type = identifier?(item) ? :identifier : :symbol if current_item_type == previous_item_type errors << "Invalid item '#{item}' at index #{index}. Expected #{current_item_type == :identifier ? 'operator' : 'identifier'}." end previous_item_type = current_item_type end errors.empty? ? nil : errors end |
#identifier?(item) ⇒ Boolean
26 27 28 |
# File 'lib/subsets.rb', line 26 def identifier?(item) item.is_a?(String) end |
#list_schemas ⇒ Object
33 34 35 |
# File 'lib/common.rb', line 33 def list_schemas (Dir.entries(File.dirname(__FILE__)).select { |name| name.upcase.end_with?('.YAML') }).map { |name| name[0, name.size - 5] } end |
#load_schema(schema_name) ⇒ Object
37 38 39 |
# File 'lib/common.rb', line 37 def load_schema(schema_name) YAML.safe_load_file(File.join(File.dirname(__FILE__), "#{schema_name}.yaml")) end |
#load_source(input) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/common.rb', line 17 def load_source(input) YAML.safe_load(input.nil? ? $stdin : File.read(input)) rescue Errno::ENOENT aargh "Could not load #{input || 'stdin'}" rescue StandardError => e aargh "#{e}\nFailed to read #{input || 'stdin'}" end |
#load_source_hash(input) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/common.rb', line 25 def load_source_hash(input) src = load_source(input) unless src.nil? return aargh("#{input} is not a simple mapping.") unless src.is_a? Hash end src end |
#load_verified(input, schema_name) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/common.rb', line 47 def load_verified(input, schema_name) src = load_source(input) unless src.nil? s = load_schema(schema_name) schemer = make_schemer(s, true) errs = schemer.validate(src).to_a unless errs.empty? aargh (errs.map { |e| e['error'] }).join("\n") src = nil end end src rescue Errno::ENOENT aargh "Could not load #{schema_name}" rescue StandardError => e aargh "Internal error: #{e}\n#{e.backtrace.join("\n")}\nFailed to read #{schema_name}" end |
#make_schemer(schema, reading) ⇒ Object
41 42 43 44 45 |
# File 'lib/common.rb', line 41 def make_schemer(schema, reading) JSONSchemer.schema(JSON.generate(schema), meta_schema: 'https://json-schema.org/draft/2020-12/schema', insert_property_defaults: reading) end |
#match_item(item, rules) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/subsets.rb', line 134 def match_item(item, rules) rules.each do |field, patterns| vals = item.fetch(field, nil) next if vals.nil? vals = [ vals ] unless vals.is_a?(Array) patterns.each do |p| vals.each do |v| next unless v.is_a?(String) return true if p.match?(v) end end end false end |
#merge_any(r) ⇒ Object
Values in any are used if there is no existing value.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/subsets.rb', line 95 def merge_any(r) r.fetch('sets', {}).each_value do |rules| any = rules.delete(:any) next if any.nil? %i[edges nodes].each do |category| c = rules.fetch(category, {}) any.each do |field, patterns| next if c.key?(field) c[field] = patterns end rules[category] = c end end end |
#merge_rules(full, overwriting) ⇒ Object
sets:
name:
nodes/edges: # Merge here.
name: []
expressions: # Merge here.
name: string
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/subsets.rb', line 117 def merge_rules(full, overwriting) full['expressions'] = {} unless full.key? 'expressions' full['expressions'].merge!(overwriting.fetch('expressions', {})) sets = full.fetch('sets', {}) ow = overwriting.fetch('sets', {}) sets.each do |name, rules| %i[nodes edges].each do |category| m = ow.dig(name, category) next if m.nil? rules[category] = {} unless rules.key? category rules[category].merge!(m) end end sets.merge!(ow) { |_key, merged, _used| merged } # Adds new sets. full['sets'] = sets end |
#save_verified(output, doc, error_return, schema_name) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/common.rb', line 79 def save_verified(output, doc, error_return, schema_name) s = load_schema(schema_name) schemer = make_schemer(s, false) errs = schemer.validate(doc).to_a unless errs.empty? aargh (errs.map { |e| e['error'] }).join("\n") return error_return end dump_result(output, YAML.dump(doc, line_width: 1_000_000), error_return) end |
#split_expression_string(expression_string) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/subsets.rb', line 10 def split_expression_string(expression_string) out = [] remaining = expression_string.lstrip until remaining.empty? item, sep, rest = remaining.partition(/[\s+-]/) out << item unless item.empty? sep.strip! unless sep.empty? out << :plus if sep == '+' out << :minus if sep == '-' end remaining = rest.lstrip end out end |