ppl 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ppl.rb CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Ppl
3
3
 
4
- Version = "1.0.6"
4
+ Version = "1.1.0"
5
5
 
6
6
  module Adapter
7
7
  end
@@ -52,6 +52,7 @@ require "ppl/command/name"
52
52
  require "ppl/command/email"
53
53
  require "ppl/command/org"
54
54
  require "ppl/command/phone"
55
+ require "ppl/command/mutt"
55
56
 
56
57
  require "ppl/entity/address_book"
57
58
  require "ppl/entity/contact"
@@ -62,6 +63,7 @@ require "ppl/error/incorrect_usage"
62
63
  require "ppl/format/address_book"
63
64
  require "ppl/format/address_book/birthdays"
64
65
  require "ppl/format/address_book/email_addresses"
66
+ require "ppl/format/address_book/mutt_query"
65
67
  require "ppl/format/address_book/names"
66
68
  require "ppl/format/address_book/one_line"
67
69
  require "ppl/format/address_book/organizations"
@@ -9,6 +9,7 @@ class Ppl::Application::Bootstrap
9
9
  Ppl::Command::Add.new,
10
10
  Ppl::Command::Rm.new,
11
11
  Ppl::Command::Ls.new,
12
+ Ppl::Command::Mutt.new,
12
13
  Ppl::Command::Mv.new,
13
14
  Ppl::Command::Show.new,
14
15
  Ppl::Command::Name.new,
@@ -0,0 +1,68 @@
1
+
2
+ class Ppl::Command::Mutt < Ppl::Application::Command
3
+
4
+ attr_writer :format
5
+
6
+ def initialize
7
+ @name = "mutt"
8
+ @description = "Integration with mutt's query_command"
9
+
10
+ @format = Ppl::Format::AddressBook::MuttQuery.new
11
+ end
12
+
13
+ def options(parser, options)
14
+ parser.banner = "usage: ppl mutt <query>"
15
+ end
16
+
17
+ def execute(input, output)
18
+ query = input.arguments.shift
19
+ if query.nil?
20
+ raise Ppl::Error::IncorrectUsage, "You must provide a query"
21
+ end
22
+
23
+ address_book = @storage.load_address_book
24
+
25
+ matches = mutt_search(address_book, query)
26
+
27
+ if matches.count > 0
28
+
29
+ line = sprintf(
30
+ "Searching database... %d entries... %d matching:",
31
+ address_book.count,
32
+ matches.count
33
+ )
34
+
35
+ results = @format.process(matches)
36
+
37
+ output.line(line)
38
+ output.line(results) unless results == ""
39
+ true
40
+
41
+ else
42
+ output.line("No matches")
43
+ false
44
+ end
45
+
46
+ end
47
+
48
+
49
+ private
50
+
51
+ def mutt_search(address_book, query)
52
+ matches = Ppl::Entity::AddressBook.new
53
+
54
+ address_book.each do |contact|
55
+ next if contact.email_address.nil?
56
+
57
+ if contact.email_address.include?(query)
58
+ matches.add_contact(contact)
59
+ elsif !contact.name.nil? && contact.name.include?(query)
60
+ matches.add_contact(contact)
61
+ end
62
+ end
63
+
64
+ matches
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,33 @@
1
+
2
+ class Ppl::Format::AddressBook::MuttQuery < Ppl::Format::AddressBook
3
+
4
+ attr_writer :table
5
+
6
+ def initialize
7
+ @table = Ppl::Format::Table.new([:email, :name])
8
+ @table.separator = Ppl::Format::Table::SEPARATOR_TABS
9
+ end
10
+
11
+ def process(address_book)
12
+ address_book.each { |contact| add_row(contact) }
13
+ @table.to_s
14
+ end
15
+
16
+
17
+ private
18
+
19
+ def add_row(contact)
20
+ name = nil
21
+ if !contact.name.nil?
22
+ name = contact.name
23
+ end
24
+
25
+ @table.add_row({
26
+ :email => contact.email_address,
27
+ :name => name,
28
+ })
29
+ end
30
+
31
+
32
+ end
33
+
@@ -1,12 +1,17 @@
1
1
 
2
2
  class Ppl::Format::Table
3
3
 
