module Yqr

Constants

VERSION

Public Class Methods

debug() click to toggle source
# File lib/yqr.rb, line 176
def debug
  @options[:debug]
end
enable_symborize() click to toggle source
# File lib/yqr.rb, line 172
def enable_symborize
  @options[:symborize]
end
exec() click to toggle source
# File lib/yqr.rb, line 111
def exec
  if debug
    puts "==== Query Info ===="
    puts %Q{self.yaml#{parse_query}}
    puts "==== Yaml Info ====="
    puts @yaml
    puts "===================="
  end

  case parse_query
  when /\A(\w|\d)/
    raise "<QUERY> format is invalid."
  end

  eval %Q{self.yaml#{parse_query}}
rescue => ex
  STDERR.puts "Error was happen."
  STDERR.print ex.message
end
exec_with_format() click to toggle source
# File lib/yqr.rb, line 131
def exec_with_format
  load_yaml
  obj = exec
  if debug
    puts "==== Object Info ===="
    puts obj.class
    puts obj
    puts "====================="
  end

  if @options[:raw]
    return raw_output_formatter(obj)
  end

  obj = result_decode obj

  case obj.class.to_s
  when "Array", "Hash"
    if @options[:json]
      require 'json'
      obj.to_json
    else
      obj.to_yaml
    end
  else
    obj
  end
end
load_body() click to toggle source
# File lib/yqr.rb, line 32
def load_body
  @yaml = if enable_symborize && Module.constants.include?(:Hashie)
    load_body_use_hashie @options[:body]
  else
    YAML.load @options[:body]
  end
end
load_body_use_hashie(body) click to toggle source
# File lib/yqr.rb, line 48
def load_body_use_hashie(body)
  tmp_file_path = "/tmp/yqr_tmp.#{Time.now.to_i}"
  File.write tmp_file_path, body
  load_yaml_use_hashie tmp_file_path, true
end
load_file() click to toggle source
# File lib/yqr.rb, line 40
def load_file
  @yaml = if enable_symborize && Module.constants.include?(:Hashie)
    load_yaml_use_hashie @options[:file]
  else
    YAML.load_file @options[:file]
  end
end
load_yaml() click to toggle source
# File lib/yqr.rb, line 23
def load_yaml
  if @options[:file]
    load_file
  else
    load_body
  end
  symbolize_all_keys @yaml
end
load_yaml_use_hashie(path, delete=false) click to toggle source
# File lib/yqr.rb, line 54
def load_yaml_use_hashie(path, delete=false)
  begin
    Hashie::Mash.load path
  rescue => ex
    if debug
      puts ex.message
    end
    YAML.load_file path
  ensure
    if delete && File.exist?(path)
      File.delete path
    end
  end
end
options() click to toggle source
# File lib/yqr.rb, line 19
def options
  @options
end
parse_query() click to toggle source
# File lib/yqr.rb, line 164
def parse_query
  if enable_symborize
    @options[:query].gsub(/\[(\w*)\]/, '[:\1]').gsub(/\[:(\d*)\]/, '[\1]')
  else
    @options[:query]
  end
end
raw_output_formatter(obj) click to toggle source
# File lib/yqr.rb, line 160
def raw_output_formatter(obj)
  obj.inspect
end
result_decode(obj) click to toggle source
# File lib/yqr.rb, line 91
def result_decode(obj)
  return obj unless enable_symborize
  case obj.class.to_s
  when 'Hash'
    obj.keys.each do |key|
      obj[(key.to_sym rescue key) || key] = result_decode(obj.delete(key))
    end
    obj
  when 'Array'
    obj.map! do |elem|
      result_decode elem
    end
    obj
  when 'Hashie::Mash'
    result_decode obj.to_h
  else
    obj
  end
end
symbolize_all_keys(obj) click to toggle source
# File lib/yqr.rb, line 69
def symbolize_all_keys(obj)
  return obj unless enable_symborize
  case obj.class.to_s
  when 'Hash'
    if Module.constants.include?(:Hashie)
      obj = Hashie::Mash.new(obj)
    else
      obj.keys.each do |key|
        obj[(key.to_sym rescue key) || key] = symbolize_all_keys(obj.delete(key))
      end
    end
    obj
  when 'Array'
    obj.map! do |elem|
      symbolize_all_keys elem
    end
    obj
  else
    obj
  end
end
yaml() click to toggle source
# File lib/yqr.rb, line 15
def yaml
  @yaml
end