Module: I18n::Processes::Command::Options::Data
- Includes:
- DSL
- Defined in:
- lib/i18n/processes/command/options/data.rb
Constant Summary collapse
- DATA_FORMATS =
%w[yaml json keys].freeze
- OUT_FORMATS =
['terminal-table', *DATA_FORMATS, 'inspect'].freeze
Instance Method Summary collapse
- #forest_pos_or_stdin!(opt, format = ) ⇒ I18n::Processes::Data::Tree::Siblings
-
#forests_stdin_and_pos!(opt, num = false, format = ) ⇒ Array<I18n::Processes::Data::Tree::Siblings>
Trees read from stdin and positional arguments.
- #merge_forests_stdin_and_pos!(opt) ⇒ Object
- #parse_forest(src, format) ⇒ I18n::Processes::Data::Tree::Siblings
- #parse_keys(src) ⇒ Object
- #print_changed_keys(diff, version = :changed_keys) ⇒ Object
- #print_forest(forest, opts, version = :show_tree) ⇒ Object
Methods included from DSL
Instance Method Details
#forest_pos_or_stdin!(opt, format = ) ⇒ I18n::Processes::Data::Tree::Siblings
33 34 35 |
# File 'lib/i18n/processes/command/options/data.rb', line 33 def forest_pos_or_stdin!(opt, format = opt[:format]) parse_forest(pos_or_stdin!(opt), format) end |
#forests_stdin_and_pos!(opt, num = false, format = ) ⇒ Array<I18n::Processes::Data::Tree::Siblings>
Returns trees read from stdin and positional arguments.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/i18n/processes/command/options/data.rb', line 38 def forests_stdin_and_pos!(opt, num = false, format = opt[:format]) args = opt[:arguments] || [] if opt[:nostdin] sources = [] else sources = [$stdin.read] num -= 1 if num end if num num.times { sources << args.shift } else sources += args args.clear end sources.map { |src| parse_forest(src, format) } end |
#merge_forests_stdin_and_pos!(opt) ⇒ Object
55 56 57 58 59 |
# File 'lib/i18n/processes/command/options/data.rb', line 55 def merge_forests_stdin_and_pos!(opt) forests_stdin_and_pos!(opt).inject(i18n.empty_forest) do |result, forest| result.merge! forest end end |
#parse_forest(src, format) ⇒ I18n::Processes::Data::Tree::Siblings
62 63 64 65 66 67 68 69 |
# File 'lib/i18n/processes/command/options/data.rb', line 62 def parse_forest(src, format) fail CommandError, "pass locale forest" unless src if format == 'keys' ::I18n::Processes::Data::Tree::Siblings.from_key_names parse_keys(src) else ::I18n::Processes::Data::Tree::Siblings.from_nested_hash i18n.data.adapter_parse(src, format) end end |
#parse_keys(src) ⇒ Object
71 72 73 |
# File 'lib/i18n/processes/command/options/data.rb', line 71 def parse_keys(src) Array(src).compact.flat_map { |v| v.strip.split(/\s*[,\s\n]\s*/) }.map(&:presence).compact end |
#print_changed_keys(diff, version = :changed_keys) ⇒ Object
89 90 91 |
# File 'lib/i18n/processes/command/options/data.rb', line 89 def print_changed_keys(diff, version = :changed_keys) terminal_report.send(version, diff) end |
#print_forest(forest, opts, version = :show_tree) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/i18n/processes/command/options/data.rb', line 75 def print_forest(forest, opts, version = :show_tree) format = opts[:format].to_s case format when 'terminal-table' terminal_report.send(version, forest) when 'inspect' puts forest.inspect when 'keys' puts forest.key_names(root: true) when *DATA_FORMATS puts i18n.data.adapter_dump forest.to_hash(true), format end end |