gas 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gas.rb CHANGED
@@ -1,34 +1,24 @@
1
- require 'fileutils'
2
-
3
1
  require 'gas/version'
4
2
  require 'gas/user'
5
- require 'gas/configuration'
6
-
7
- # TODO: Refactor this file
3
+ require 'gas/config'
4
+ require 'gas/gitconfig'
8
5
 
9
6
  module Gas
10
7
 
11
- @config = File.expand_path('~/.gas')
12
- @gitconfig = File.expand_path('~/.gitconfig')
8
+ @config = Config.new
9
+ @gitconfig = Gitconfig.new
13
10
 
14
11
  # Lists all authors
15
12
  def self.list
16
- if !File.exists? @config
17
- FileUtils.touch @config
18
- end
19
-
20
- config = File.read(@config)
21
- configuration = Configuration.parse config
22
13
  puts 'Available users:'
23
- puts configuration
14
+ puts @config
24
15
 
25
16
  self.show
26
17
  end
27
18
 
28
19
  # Shows the current user
29
20
  def self.show
30
- gitconfig = File.read(@gitconfig)
31
- user = Gas::Configuration.current_user gitconfig
21
+ user = @gitconfig.current_user
32
22
  puts 'Current user:'
33
23
  puts "#{user.name} <#{user.email}>"
34
24
  end
@@ -36,26 +26,11 @@ module Gas
36
26
  # Sets _nickname_ as current user
37
27
  # @param [String] nickname The nickname to use
38
28
  def self.use(nickname)
39
- if !File.exists? @config
40
- FileUtils.touch @config
41
- end
42
-
43
- config = File.read(@config)
44
- configuration = Configuration.parse config
29
+ self.no_user? nickname
30
+ user = @config[nickname]
45
31
 
46
- if !configuration.exists? nickname
47
- puts "Nickname #{nickname} does not exist"
48
- return
49
- end
50
-
51
- user = configuration[nickname]
52
-
53
- gitconfig = File.read(@gitconfig)
54
- gitconfig.gsub! /name\s?=\s?.+/, "name = #{user.name}"
55
- gitconfig.gsub! /email\s?=\s?.+/, "email = #{user.email}"
56
- File.open @gitconfig, 'w' do |file|
57
- file.write gitconfig
58
- end
32
+ @gitconfig.change_user user.name, user.email
33
+ @gitconfig.save!
59
34
 
60
35
  self.show
61
36
  end
@@ -65,19 +40,10 @@ module Gas
65
40
  # @param [String] name The name of the author
66
41
  # @param [String] email The email of the author
67
42
  def self.add(nickname, name, email)
68
- config = File.read(@config)
69
- configuration = Configuration.parse config
70
-
71
- if configuration.exists? nickname
72
- puts "Nickname #{nickname} does already exist"
73
- return
74
- end
75
-
43
+ self.has_user? nickname
76
44
  user = User.new name, email, nickname
77
- configuration.add user
78
- File.open @config, 'w' do |file|
79
- file.write configuration
80
- end
45
+ @config.add user
46
+ @config.save!
81
47
 
82
48
  puts 'Added author'
83
49
  puts user
@@ -86,25 +52,34 @@ module Gas
86
52
  # Deletes a author from the config using nickname
87
53
  # @param [String] nickname The nickname of the author
88
54
  def self.delete(nickname)
89
- config = File.read(@config)
90
- configuration = Configuration.parse config
91
-
92
- if !configuration.exists? nickname
93
- puts "Nickname #{nickname} does not exist"
94
- return
95
- end
96
-
97
- configuration.delete nickname
98
- File.open @config, 'w' do |file|
99
- file.write configuration
100
- end
55
+ self.no_user? nickname
56
+ @config.delete nickname
57
+ @config.save!
101
58
 
102
59
  puts "Deleted author #{nickname}"
103
60
  end
104
61
 
105
62
  # Prints the current version
106
63
  def self.version
107
- puts Gas::Version
64
+ puts Gas::VERSION
65
+ end
66
+
67
+ # Checks if the user exists and gives error and exit if not
68
+ # @param [String] nickname
69
+ def self.no_user?(nickname)
70
+ if !@config.exists? nickname
71
+ puts "Nickname #{nickname} does not exist"
72
+ exit
73
+ end
74
+ end
75
+
76
+ # Checks if the user exists and gives error and exit if so
77
+ # @param [String] nickname
78
+ def self.has_user?(nickname)
79
+ if @config.exists? nickname
80
+ puts "Nickname #{nickname} does already exist"
81
+ exit
82
+ end
108
83
  end
