class YAMLM::CLI

YAMLM::CLI

Public Class Methods

new(argv = []) click to toggle source

@param argv [Array]

# File lib/yamlm/cli.rb, line 7
def initialize(argv = [])
  @files = argv
end

Public Instance Methods

execute() click to toggle source

@return [void]

# File lib/yamlm/cli.rb, line 12
def execute
  puts dump_yaml(
    merge_hash_array(load_texts(merge_texts(read_files(@files))))
  )
end

Private Instance Methods

deep_merge_hash(origin_hash, other_hash) click to toggle source

@param origin_hash [Hash] @param other_hash [Hash] @return [Hash]

# File lib/yamlm/cli.rb, line 47
def deep_merge_hash(origin_hash, other_hash)
  origin_hash.merge!(other_hash) do |_key, this_val, other_val|
    if this_val.is_a?(Hash) && other_val.is_a?(Hash)
      deep_merge_hash(this_val.dup, other_val)
    else
      other_val
    end
  end
end
dump_yaml(hash) click to toggle source

@param hash [Hash] @return [String]

# File lib/yamlm/cli.rb, line 59
def dump_yaml(hash)
  YAML.dump(hash.empty? ? nil : hash)
end
load_texts(texts) click to toggle source

@param texts [Array] @return [Array]

# File lib/yamlm/cli.rb, line 34
def load_texts(texts)
  texts.map { |t| YAML.safe_load(t, [], [], true) }
end
merge_hash_array(array) click to toggle source

@param array [Array] @return [Hash]

# File lib/yamlm/cli.rb, line 40
def merge_hash_array(array)
  array.each_with_object({}) { |h, r| deep_merge_hash(r, h) }
end
merge_texts(texts) click to toggle source

@param texts [Array] @return [Array]

# File lib/yamlm/cli.rb, line 28
def merge_texts(texts)
  texts.each_with_object([]) { |t, r| r << r.last.to_s + t }
end
read_files(files) click to toggle source

@param files [Array] @return [Array]

# File lib/yamlm/cli.rb, line 22
def read_files(files)
  files.map { |f| File.read(f) }
end