Skip to content

Commit

Permalink
Create CLI command object for shared subcommand behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Bridges committed Sep 12, 2013
1 parent 1afe7c5 commit cc52b67
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
15 changes: 4 additions & 11 deletions lib/minty/cli/accounts.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
require 'mixlib/cli'
require 'text-table'

require 'minty/cli/command'

module Minty
class CLI
class Accounts
include Mixlib::CLI

def initialize(*args)
@credentials = Minty::Credentials.load
@client = Minty::Client.new(@credentials)
super
end

class Accounts < Command
def exec
table = Text::Table.new
table.head = %w[Name Value Type]
@client.accounts.sort_by { |a| -a.value }.each do |account|
client.accounts.sort_by { |a| -a.value }.each do |account|
table.rows << [account.name, account.value, account.type.capitalize]
end
puts table
Expand Down
18 changes: 18 additions & 0 deletions lib/minty/cli/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'mixlib/cli'

module Minty
class CLI
class Command
include Mixlib::CLI

attr_reader :credentials, :client

def initialize(*args)
@credentials = Minty::Credentials.load
@client = Minty::Client.new(@credentials)
super
end

end
end
end
15 changes: 4 additions & 11 deletions lib/minty/cli/goals.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
require 'mixlib/cli'
require 'text-table'

require 'minty/cli/command'

module Minty
class CLI
class Goals
include Mixlib::CLI

class Goals < Command
banner "Usage: minty budget [options]"

def initialize(*args)
@credentials = Minty::Credentials.load
@client = Minty::Client.new(@credentials)
super
end

def exec
table = Text::Table.new
table.head = %w[Name Amount Budget Status]

@client.goals.each do |status, goals|
client.goals.each do |status, goals|
goals.each do |goal|
table.rows << [
goal.name,
Expand Down
5 changes: 3 additions & 2 deletions lib/minty/cli/login.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'io/console'

require 'minty/cli/command'

module Minty
class CLI
class Login
include Mixlib::CLI
class Login < Command

banner "Usage: minty login [options]"

Expand Down
13 changes: 4 additions & 9 deletions lib/minty/cli/refresh.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
require 'minty/cli/command'

module Minty
class CLI
class Refresh
include Mixlib::CLI
class Refresh < Command

banner "Usage: minty refresh"

def initialize(*args)
super
end

def exec
@credentials = Minty::Credentials.load
@client = Minty::Client.new(@credentials)
@client.refresh
client.refresh
puts "Refreshing..."
end

Expand Down

0 comments on commit cc52b67

Please sign in to comment.