Class: Razor::CLI::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/razor/cli/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(parse, navigate, collections, segments) ⇒ Query

Returns a new instance of Query.



2
3
4
5
6
7
8
9
# File 'lib/razor/cli/query.rb', line 2

def initialize(parse, navigate, collections, segments)
  @parse = parse
  @navigate = navigate
  @collections = collections
  @segments = segments
  @stripped_segments = []
  @options = {}
end

Instance Method Details

#get_optparse(doc, nav) ⇒ Object



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
# File 'lib/razor/cli/query.rb', line 15

def get_optparse(doc, nav)
  # If the last document is an Array, we need to find
  # which element matches the given query. Once found,
  # return the 'params' section, if it has one.
  if doc.is_a?(Array)
    query = doc.find {|coll| coll['name'] == nav}
    params = (query && query['params']) || {}
  elsif doc.is_a?(Hash)
    params = (doc[nav].is_a?(Hash) && doc[nav].has_key?('params') &&
        doc[nav]['params'].is_a?(Hash) && doc[nav]['params']) || {}
  end
  @queryoptparse = OptionParser.new do |opts|
    opts.on "-f", "--full", "Show full details when viewing entities" do
      @parse.format = 'full'
    end

    opts.on "-s", "--short", "Show shortened details when viewing entities" do
      @parse.format = 'short'
    end

    params.each do |param, args|
      if args['type'] == 'boolean'
        opts.on "--#{param}" do
          @options[param] = true
        end
      else
        opts.on "--#{param} VALUE" do |value|
          @options[param] = value
        end
      end
    end
  end
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/razor/cli/query.rb', line 49

def run
  @doc = @collections
  while @segments.any?
    nav = @segments.shift
    @parse.stripped_args << nav
    @segments = get_optparse(@doc, nav).order(@segments)
    @doc = @navigate.move_to nav, @doc, @options
  end

  # Get the next level if it's a list of objects.
  if @doc.is_a?(Hash) and @doc['items'].is_a?(Array)
    # Cache doc_resource since these queries are just for extra detail.
    temp_doc_resource = @navigate.doc_resource
    @doc['items'] = @doc['items'].map do |item|
      item.is_a?(Hash) && item.has_key?('id') ? @navigate.json_get(URI.parse(item['id'])) : item
    end
    @navigate.doc_resource = temp_doc_resource
  end
  @doc
end

#stripped_segmentsObject



11
12
13
# File 'lib/razor/cli/query.rb', line 11

def stripped_segments
  @stripped_segments.join
end