Skip to content
This repository has been archived by the owner on Dec 12, 2019. It is now read-only.

Commit

Permalink
Removed Mamertes.App and added class hooks specifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Aug 2, 2013
1 parent 675bd8b commit ad0e200
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This application:
require "rubygems"
require "mamertes"

Mamertes.App(name: "Mamertes Usage Test", version: "1.0.0", description: "An example modelled like a TODO application", banner: "Do you like Mamertes?") do
Mamertes::Application.create(name: "Mamertes Usage Test", version: "1.0.0", description: "An example modelled like a TODO application", banner: "Do you like Mamertes?") do
option(:storage, ["f", "file"], {type: String, help: "The file where store TODOs to.", meta: "FILE"})

command :list do
Expand Down
12 changes: 0 additions & 12 deletions lib/mamertes/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,4 @@ def fetch_commands_for_help(command)
command.arguments.collect {|c| c.split(":") }.flatten.collect(&:strip).select(&:present?)
end
end

# Initializes a new Mamertes application.
#
# In options, you can override the command line arguments with `:__args__`, and you can skip execution by specifying `run: false`.
#
# @see Command#setup_with
#
# @param options [Hash] The settings to initialize the application with.
# @return [Application] The created application.
def self.App(options = {}, &block)
Mamertes::Application.create(options, &block)
end
end
54 changes: 40 additions & 14 deletions lib/mamertes/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,29 +394,35 @@ def synopsis(value = nil)
#
# This hook is only executed if no subcommand is executed.
#
# @return [Proc|NilClass] The before hook of this command.
def before(&hook)
@before = hook if block_given? && hook.arity == 1
# @param method [String|Symbol|NilClass] The method of the application to hookup.
# @param hook [Proc] The block to hookup if method is not provided.
# @return [Proc|Symbol|NilClass] The before hook of this command.
def before(method = nil, &hook)
@before = assign_hook(method, &hook) if method || hook
@before
end

# Reads and optionally sets the action of this command.
#
# A command action is only executed if no subcommand is executed.
#
# @return [Proc|NilClass] The action of this command.
def action(&hook)
@action = hook if block_given? && hook.arity == 1
# @param method [String|Symbol|NilClass] The method of the application to hookup.
# @param hook [Proc] The block to hookup if method is not provided.
# @return [Proc|Symbol|NilClass] The action of this command.
def action(method = nil, &hook)
@action = assign_hook(method, &hook) if method || hook
@action
end

# Sets the after hook, that is a block executed after the action of this command.
#
# This hook is only executed if no subcommand is executed.
#
# @return [Proc|NilClass] The after hook of this command.
def after(&hook)
@after = hook if block_given? && hook.arity == 1
# @param method [String|Symbol|NilClass] The method of the application to hookup.
# @param hook [Proc] The block to hookup if method is not provided.
# @return [Proc|Symbol|NilClass] The after hook of this command.
def after(method = nil, &hook)
@after = assign_hook(method, &hook) if method || hook
@after
end

Expand Down Expand Up @@ -469,7 +475,7 @@ def setup_with(options = {})
self
end

# Execute this command, running its action or a subcommand.
# Executes this command, running its action or a subcommand.
#
# @param args [Array] The arguments to pass to the command.
def execute(args)
Expand All @@ -479,25 +485,45 @@ def execute(args)
commands[subcommand[:name]].execute(subcommand[:args])
elsif action then # Run our action
# Run the before hook
before.call(self) if before
execute_hook(before)

# Run the action
action.call(self) if action
execute_hook(action)

# Run the after hook
after.call(self) if after
execute_hook(after)
else # Show the help
show_help
end
end