109
84
 
110
85
  end
@@ -1,26 +1,36 @@
1
+ require 'fileutils'
2
+
1
3
  module Gas
2
4
 
3
5
  # Class that keeps track of users
4
- class Configuration
6
+ class Config
5
7
  attr_reader :users
6
8
 
7
- # Parses [User]s from a string of the config file
8
- # @param [String] config The configuration file
9
- # @return [Configuration]
10
- def self.parse(config)
11
- users = []
12
- config.scan(/\[(.+)\]\s+name = (.+)\s+email = (.+)/) do |nickname, name, email|
13
- users << User.new(name, email, nickname)
14
- end
9
+ # Initializes the object. If no users are supplied we look for a config file, if none then create it, and parse it to load users
10
+ # @param [Array<User>] users The override users
11
+ # @param [String] config The override config
12
+ def initialize(users = nil, config = nil)
13
+ @config_file = File.expand_path('~/.gas')
14
+ @config = ''
15
15
 
16
- Configuration.new users
17
- end
16
+ if config.nil?
17
+ if !File.exists? @config_file
18
+ FileUtils.touch @config_file
19
+ end
18
20
 
19
- # Can parse out the current user from the gitconfig
20
- # @param [String] gitconfig The git configuration
21
- # @return [User] The current user
22
- def self.current_user(gitconfig)
23
- User.parse gitconfig
21
+ @config = File.read(@config_file)
22
+ else
23
+ @config = config
24
+ end
25
+
26
+ if users.nil?
27
+ @users = []
28
+ @config.scan(/\[(.+)\]\s+name = (.+)\s+email = (.+)/) do |nickname, name, email|
29
+ @users << User.new(name, email, nickname)
30
+ end
31
+ else
32
+ @users = users
33
+ end
24
34
  end
25
35
 
26
36
  # Checks if a user with _nickname_ exists
@@ -70,9 +80,11 @@ module Gas
70
80
  end
71
81
  end
72
82
 
73
- # @param [Array<User>] users
74
- def initialize(users)
75
- @users = users
83
+ # Saves the current users to the config file
84
+ def save!
85
+ File.open @config_file, 'w' do |file|
86
+ file.write @config
87
+ end
76
88
  end
77
89
 
78
90
  # Override to_s to output correct format
@@ -0,0 +1,45 @@
1
+ module Gas
2
+
3
+ # Class that class that interacts with the .gitconfig
4
+ class Gitconfig
5
+
6
+ def initialize(gitconfig = nil)
7
+ @gitconfig_file = File.expand_path('~/.gitconfig')
8
+ @gitconfig = ''
9
+
10
+ if gitconfig.nil?
11
+ @gitconfig = File.read(@gitconfig_file)
12
+ else
13
+ @gitconfig = gitconfig
14
+ end
15
+ end
16
+
17
+ # Parse out the current user from the gitconfig
18
+ # TODO: Error handling
19
+ # @param [String] gitconfig The git configuration
20
+ # @return [User] The current user
21
+ def current_user
22
+ regex = /\[user\]\s+name = (.+)\s+email = (.+)/
23
+ matches = regex.match @gitconfig
24
+
25
+ User.new matches[1], matches[2]
26
+ end
27
+
28
+ # Changes the user
29
+ # @param [String] name The new name
30
+ # @param [String] email The new email
31
+ def change_user(name, email)
32
+ @gitconfig.gsub! /name\s?=\s?.+/, "name = #{name}"
33
+ @gitconfig.gsub! /email\s?=\s?.+/, "email = #{email}"
34
+ end
35
+
36
+ # Saves the gitconfig
37
+ def save!
38
+ File.open @gitconfig_file, 'w' do |file|
39
+ file.write @gitconfig
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+
@@ -4,16 +4,6 @@ module Gas
4
4
  class User
5
5
  attr_reader :name, :email, :nickname
6
6
 
