Module: I18n::Tasks::Command::Options::Data

Includes:
DSL
Defined in:
lib/i18n/tasks/command/options/data.rb

Constant Summary collapse

DATA_FORMATS =
%w[yaml json keys].freeze
OUT_FORMATS =
['terminal-table', *DATA_FORMATS, 'inspect', 'key-values'].freeze

Instance Method Summary collapse

Methods included from DSL

included, #t

Instance Method Details

#forest_pos_or_stdin!(opt, format = ) ⇒ I18n::Tasks::Data::Tree::Siblings



36
37
38
# File 'lib/i18n/tasks/command/options/data.rb', line 36

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::Tasks::Data::Tree::Siblings>

Returns trees read from stdin and positional arguments.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/i18n/tasks/command/options/data.rb', line 41

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



58
59
60
61
62
# File 'lib/i18n/tasks/command/options/data.rb', line 58

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::Tasks::Data::Tree::Siblings



65
66
67
68
69
70
71
72
73
# File 'lib/i18n/tasks/command/options/data.rb', line 65

def parse_forest(src, format)
  fail CommandError, I18n.t('i18n_tasks.cmd.errors.pass_forest') unless src

  if format == 'keys'
    ::I18n::Tasks::Data::Tree::Siblings.from_key_names parse_keys(src)
  else
    ::I18n::Tasks::Data::Tree::Siblings.from_nested_hash i18n.data.adapter_parse(src, format)
  end
end

#parse_keys(src) ⇒ Object



75
76
77
# File 'lib/i18n/tasks/command/options/data.rb', line 75

def parse_keys(src)
  Array(src).compact.flat_map { |v| v.strip.split(/\s*[,\s\n]\s*/) }.map(&:presence).compact
end


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/i18n/tasks/command/options/data.rb', line 79

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 'key-values'
    puts(forest.key_values(root: true).map { |kv| kv.join("\t") })
  when *DATA_FORMATS
    puts i18n.data.adapter_dump forest.to_hash(true), format
  end
end