Class: WcaI18n::YAMLToEnrichedRubyHash
- Inherits:
-
Psych::Visitors::ToRuby
- Object
- Psych::Visitors::ToRuby
- WcaI18n::YAMLToEnrichedRubyHash
- Defined in:
- lib/wca_i18n/yaml_to_enriched_ruby_hash.rb
Overview
Re-implement some parts of the ToRuby emitter to inject our TranslatedLeaf where needs be
Class Method Summary collapse
Instance Method Summary collapse
- #_build_original_hashes_by_line_from_text(text) ⇒ Object
-
#initialize(*args, text) ⇒ YAMLToEnrichedRubyHash
constructor
A new instance of YAMLToEnrichedRubyHash.
- #pluralization_map?(v) ⇒ Boolean
-
#revive_hash(hash, o) ⇒ Object
Copy from the revive_hash method in github.com/ruby/psych/blob/e9e4567adefc52e6511df7060851bce9fe408082/lib/psych/visitors/to_ruby.rb Except we override the generic case with our code.
Constructor Details
#initialize(*args, text) ⇒ YAMLToEnrichedRubyHash
Returns a new instance of YAMLToEnrichedRubyHash.
25 26 27 28 |
# File 'lib/wca_i18n/yaml_to_enriched_ruby_hash.rb', line 25 def initialize(*args, text) super(*args) _build_original_hashes_by_line_from_text(text) end |
Class Method Details
.create(original_hashes_map) ⇒ Object
11 12 13 14 15 |
# File 'lib/wca_i18n/yaml_to_enriched_ruby_hash.rb', line 11 def self.create(original_hashes_map) class_loader = Psych::ClassLoader.new scanner = Psych::ScalarScanner.new class_loader new(scanner, class_loader, original_hashes_map) end |
.parse(text) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/wca_i18n/yaml_to_enriched_ruby_hash.rb', line 17 def self.parse(text) tree = Psych.parser.parse(text) emitter = self.create(text) # Not sure why, but "accept" returns an array with one element. # Probably because the root of the YAML is a sequence by default? emitter.accept(tree.handler.root).first end |
Instance Method Details
#_build_original_hashes_by_line_from_text(text) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/wca_i18n/yaml_to_enriched_ruby_hash.rb', line 85 def _build_original_hashes_by_line_from_text(text) @original_hashes_by_line = {}.tap do |hash| # Build a hash mapping a line number to its comment text.each_line.with_index do |line, index| stripped_line = line.strip if stripped_line.start_with?(ORIGINAL_HASH_TAG) hash[index] = stripped_line[ORIGINAL_HASH_TAG.length..-1] end end end end |
#pluralization_map?(v) ⇒ Boolean
30 31 32 33 34 35 36 |
# File 'lib/wca_i18n/yaml_to_enriched_ruby_hash.rb', line 30 def pluralization_map?(v) return false unless v.is_a?(Psych::Nodes::Mapping) v.children.each_slice(2) do |k,v| return true if WcaI18n::PLURALIZATION_KEYS.include?(accept(k)) end return false end |
#revive_hash(hash, o) ⇒ Object
Copy from the revive_hash method in github.com/ruby/psych/blob/e9e4567adefc52e6511df7060851bce9fe408082/lib/psych/visitors/to_ruby.rb Except we override the generic case with our code.
40 41 42 43 44 45 46 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 |
# File 'lib/wca_i18n/yaml_to_enriched_ruby_hash.rb', line 40 def revive_hash hash, o o.children.each_slice(2) { |k,v| key = accept(k) val = accept(v) if key == SHOVEL && k.tag != "tag:yaml.org,2002:str" case v when Nodes::Alias, Nodes::Mapping begin hash.merge! val rescue TypeError hash[key] = val end when Nodes::Sequence begin h = {} val.reverse_each do |value| h.merge! value end hash.merge! h rescue TypeError hash[key] = val end else hash[key] = val end else # This is where we handle the translated key if v.is_a?(Psych::Nodes::Scalar) && !WcaI18n::PLURALIZATION_KEYS.include?(key) # For scalar value, the start line registered is the correct line # We assume that the '#original_hash: ' comment comes on the line before. original_hash = @original_hashes_by_line.delete(v.start_line - 1) val = WcaI18n::TranslatedLeaf.new(val, original_hash) end if pluralization_map?(v) # For mappings, the start line registered is the line of the first key/value! original_hash = @original_hashes_by_line.delete(v.start_line - 2) val = WcaI18n::TranslatedLeaf.new(val, original_hash) end hash[key] = val end } hash end |