Class: JSONSelect

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

Defined Under Namespace

Modules: Ast, DepthHelpers, KeyHelpers, PositionHelpers, Selector, SizeHelpers, TypeHelpers Classes: SelectorParser

Constant Summary collapse

ParseError =
Class.new(RuntimeError)
VERSION =
"0.1.4"
@@parser_cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, use_parser_cache = true) ⇒ JSONSelect

Returns a new instance of JSONSelect.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/json_select/selector.rb', line 11

def initialize(src, use_parser_cache=true)
  case src

  when String
    ast = nil

    if use_parser_cache
      ast = @@parser_cache[src]
    end

    if ast
      @ast = ast

    else
      parser = JSONSelect::SelectorParser.new
      tree   = parser.parse(src)
      unless tree
        raise JSONSelect::ParseError, parser.failure_reason
      end

      @ast = tree.to_ast

      if use_parser_cache
        @@parser_cache[src] = ast
      end
    end

  when Array
    @ast = src

  else
    raise ArgumentError, "Expected a string for ast"

  end

  @helpers = Module.new
  @helpers.send(:extend,
    JSONSelect::KeyHelpers,
    JSONSelect::TypeHelpers,
    JSONSelect::SizeHelpers,
    JSONSelect::DepthHelpers,
    JSONSelect::PositionHelpers)

  @helper_methods = {}
  (@helpers.public_methods - Module.public_methods).map do |name|
    @helper_methods[name.to_sym] = @helpers.method(name)
  end
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



3
4
5
# File 'lib/json_select/selector.rb', line 3

def ast
  @ast
end

Class Method Details

.reset_cache!Object



7
8
9
# File 'lib/json_select/selector.rb', line 7

def self.reset_cache!
  @@parser_cache.clear
end

Instance Method Details

#inspectObject



125
126
127
# File 'lib/json_select/selector.rb', line 125

def inspect
  "#<JSONSelect (#{to_s})>"
end

#match(object) ⇒ Object Also known as: =~

Returns the first matching child in ‘object`



61
62
63
64
65
66
67
# File 'lib/json_select/selector.rb', line 61

def match(object)
  _each(@ast, object, nil, nil, nil, 0) do |object|
    return object
  end

  return nil
end

#matches(object) ⇒ Object

Returns all matching children in ‘object`



72
73
74
75
76
77
78
79
80
# File 'lib/json_select/selector.rb', line 72

def matches(object)
  matches = []

  _each(@ast, object, nil, nil, nil, 0) do |object|
    matches << object
  end

  matches
end

#test(object) ⇒ Object Also known as: ===

Returns true if ‘object` has any matching children.



83
84
85
86
87
88
89
# File 'lib/json_select/selector.rb', line 83

def test(object)
  _each(@ast, object, nil, nil, nil, 0) do |object|
    return true
  end

  return false
end

#to_sObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/json_select/selector.rb', line 93

def to_s
  selectors = @ast
  selectors = (selectors[0] == ',' ? selectors[1..-1] : [selectors])

  selectors.map do |selector|
    selector.map do |part|
      case part

      when :>
        '>'

      when Hash
        tests = (part[:tests].empty? ? ['*'] : part[:tests])

        tests.map do |test|
          case test

          when '*'
            '*'

          when Hash
            @helpers.send("format_#{test[:f]}", test)

          end
        end.join('')

      end

    end.join(' ')
  end.join(', ')
end