Module: MarkdownComposer::DataPath
- Defined in:
- lib/markdown_composer/data_path.rb
Class Method Summary collapse
- .apply_filter(current, filter, diagnostics:, diagnostic_path:) ⇒ Object
- .apply_segment(current, segment, diagnostics:, diagnostic_path:) ⇒ Object
- .cast(value) ⇒ Object
- .collect_recursive_values(current, key, values) ⇒ Object
- .compare(actual, operator, expected) ⇒ Object
- .evaluate(data, expression, diagnostics:, diagnostic_path:) ⇒ Object
- .fetch_key(hash, key, diagnostics:, diagnostic_path:) ⇒ Object
- .filter_atom_match?(item, atom) ⇒ Boolean
- .filter_match?(item, filter) ⇒ Boolean
- .node_for(data_block, path, value, index) ⇒ Object
- .normalize_key(key) ⇒ Object
- .numeric(value) ⇒ Object
- .parse_auto(source) ⇒ Object
- .parse_data(data_block, diagnostics:, path:) ⇒ Object
- .parse_segment(segment) ⇒ Object
- .project(current, key, diagnostics:, diagnostic_path:) ⇒ Object
- .project_keys(item, keys, diagnostics:, diagnostic_path:) ⇒ Object
- .quoted?(value) ⇒ Boolean
- .recursive_project(current, key) ⇒ Object
- .render_value(value) ⇒ Object
- .resolve(data_block, path, diagnostics:, diagnostic_path:) ⇒ Object
- .split_key_list(key) ⇒ Object
- .split_path(expression) ⇒ Object
- .split_top_level(text, delimiter) ⇒ Object
- .take_filter?(filter) ⇒ Boolean
- .unquote(value) ⇒ Object
- .value_type(value) ⇒ Object
Class Method Details
.apply_filter(current, filter, diagnostics:, diagnostic_path:) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/markdown_composer/data_path.rb', line 122 def apply_filter(current, filter, diagnostics:, diagnostic_path:) if take_filter?(filter) errors = Take.validate(Take.parse(filter)) errors.each { || diagnostics.error("data_path.take_invalid", , path: diagnostic_path) } return [] if errors.any? return Take.apply(Array(current), Take.parse(filter), diagnostics: diagnostics, path: diagnostic_path) end Array(current).select { |item| filter_match?(item, filter) } end |
.apply_segment(current, segment, diagnostics:, diagnostic_path:) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/markdown_composer/data_path.rb', line 66 def apply_segment(current, segment, diagnostics:, diagnostic_path:) key, filter = parse_segment(segment) current = project(current, key, diagnostics: diagnostics, diagnostic_path: diagnostic_path) unless key.empty? if filter current = quoted?(filter) ? project(current, unquote(filter), diagnostics: diagnostics, diagnostic_path: diagnostic_path) : apply_filter(current, filter, diagnostics: diagnostics, diagnostic_path: diagnostic_path) end current end |
.cast(value) ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/markdown_composer/data_path.rb', line 254 def cast(value) return value if value.is_a?(Numeric) || value == true || value == false return value.to_i if value.to_s.match?(/\A-?\d+\z/) return value.to_f if value.to_s.match?(/\A-?\d+\.\d+\z/) value.to_s end |
.collect_recursive_values(current, key, values) ⇒ Object
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/markdown_composer/data_path.rb', line 243 def collect_recursive_values(current, key, values) case current when Hash values << current[key] if current.key?(key) values << current[key.to_sym] if current.key?(key.to_sym) current.each_value { |value| collect_recursive_values(value, key, values) } when Array current.each { |value| collect_recursive_values(value, key, values) } end end |
.compare(actual, operator, expected) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/markdown_composer/data_path.rb', line 163 def compare(actual, operator, expected) case operator when "=" then actual.to_s == expected.to_s when "!=" then actual.to_s != expected.to_s when ">" then numeric(actual) > numeric(expected) when ">=" then numeric(actual) >= numeric(expected) when "<" then numeric(actual) < numeric(expected) when "<=" then numeric(actual) <= numeric(expected) when "~=" then actual.to_s.include?(expected.to_s) else false end end |
.evaluate(data, expression, diagnostics:, diagnostic_path:) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/markdown_composer/data_path.rb', line 44 def evaluate(data, expression, diagnostics:, diagnostic_path:) current = data segments = split_path(expression) index = 0 while index < segments.length segment = segments[index] if segment == "**" && segments[index + 1] current = recursive_project(current, segments[index + 1]) index += 2 next end current = apply_segment(current, segment, diagnostics: diagnostics, diagnostic_path: diagnostic_path) index += 1 end current end |
.fetch_key(hash, key, diagnostics:, diagnostic_path:) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/markdown_composer/data_path.rb', line 114 def fetch_key(hash, key, diagnostics:, diagnostic_path:) return hash[key] if hash.key?(key) return hash[key.to_sym] if hash.key?(key.to_sym) diagnostics.warn("data_path.missing_key", "Missing data key #{key.inspect}", path: diagnostic_path) nil end |
.filter_atom_match?(item, atom) ⇒ Boolean
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/markdown_composer/data_path.rb', line 147 def filter_atom_match?(item, atom) negated = atom.start_with?("!") && !atom.start_with?("!=") atom = atom[1..-1].to_s.strip if negated matched = if atom =~ /\A([^!<>=~]+)(!?=|>=|<=|>|<|~=)(.+)\z/ key = Regexp.last_match(1).strip operator = Regexp.last_match(2) expected = cast(unquote(Regexp.last_match(3).strip)) actual = cast(item[key] || item[key.to_sym]) compare(actual, operator, expected) else !!(item[atom] || item[atom.to_sym]) end negated ? !matched : matched end |
.filter_match?(item, filter) ⇒ Boolean
139 140 141 142 143 144 145 |
# File 'lib/markdown_composer/data_path.rb', line 139 def filter_match?(item, filter) return false unless item.is_a?(Hash) split_top_level(filter, "|").any? do |or_group| split_top_level(or_group, ";").all? { |atom| filter_atom_match?(item, atom.strip) } end end |
.node_for(data_block, path, value, index) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/markdown_composer/data_path.rb', line 266 def node_for(data_block, path, value, index) type = value.is_a?(Hash) ? "data_record" : "data_value" value_text = render_value(value) ComposerNode.new( id: "#{data_block.id}:data:#{index + 1}", source_key: data_block.source_key, type: type, source_position: data_block.source_position * 1000 + index + 1, level: nil, text: value_text.strip, attributes: data_block.attributes.merge( "path" => path.to_s, "source_type" => "data_block", "value" => value, "value_type" => value_type(value) ), children: [], raw: value_text.end_with?("\n") ? value_text : "#{value_text}\n", start_line: data_block.start_line, end_line: data_block.end_line ) end |
.normalize_key(key) ⇒ Object
184 185 186 |
# File 'lib/markdown_composer/data_path.rb', line 184 def normalize_key(key) unquote(key.to_s).gsub("\\.", ".").gsub("\\,", ",") end |
.numeric(value) ⇒ Object
262 263 264 |
# File 'lib/markdown_composer/data_path.rb', line 262 def numeric(value) value.is_a?(Numeric) ? value : value.to_s.to_f end |
.parse_auto(source) ⇒ Object
38 39 40 41 42 |
# File 'lib/markdown_composer/data_path.rb', line 38 def parse_auto(source) JSON.parse(source) rescue JSON::ParserError YAML.safe_load(source, permitted_classes: [ Symbol ], aliases: false) end |
.parse_data(data_block, diagnostics:, path:) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/markdown_composer/data_path.rb', line 22 def parse_data(data_block, diagnostics:, path:) format = data_block.attributes.fetch("format", "auto").to_s source = data_block.text.to_s case format when "json" JSON.parse(source) when "yaml", "yml" YAML.safe_load(source, permitted_classes: [ Symbol ], aliases: false) else parse_auto(source) end rescue JSON::ParserError, Psych::Exception => e diagnostics.error("data_path.invalid_data", "Invalid #{format} data: #{e.}", path: path) nil end |
.parse_segment(segment) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/markdown_composer/data_path.rb', line 75 def parse_segment(segment) if segment =~ /\A([^\[]*)\[(.+)\]\z/ [ normalize_key(Regexp.last_match(1).strip), Regexp.last_match(2).strip ] else [ normalize_key(segment.strip), nil ] end end |
.project(current, key, diagnostics:, diagnostic_path:) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/markdown_composer/data_path.rb', line 83 def project(current, key, diagnostics:, diagnostic_path:) keys = split_key_list(key) if keys.length > 1 return Array(current).map { |item| project_keys(item, keys, diagnostics: diagnostics, diagnostic_path: diagnostic_path) }.compact if current.is_a?(Array) return project_keys(current, keys, diagnostics: diagnostics, diagnostic_path: diagnostic_path) end key = keys.first case current when Hash fetch_key(current, key, diagnostics: diagnostics, diagnostic_path: diagnostic_path) when Array current.map { |item| project(item, key, diagnostics: diagnostics, diagnostic_path: diagnostic_path) }.flatten.compact else diagnostics.warn("data_path.type_mismatch", "Cannot read #{key.inspect} from #{current.class}", path: diagnostic_path) nil end end |
.project_keys(item, keys, diagnostics:, diagnostic_path:) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/markdown_composer/data_path.rb', line 102 def project_keys(item, keys, diagnostics:, diagnostic_path:) unless item.is_a?(Hash) diagnostics.warn("data_path.type_mismatch", "Cannot project keys from #{item.class}", path: diagnostic_path) return nil end keys.each_with_object({}) do |key, hash| value = fetch_key(item, key, diagnostics: diagnostics, diagnostic_path: diagnostic_path) hash[key] = value unless value.nil? end end |
.quoted?(value) ⇒ Boolean
180 181 182 |
# File 'lib/markdown_composer/data_path.rb', line 180 def quoted?(value) value.to_s.match?(/\A(["']).*\1\z/) end |
.recursive_project(current, key) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/markdown_composer/data_path.rb', line 236 def recursive_project(current, key) key = normalize_key(key) values = [] collect_recursive_values(current, key, values) values end |
.render_value(value) ⇒ Object
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/markdown_composer/data_path.rb', line 289 def render_value(value) case value when Hash YAML.dump(value).sub(/\A---\s*\n/, "") when Array value.map { |item| render_value(item).strip }.join("\n") else value.to_s end end |
.resolve(data_block, path, diagnostics:, diagnostic_path:) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/markdown_composer/data_path.rb', line 7 def resolve(data_block, path, diagnostics:, diagnostic_path:) unless data_block.type == "data_block" diagnostics.error("data_path.scope_invalid", "data_path is only valid inside data_block include scope", path: diagnostic_path) return [] end data = parse_data(data_block, diagnostics: diagnostics, path: diagnostic_path) return [] if data.nil? evaluated = evaluate(data, path.to_s, diagnostics: diagnostics, diagnostic_path: diagnostic_path) values = evaluated.is_a?(Array) ? evaluated : [ evaluated ].compact diagnostics.warn("data_path.empty", "Data path matched no values", path: diagnostic_path) if values.empty? values.each_with_index.map { |value, index| node_for(data_block, path, value, index) } end |
.split_key_list(key) ⇒ Object
188 189 190 |
# File 'lib/markdown_composer/data_path.rb', line 188 def split_key_list(key) split_top_level(key.to_s, ",").map { |part| normalize_key(part.strip) }.reject(&:empty?) end |
.split_path(expression) ⇒ Object
62 63 64 |
# File 'lib/markdown_composer/data_path.rb', line 62 def split_path(expression) split_top_level(expression.to_s, ".").map(&:strip).reject(&:empty?) end |
.split_top_level(text, delimiter) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/markdown_composer/data_path.rb', line 192 def split_top_level(text, delimiter) parts = [] current = +"" quote = nil bracket_depth = 0 escaped = false text.to_s.each_char do |char| if escaped current << char escaped = false next end if char == "\\" escaped = true next end if quote quote = nil if char == quote current << char next end if char == '"' || char == "'" quote = char current << char next end bracket_depth += 1 if char == "[" bracket_depth -= 1 if char == "]" && bracket_depth.positive? if char == delimiter && bracket_depth.zero? parts << current current = +"" else current << char end end parts << current parts end |
.take_filter?(filter) ⇒ Boolean
134 135 136 137 |
# File 'lib/markdown_composer/data_path.rb', line 134 def take_filter?(filter) filter.match?(/\A(?:all|odd|even)\z/) || filter.match?(/\A(?:first|last|position|range|ranges|skip|skip_last|every|except|top_percent|bottom_percent|middle|middle_percent|alternate|random):/) end |
.unquote(value) ⇒ Object
176 177 178 |
# File 'lib/markdown_composer/data_path.rb', line 176 def unquote(value) value.sub(/\A["']/, "").sub(/["']\z/, "") end |
.value_type(value) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/markdown_composer/data_path.rb', line 300 def value_type(value) case value when Hash then "object" when Array then "array" when String then "string" when Integer then "integer" when Float then "float" when TrueClass, FalseClass then "boolean" when NilClass then "null" else value.class.name end end |