Module: CollinsShell::Console::CommandHelpers
- Includes:
- Cache
- Included in:
- Asset, Filesystem
- Defined in:
- lib/collins_shell/console/command_helpers.rb
Instance Method Summary collapse
-
#asset_context?(stack) ⇒ Boolean
True if in asset context.
-
#asset_exists?(tag) ⇒ Boolean
True if asset associated with specified tag exists.
-
#call_collins(operation, &block) ⇒ Object
Result of calling collins using the specifed block.
-
#cli_options ⇒ Hash
CLI specified options.
-
#collins_client ⇒ Collins::Client
A Collins::Client instance.
-
#find_assets(tags_values, details) ⇒ Array<Collins::Asset>, Array<String>
Given an array of keys and values, return matching assets.
- #find_one_asset(tags_values, details = true) ⇒ Array<Collins::Asset>, NilClass
-
#get_all_tags(include_virtual = true) ⇒ Object
return known tags including virtual ones (ones not stored, but that can be used as query parameters.
-
#get_asset(tag) ⇒ Collins::Asset
Associated with the specified tag.
-
#get_tag_values(tag, resolve_asset = true) ⇒ Array<String>, Array<Collins::Asset>
An array of values associated with the specified tag, or the asset associated with that tag if resolve asset is true.
-
#resolve_asset_tag(tag, stack) ⇒ String, NilClass
Given a possible string tag and the context stack, find the asset tag.
-
#shell_handle ⇒ CollinsShell::Util
A handle on the CLI interface.
-
#tag_from_stack(stack) ⇒ NilClass, String
The asset tag at the top of the stack, if it exists.
- #virtual_tags ⇒ Object
Methods included from Cache
#cache_get_or_else, #clear_cache
Instance Method Details
#asset_context?(stack) ⇒ Boolean
Returns true if in asset context.
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
Returns true if asset associated with specified tag exists.
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
Returns result of calling collins using the specifed block.
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_options ⇒ Hash
Returns CLI specified options.
33 34 35 |
# File 'lib/collins_shell/console/command_helpers.rb', line 33 def CollinsShell::Console. end |
#collins_client ⇒ Collins::Client
Returns A Collins::Client instance.
28 29 30 |
# File 'lib/collins_shell/console/command_helpers.rb', line 28 def collins_client shell_handle.get_collins_client 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 , details q = .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 , details = true find_assets(, 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 include_virtual = true begin cache_get_or_else("get_all_tags(#{include_virtual.to_s})") { = call_collins("get_all_tags"){|c| c.}.map{|t|t.name} if include_virtual then [ + Collins::Asset::Find.to_a].flatten.sort else .sort end } rescue Exception => e puts "Error retrieving tags: #{e}" [] end end |
#get_asset(tag) ⇒ Collins::Asset
Returns associated with the specified tag.
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>
Returns an array of values associated with the specified tag, or the asset associated with that tag if resolve asset is true.
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_handle ⇒ CollinsShell::Util
Returns a handle on the CLI interface.
110 111 112 |
# File 'lib/collins_shell/console/command_helpers.rb', line 110 def shell_handle CollinsShell::Asset.new([], ) end |
#tag_from_stack(stack) ⇒ NilClass, String
Returns the asset tag at the top of the stack, if it exists.
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_tags ⇒ Object
77 78 79 |
# File 'lib/collins_shell/console/command_helpers.rb', line 77 def Collins::Asset::Find.to_a end |