4
+ SEPARATOR_SPACES = 0
5
+ SEPARATOR_TABS = 1
6
+
4
7
  attr_accessor :columns
5
8
  attr_accessor :rows
9
+ attr_accessor :separator
6
10
 
7
11
  def initialize(columns=[])
8
- @columns = columns
9
- @rows = []
12
+ @columns = columns
13
+ @rows = []
14
+ @separator = SEPARATOR_SPACES
10
15
 
11
16
  @column_widths = {}
12
17
  @columns.each { |c| @column_widths[c] = 0 }
@@ -40,7 +45,11 @@ class Ppl::Format::Table
40
45
 
41
46
  def format_cell(row, column)
42
47
  width = @column_widths[column]
43
- string = sprintf("%-#{width}s ", row[column])
48
+ if @separator == SEPARATOR_SPACES
49
+ string = sprintf("%-#{width}s ", row[column])
50
+ else
51
+ string = sprintf("%s\t", row[column])
52
+ end
44
53
  return string
45
54
  end
46
55
 
data/ppl.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
  Gem::Specification.new do |spec|
3
3
 
4
4
  spec.name = "ppl"
5
- spec.version = "1.0.6"
5
+ spec.version = "1.1.0"
6
6
  spec.date = "2012-12-20"
7
7
 
8
8
  spec.required_ruby_version = ">= 1.9.3"
@@ -48,6 +48,9 @@ describe Ppl::Application::Bootstrap do
48
48
  it "should contain the 'ls' command" do
49
49
  @bootstrap.command_suite.find_command("ls").should_not be nil
50
50
  end
51
+ it "should contain the 'mutt' command" do
52
+ @bootstrap.command_suite.find_command("mutt").should_not be nil
53
+ end
51
54
  it "should contain the 'mv' command" do
52
55
  @bootstrap.command_suite.find_command("mv").should_not be nil
53
56
  end
@@ -0,0 +1,68 @@
1
+
2
+ describe Ppl::Command::Mutt do
3
+
4
+ before(:each) do
5
+ @command = Ppl::Command::Mutt.new
6
+ @input = Ppl::Application::Input.new
7
+ @output = double(Ppl::Application::Output)
8
+ @storage = double(Ppl::Adapter::Storage)
9
+
10
+ @address_book = Ppl::Entity::AddressBook.new
11
+ @contact = Ppl::Entity::Contact.new
12
+
13
+ @command.storage = @storage
14
+ end
15
+
16
+ describe "#name" do
17
+ it "should be 'mutt'" do
18
+ @command.name.should eq "mutt"
19
+ end
20
+ end
21
+
22
+ describe "#execute" do
23
+
24
+ it "should raise an error if no query is given" do
25
+ expect{@command.execute(@input, @output)}.to raise_error(Ppl::Error::IncorrectUsage)
26
+ end
27
+
28
+ it "should search the address book for the query" do
29
+ @storage.should_receive(:load_address_book).and_return(@address_book)
30
+ @input.arguments.push "query"
31
+ @command.stub(:mutt_search) { |ab| [] }
32
+ @output.should_receive(:line).with("No matches")
33
+ @command.execute(@input, @output).should eq false
34
+ end
35
+
36
+ it "should return email address matches" do
37
+
38
+ @contact.name = "Test User"
39
+ @contact.email_address = "test@example.org"
40
+ @address_book.add_contact(@contact)
41
+
42
+ @input.arguments.push "example"
43
+
44
+ @storage.should_receive(:load_address_book).and_return(@address_book)
45
+ @output.should_receive(:line).with("Searching database... 1 entries... 1 matching:")
46
+ @output.should_receive(:line).with("test@example.org\tTest User")
47
+ @command.execute(@input, @output).should eq true
48
+ end
49
+
50
+ it "should return name matches" do
51
+
52
+ @contact.name = "Test User"
53
+ @contact.email_address = "test@example.org"
54
+ @address_book.add_contact(@contact)
55
+
56
+ @input.arguments.push "User"
57
+
58
+ @storage.should_receive(:load_address_book).and_return(@address_book)
59
+ @output.should_receive(:line).with("Searching database... 1 entries... 1 matching:")
60
+ @output.should_receive(:line).with("test@example.org\tTest User")
61
+ @command.execute(@input, @output).should eq true
62
+ end
63
+
64
+
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,30 @@
1
+
2
+ describe Ppl::Format::AddressBook::MuttQuery do
3
+
4
+ before(:each) do
5
+ @format = Ppl::Format::AddressBook::MuttQuery.new
6
+ @address_book = Ppl::Entity::AddressBook.new
7
+ @contact = Ppl::Entity::Contact.new
8
+ @table = double(Ppl::Format::Table)
9
+
10
+ @contact.email_address = "test@example.org"
11
+ @contact.name = "Test Contact"
12
+
13
+ @format.table = @table
14
+ @address_book.add_contact(@contact)
15
+ end
16
+
17
+ describe "#process" do
18
+
19
+ it "should list the given contacts" do
20
+ @table.should_receive(:add_row).with({
21
+ :email => "test@example.org",
22
+ :name => "Test Contact",
23
+ })
24
+ @format.process(@address_book)
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
@@ -50,6 +50,16 @@ describe Ppl::Format::Table do
50
50
  @table.to_s.should eq "12345 John Doe jdoe@example.org"