private
# Setup the application localization.
# Setups the application localization.
#
# @param options [Hash] The settings for this command.
def setup_i18n(options)
i18n_setup(:mamertes, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
self.i18n = (options[:locale]).ensure_string
end

# Assigns a hook to a command.
#
# @param method [String|Symbol|NilClass] The method of the application to hookup.
# @param block [Proc] The block to hookup if method is not provided.
def assign_hook(method, &hook)
assigned = nil
assigned = method if method.is_a?(::String) || method.is_a?(::Symbol)
assigned = hook if !assigned && hook && hook.arity == 1
assigned
end

# Executes a hook.
#
# @param hook [String|Symbol|Proc|NilClass] The hook to execute.
def execute_hook(hook)
if hook then
hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/mamertes/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ module Version
MAJOR = 2

# The minor version.
MINOR = 3
MINOR = 4

# The patch version.
PATCH = 1
PATCH = 0

# The current version number of Mamertes.
STRING = [MAJOR, MINOR, PATCH].compact.join(".")
Expand Down
21 changes: 9 additions & 12 deletions spec/mamertes/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@

describe ".create" do
it "should complain about a missing block" do
expect { ::Mamertes.App }.to raise_error(::Mamertes::Error)
expect { ::Mamertes::Application.create }.to raise_error(::Mamertes::Error)
end

it "should print errors" do
allow(::Mamertes::Application).to receive(:create_application).and_raise(ArgumentError.new("ERROR"))
expect(Kernel).to receive(:puts).with("ERROR")
expect(Kernel).to receive(:exit).with(1)
::Mamertes.App {}
::Mamertes::Application.create(__args__: []) {}
end

it "should create a default application" do
Expand All @@ -130,7 +130,7 @@
it "should execute the block" do
allow_any_instance_of(::Bovem::Console).to receive(:write)
allow(Kernel).to receive(:exit)
options = {name: "OK"}
options = {name: "OK", __args__: []}
check = false

application = ::Mamertes::Application.create(options) { check = true }
Expand All @@ -142,12 +142,16 @@
args = []

application = ::Mamertes::Application.create do
option("require", [], {})
option("format", [], {})
option("example", [], {})

action do |command|
args = command.arguments.join("-")
end
end

expect(args).to eq(ARGV.join("-"))
expect(args).to eq(ARGV.reject {|a| a =~ /^--/ }.join("-"))
end

it "can override arguments" do
Expand All @@ -174,11 +178,4 @@
expect(args).to eq([])
end
end
end

describe "Mamertes::App" do
it "should forward to Mamertes::Application.create" do
expect(::Mamertes::Application).to receive(:create).with("OPTIONS")
::Mamertes.App("OPTIONS")
end
end
end
31 changes: 31 additions & 0 deletions spec/mamertes/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@
valid = Proc.new{|a| puts "OK" }

expect(command.before).to be_nil
expect(command.before(1)).to be_nil
expect(command.before { puts "OK" }).to be_nil
expect(command.before {|a, b| puts "OK" }).to be_nil
expect(command.before(:method)).to eq(:method)
expect(command.action("METHOD")).to eq("METHOD")
expect(command.before(&valid)).to eq(valid)
expect(command.before("METHOD", &valid)).to eq("METHOD")
end
end

Expand All @@ -93,9 +97,13 @@
valid = Proc.new{|a| puts "OK" }

expect(command.action).to be_nil
expect(command.action(1)).to be_nil
expect(command.action { puts "OK" }).to be_nil
expect(command.action {|a, b| puts "OK" }).to be_nil
expect(command.action(:method)).to eq(:method)
expect(command.action("METHOD")).to eq("METHOD")
expect(command.action(&valid)).to eq(valid)
expect(command.action("METHOD", &valid)).to eq("METHOD")
end
end

Expand All @@ -104,9 +112,13 @@
valid = Proc.new{|a| puts "OK" }

expect(command.after).to be_nil
expect(command.after(1)).to be_nil
expect(command.after { puts "OK" }).to be_nil
expect(command.after {|a, b| puts "OK" }).to be_nil
expect(command.after(:method)).to eq(:method)
expect(command.after("METHOD")).to eq("METHOD")
expect(command.after(&valid)).to eq(valid)
expect(command.after("METHOD", &valid)).to eq("METHOD")
end
end

Expand Down Expand Up @@ -317,6 +329,25 @@
expect(check).to eq(["A", "B", "C"])
end

it "should execute the hooks even they are methods" do
check = []
child = []
args = ["command"]

allow(command).to receive(:application).and_return(Object.new)
allow(command.application).to receive(:action_before) { check << "A" }
allow(command.application).to receive("action_perform") { check << "B" }
allow(command.application).to receive(:action_after) { check << "C" }

command.before(:action_before)
command.action("action_perform")
command.after(:action_after)

allow(::Mamertes::Parser).to receive(:parse).and_return(nil)
command.execute(args)
expect(check).to eq(["A", "B", "C"])
end

it "should skip its actions and hooks and pass control to the subcommand" do
check = []
child = []
Expand Down

0 comments on commit ad0e200

Please sign in to comment.