7
- # Static function to parse out the data from a git config string
8
- # @param [String] gitconfig
9
- # @return [User] a new user
10
- def self.parse(gitconfig)
11
- regex = /\[user\]\s+name = (.+)\s+email = (.+)/
12
- matches = regex.match gitconfig
13
-
14
- User.new matches[1], matches[2]
15
- end
16
-
17
7
  # @param [String] name The name of the user
18
8
  # @param [String] email The email of the user
19
9
  # @param [String] nickname A nickname for the user, not used when parsing from gitconfig
@@ -1,6 +1,6 @@
1
1
  module Gas
2
2
 
3
- Version = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
 
5
5
  end
6
6
 
@@ -2,33 +2,28 @@ require './spec/spec_helper'
2
2
 
3
3
  require './lib/gas'
4
4
 
5
- describe Gas::Configuration do
6
- it 'should be able to parse users from config format' do
7
- name = 'Fredrik Wallgren'
8
- email = 'fredrik.wallgren@gmail.com'
9
- nickname = 'walle'
10
- config = "[#{nickname}]\n name = #{name}\n email = #{email}\n\n[user2]\n name = foo\n email = bar"
11
- config = Gas::Configuration.parse config
12
- config.users.count.should == 2
13
- config.users[0].name.should == name
14
- config.users[0].email.should == email
15
- config.users[0].nickname.should == nickname
5
+ describe Gas::Config do
6
+
7
+ before :each do
8
+ @name = 'Fredrik Wallgren'
9
+ @email = 'fredrik.wallgren@gmail.com'
10
+ @nickname = 'walle'
11
+ config = "[#{@nickname}]\n name = #{@name}\n email = #{@email}\n\n[user2]\n name = foo\n email = bar"
12
+ @config = Gas::Config.new nil, config
16
13
  end
17
14
 
18
- it 'should be able to get current user from gitconfig' do
19
- name = 'Fredrik Wallgren'
20
- email = 'fredrik.wallgren@gmail.com'
21
- gitconfig = "[other stuff]\n foo = bar\n\n[user]\n name = #{name}\n email = #{email}\n\n[foo]\n bar = foo"
22
- user = Gas::Configuration.current_user gitconfig
23
- user.name.should == name
24
- user.email.should == email
15
+ it 'should be able to parse users from config format' do
16
+ @config.users.count.should == 2
17
+ @config.users[0].name.should == @name
18
+ @config.users[0].email.should == @email
19
+ @config.users[0].nickname.should == @nickname
25
20
  end
26
21
 
27
22
  it 'should output the users in the correct format' do
28
23
  user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
29
24
  user2 = Gas::User.new 'foo', 'bar', 'user2'
30
25
  users = [user1, user2]
31
- config = Gas::Configuration.new users
26
+ config = Gas::Config.new users
32
27
  config.to_s.should == "[walle]\n name = Fredrik Wallgren\n email = fredrik.wallgren@gmail.com\n[user2]\n name = foo\n email = bar"
33
28
  end
34
29
 
@@ -36,7 +31,7 @@ describe Gas::Configuration do
36
31
  user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
37
32
  user2 = Gas::User.new 'foo', 'bar', 'user2'
38
33
  users = [user1, user2]
39
- config = Gas::Configuration.new users
34
+ config = Gas::Config.new users
40
35
  config.exists?('walle').should be_true
41
36
  config.exists?('foo').should be_false
42
37
  config.exists?('user2').should be_true
@@ -46,7 +41,7 @@ describe Gas::Configuration do
46
41
  user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
47
42
  user2 = Gas::User.new 'foo', 'bar', 'user2'
48
43
  users = [user1, user2]
49
- config = Gas::Configuration.new users
44
+ config = Gas::Config.new users
50
45
  config.get('walle').should == user1
51
46
  config.get('user2').should == user2
52
47
  config['walle'].should == user1
@@ -59,7 +54,7 @@ describe Gas::Configuration do
59
54
  user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
60
55
  user2 = Gas::User.new 'foo', 'bar', 'user2'
61
56
  users = [user1]
62
- config = Gas::Configuration.new users
57
+ config = Gas::Config.new users
63
58
  config.users.count.should == 1
64
59
  config.add user2
65
60
  config.users.count.should == 2
@@ -69,7 +64,7 @@ describe Gas::Configuration do
69
64
  user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