51
51
  end
52
52
 
53
+ it "should use tabs if requested" do
54
+ @table.add_row({
55
+ :id => 12345,
56
+ :name => "John Doe",
57
+ :email => "jdoe@example.org",
58
+ })
59
+ @table.separator = Ppl::Format::Table::SEPARATOR_TABS
60
+ @table.to_s.should eq "12345\tJohn Doe\tjdoe@example.org"
61
+ end
62
+
53
63
  it "should align multiple rows into neat columns" do
54
64
  @table.add_row({
55
65
  :id => 12345,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ppl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-12-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: inifile
16
- requirement: &16103420 !ruby/object:Gem::Requirement
16
+ requirement: &15938960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.0.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *16103420
24
+ version_requirements: *15938960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rugged
27
- requirement: &16091980 !ruby/object:Gem::Requirement
27
+ requirement: &15936860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.17.0.b6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *16091980
35
+ version_requirements: *15936860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: vpim
38
- requirement: &16091260 !ruby/object:Gem::Requirement
38
+ requirement: &15936120 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - =
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0.695'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *16091260
46
+ version_requirements: *15936120
47
47
  description: CLI Address Book
48
48
  email: henry@henrysmith.org
49
49
  executables:
@@ -80,6 +80,7 @@ files:
80
80
  - lib/ppl/command/help.rb
81
81
  - lib/ppl/command/init.rb
82
82
  - lib/ppl/command/ls.rb
83
+ - lib/ppl/command/mutt.rb
83
84
  - lib/ppl/command/mv.rb
84
85
  - lib/ppl/command/name.rb
85
86
  - lib/ppl/command/org.rb
@@ -93,6 +94,7 @@ files:
93
94
  - lib/ppl/format/address_book.rb
94
95
  - lib/ppl/format/address_book/birthdays.rb
95
96
  - lib/ppl/format/address_book/email_addresses.rb
97
+ - lib/ppl/format/address_book/mutt_query.rb
96
98
  - lib/ppl/format/address_book/names.rb
97
99
  - lib/ppl/format/address_book/one_line.rb
98
100
  - lib/ppl/format/address_book/organizations.rb
@@ -126,6 +128,7 @@ files:
126
128
  - spec/ppl/command/help_spec.rb
127
129
  - spec/ppl/command/init_spec.rb
128
130
  - spec/ppl/command/ls_spec.rb
131
+ - spec/ppl/command/mutt_spec.rb
129
132
  - spec/ppl/command/mv_spec.rb
130
133
  - spec/ppl/command/name_spec.rb
131
134
  - spec/ppl/command/org_spec.rb
@@ -136,6 +139,7 @@ files:
136
139
  - spec/ppl/entity/contact_spec.rb
137
140
  - spec/ppl/format/address_book/birthdays_spec.rb
138
141
  - spec/ppl/format/address_book/email_addresses_spec.rb
142
+ - spec/ppl/format/address_book/mutt_query_spec.rb
139
143
  - spec/ppl/format/address_book/names_spec.rb
140
144
  - spec/ppl/format/address_book/one_line_spec.rb
141
145
  - spec/ppl/format/address_book/organizations_spec.rb