class Convoy::ActionCommand::Base

Attributes

arguments[R]
config[R]
options[R]

Public Class Methods

new(options, arguments, config={}) click to toggle source
# File lib/convoy/action_command/base.rb, line 6
def initialize(options, arguments, config={})
    @options             = options
    @arguments           = arguments
    @config              = config
    @command_context     = nil
    @command_options     = nil
    @parent_options      = nil
    @grandparent_options = nil
    @global_options      = nil
end

Protected Instance Methods

ancestor_options(generation_number = 3) click to toggle source

generation_number 1 is parent, 2 is grandparent and so on default is 3 for great grandparent

# File lib/convoy/action_command/base.rb, line 54
def ancestor_options(generation_number = 3)
    ensure_ancestor(generation_number) { |ancestor_context| context_hash(ancestor_context)[:options] || {} }
end
command_context() click to toggle source
# File lib/convoy/action_command/base.rb, line 19
def command_context
    return @command_context if @command_context
    @command_context     = []
    options[:global]     ||= {}
    current_command_hash = options[:global][:commands] || {}
    until current_command_hash.keys.empty?
        key = current_command_hash.keys.first
        @command_context << key
        current_command_hash = current_command_hash[key][:commands] || {}
    end
    @command_context
end
command_name() click to toggle source
# File lib/convoy/action_command/base.rb, line 32
def command_name
    command_context.last || :global
end
command_options() click to toggle source
# File lib/convoy/action_command/base.rb, line 36
def command_options
    @command_options ||= context_hash(command_context)[:options] || {}
end
global_options() click to toggle source
# File lib/convoy/action_command/base.rb, line 40
def global_options
    @global_options ||= (options[:global] || {})[:options] || {}
end
grandparent_options() click to toggle source
# File lib/convoy/action_command/base.rb, line 48
def grandparent_options
    @grandparent_options ||= ensure_grandparent { |grandparent_context| context_hash(grandparent_context)[:options] || {} }
end
parent_options() click to toggle source
# File lib/convoy/action_command/base.rb, line 44
def parent_options
    @parent_options ||= ensure_parent { |parent_context| context_hash(parent_context)[:options] || {} }
end

Private Instance Methods

context_hash(context) click to toggle source
# File lib/convoy/action_command/base.rb, line 60
def context_hash(context)
    context_hash = options[:global]
    context.each do |command_name|
        context_hash = context_hash[:commands][command_name]
    end
    context_hash
end
ensure_ancestor(generation_number, &block) click to toggle source
# File lib/convoy/action_command/base.rb, line 76
def ensure_ancestor(generation_number, &block)
    return {} if generation_number < 0
    #return {} if generation_number == 0
    return {} unless command_context.size >= generation_number
    ancestor_context = command_context.dup.slice(0, command_context.size - generation_number)
    block.call(ancestor_context)
end
ensure_grandparent(&block) click to toggle source
# File lib/convoy/action_command/base.rb, line 72
def ensure_grandparent(&block)
    ensure_ancestor(2, &block)
end
ensure_parent(&block) click to toggle source
# File lib/convoy/action_command/base.rb, line 68
def ensure_parent(&block)
    ensure_ancestor(1, &block)
end