70
65
  user2 = Gas::User.new 'foo', 'bar', 'user2'
71
66
  users = [user1, user2]
72
- config = Gas::Configuration.new users
67
+ config = Gas::Config.new users
73
68
  config.users.count.should == 2
74
69
  config.delete 'walle'
75
70
  config.users.count.should == 1
@@ -0,0 +1,29 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+
5
+ describe Gas::Gitconfig do
6
+
7
+ before :each do
8
+ @name = 'Fredrik Wallgren'
9
+ @email = 'fredrik.wallgren@gmail.com'
10
+ gitconfig = "[other stuff]\n foo = bar\n\n[user]\n name = #{@name}\n email = #{@email}\n\n[foo]\n bar = foo"
11
+ @gitconfig = Gas::Gitconfig.new gitconfig
12
+ end
13
+
14
+ it 'should be able to get current user from gitconfig' do
15
+ user = @gitconfig.current_user
16
+ user.name.should == @name
17
+ user.email.should == @email
18
+ end
19
+
20
+ it 'should be able to change the current user' do
21
+ name = 'Test Testsson'
22
+ email = 'test@testsson.com'
23
+ @gitconfig.change_user name, email
24
+
25
+ user = @gitconfig.current_user
26
+ user.name.should == name
27
+ user.email.should == email
28
+ end
29
+ end
@@ -3,15 +3,6 @@ require './spec/spec_helper'
3
3
  require './lib/gas'
4
4
 
5
5
  describe Gas::User do
6
- it 'should be able to parse name and email from gitconfig format' do
7
- name = 'Fredrik Wallgren'
8
- email = 'fredrik.wallgren@gmail.com'
9
- gitconfig = "[other stuff]\n foo = bar\n\n[user]\n name = #{name}\n email = #{email}\n\n[foo]\n bar = foo"
10
- user = Gas::User.parse gitconfig
11
- user.name.should == name
12
- user.email.should == email
13
- end
14
-
15
6
  it 'should output in the right format' do
16
7
  name = 'Fredrik Wallgren'
17
8
  email = 'fredrik.wallgren@gmail.com'
@@ -1,6 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.configure do |config|
4
- config.mock_with :rr
5
- end
6
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-20 00:00:00.000000000 +02:00
12
+ date: 2011-07-21 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: thor
17
- requirement: &19173520 !ruby/object:Gem::Requirement
17
+ requirement: &22238120 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 0.14.6
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *19173520
25
+ version_requirements: *22238120
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &19210280 !ruby/object:Gem::Requirement
28
+ requirement: &22237700 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,21 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *19210280
37
- - !ruby/object:Gem::Dependency
38
- name: rr
39
- requirement: &19209820 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ! '>='
43
- - !ruby/object:Gem::Version
44
- version: '0'
45
- type: :development
46
- prerelease: false
47
- version_requirements: *19209820
36
+ version_requirements: *22237700
48
37
  - !ruby/object:Gem::Dependency
49
38
  name: bundler
50
- requirement: &19209400 !ruby/object:Gem::Requirement
39
+ requirement: &22274100 !ruby/object:Gem::Requirement
51
40
  none: false
52
41
  requirements:
53
42
  - - ! '>='
@@ -55,7 +44,7 @@ dependencies:
55
44
  version: '0'
56
45
  type: :development
57
46
  prerelease: false
58
- version_requirements: *19209400
47
+ version_requirements: *22274100
59
48
  description: Gas is a utility to keep track of your git authors. Add them to gas and
60
49
  switch at any time. Great if you use one author at work and one at home for instance.
61
50
  email: fredrik.wallgren@gmail.com
@@ -70,10 +59,12 @@ files:
70
59
  - lib/gas.rb
71
60
  - lib/gas/version.rb
72
61
  - lib/gas/user.rb
73
- - lib/gas/configuration.rb
62
+ - lib/gas/gitconfig.rb
63
+ - lib/gas/config.rb
74
64
  - spec/spec_helper.rb
75
65
  - spec/gas/user_spec.rb
76
- - spec/gas/configuration_spec.rb
66
+ - spec/gas/config_spec.rb
67
+ - spec/gas/gitconfig_spec.rb
77
68
  - LICENSE
78
69
  - README.textile
79
70
  has_rdoc: true