Class: YamlQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/yamlquery.rb,
lib/yamlquery/version.rb

Defined Under Namespace

Classes: Output, Parser, Settings, Transform

Constant Summary collapse

VERSION =
'0.0.2'

Instance Method Summary collapse

Constructor Details

#initialize(object, options) ⇒ YamlQuery

Returns a new instance of YamlQuery.



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/yamlquery.rb', line 2

def initialize(object, options)
  @object   = object
  @options = options
  tree = parse(@options)
  yqt = YamlQuery::Transform.new
  parsed_query = yqt.apply(tree)
  if options[:debug]
    pp parsed_query
  end
  @keys     = parsed_query[:keys]
  @operator = parsed_query[:operator].to_s
  @arg      = parsed_query[:arg].to_s
end

Instance Method Details

#object_search(object = @object, index = 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yamlquery.rb', line 16

def object_search(object = @object, index = 0)
  result_hash = {}
  object.each do |k, v|
    if @keys[index] == k or @keys[index] == '*'
      if @keys.length == index + 1
        result_hash[k] = v if compare(v)
      elsif v.is_a?(Hash)
        result_hash[k] = object_search(v, index + 1)
        result_hash.delete(k) if result_hash[k].empty?
      end
      # A key shouldn't show up more than once, so we can return
      # as soon as we find a match if we aren't matching a wildcard.
      return result_hash unless @keys[index] == '*'
    end
  end
  return result_hash
end