Module: CollinsShell::Console::CommandHelpers

Includes:
Cache
Included in:
Asset, Filesystem
Defined in:
lib/collins_shell/console/command_helpers.rb

Instance Method Summary collapse

Methods included from Cache

#cache_get_or_else, #clear_cache

Instance Method Details

#asset_context?(stack) ⇒ Boolean



127
128
129
# File 'lib/collins_shell/console/command_helpers.rb', line 127

def asset_context? stack
  !tag_from_stack(stack).nil?
end

#asset_exists?(tag) ⇒ Boolean



8
9
10
11
12
13
# File 'lib/collins_shell/console/command_helpers.rb', line 8

def asset_exists? tag
  m = "asset_exists?(#{tag})"
  cache_get_or_else(m) {
    call_collins(m) {|c| c.exists?(tag)}
  }
end

#call_collins(operation, &block) ⇒ Object



24
25
26
# File 'lib/collins_shell/console/command_helpers.rb', line 24

def call_collins operation, &block
  shell_handle.call_collins(collins_client, operation, &block)
end

#cli_optionsHash



33
34
35
# File 'lib/collins_shell/console/command_helpers.rb', line 33

def cli_options
  CollinsShell::Console.options
end

#collins_clientCollins::Client



28
29
30
# File 'lib/collins_shell/console/command_helpers.rb', line 28

def collins_client
  shell_handle.get_collins_client cli_options
end

#find_assets(tags_values, details) ⇒ Array<Collins::Asset>, Array<String>

Given an array of keys and values, return matching assets



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/collins_shell/console/command_helpers.rb', line 46

def find_assets tags_values, details
  q = tags_values.each_slice(2).inject({}) {|h,o| h.update(o[0].to_s.to_sym => o[1])}
  q.update(:details => details)
  selector = shell_handle.get_selector(q, nil, 5000)
  call_collins("find(#{selector})") do |c|
    if details then
      c.find(selector)
    else
      c.find(selector).map{|a| a.tag}.sort
    end
  end
end

#find_one_asset(tags_values, details = true) ⇒ Array<Collins::Asset>, NilClass



38
39
40
# File 'lib/collins_shell/console/command_helpers.rb', line 38

def find_one_asset tags_values, details = true
  find_assets(tags_values, details).first
end

#get_all_tags(include_virtual = true) ⇒ Object

return known tags including virtual ones (ones not stored, but that can be used as query parameters



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/collins_shell/console/command_helpers.rb', line 83

def get_all_tags include_virtual = true
  begin
    cache_get_or_else("get_all_tags(#{include_virtual.to_s})") {
      tags = call_collins("get_all_tags"){|c| c.get_all_tags}.map{|t|t.name}
      if include_virtual then
        [tags + Collins::Asset::Find.to_a].flatten.sort
      else
        tags.sort
      end
    }
  rescue Exception => e
    puts "Error retrieving tags: #{e}"
    []
  end
end

#get_asset(tag) ⇒ Collins::Asset



16
17
18
19
20
21
# File 'lib/collins_shell/console/command_helpers.rb', line 16

def get_asset tag
  m = "get_asset(#{tag})"
  cache_get_or_else(m) {
    call_collins(m) {|c| c.get(tag)}
  }
end

#get_tag_values(tag, resolve_asset = true) ⇒ Array<String>, Array<Collins::Asset>



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/collins_shell/console/command_helpers.rb', line 60

def get_tag_values tag, resolve_asset = true
  m = "get_tag_values(#{tag}, #{resolve_asset.to_s})"
  cache_get_or_else(m) {
    call_collins(m) {|c| c.get_tag_values(tag)}.sort
  }
rescue Exception => e
  if e.is_a?(Collins::RequestError) then
    if e.code.to_i == 404 then
      [get_asset(tag)] if resolve_asset
    else
      raise e
    end
  else
    [get_asset(tag)] if resolve_asset
  end
end

#resolve_asset_tag(tag, stack) ⇒ String, NilClass

Given a possible string tag and the context stack, find the asset tag



103
104
105
106
107
# File 'lib/collins_shell/console/command_helpers.rb', line 103

def resolve_asset_tag tag, stack
  Collins::Option(tag).map{|s| s.strip}.filter_not{|s| s.empty?}.get_or_else {
    tag_from_stack stack
  }
end

#shell_handleCollinsShell::Util



110
111
112
# File 'lib/collins_shell/console/command_helpers.rb', line 110

def shell_handle
  CollinsShell::Asset.new([], cli_options)
end

#tag_from_stack(stack) ⇒ NilClass, String



116
117
118
119
120
121
122
123
124
# File 'lib/collins_shell/console/command_helpers.rb', line 116

def tag_from_stack stack
  node = stack.last
  node = node.eval('self') unless node.nil?
  if node and node.asset? then
    node.console_asset.tag
  else
    nil
  end
end

#virtual_tagsObject



77
78
79
# File 'lib/collins_shell/console/command_helpers.rb', line 77

def virtual_tags
  Collins::Asset::Find.to_a
end