Class: GitCheckCI::Config
- Inherits:
-
Object
- Object
- GitCheckCI::Config
show all
- Defined in:
- lib/git-check-ci/config.rb
Constant Summary
collapse
- Error =
Class.new(RuntimeError)
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Config
configs are directory (well, repo) specific
14
15
16
17
18
19
|
# File 'lib/git-check-ci/config.rb', line 14
def initialize(options = {})
@directory = options.delete(:dir) || Dir.pwd
@chain = options.delete(:chain) || []
@default = options.delete(:default)
raise Error.new("Bad options #{options.inspect}") if options.any?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/git-check-ci/config.rb', line 37
def method_missing(method_name, *args)
if "string".respond_to?(method_name)
to_s.send(method_name, *args)
elsif method_name.to_s =~ /(.*)=$/
self.class.new(:dir => @directory, :chain => @chain + [$1]).send(:set,*args)
else
options = args.first || {}
self.class.new(options.merge :dir => @directory, :chain => @chain + [method_name])
end
end
|
Class Method Details
.method_missing(method_name, *args) ⇒ Object
59
60
61
|
# File 'lib/git-check-ci/config.rb', line 59
def self.method_missing(method_name, *args)
new :chain => [method_name]
end
|
.valid? ⇒ Boolean
64
65
66
|
# File 'lib/git-check-ci/config.rb', line 64
def self.valid?
new.valid?
end
|
Instance Method Details
#get ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/git-check-ci/config.rb', line 82
def get
in_dir do
value = %x(git config #{key}).strip
value.empty? ? (@default || "") : value
end
end
|
#is_git_dir? ⇒ Boolean
51
52
53
54
55
56
|
# File 'lib/git-check-ci/config.rb', line 51
def is_git_dir?
in_dir do
%x(git status 2>/dev/null)
($? == 0)
end
end
|
#set(value) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/git-check-ci/config.rb', line 69
def set(value)
in_dir do
if value.nil?
%x(git config --unset #{key} || true)
success = ($? == 0)
else
success = system("git", "config", key, value)
end
raise Error.new("writing config failed (#{key} -> #{value})") unless success
end
end
|
#to_s ⇒ Object
21
22
23
|
# File 'lib/git-check-ci/config.rb', line 21
def to_s
@to_s ||= get
end
|
#valid? ⇒ Boolean
return true when ci.url, ci.project, ci.status exist
27
28
29
30
31
32
33
34
|
# File 'lib/git-check-ci/config.rb', line 27
def valid?
self.ci.url.to_s
self.ci.project.to_s
self.ci.status.to_s
true
rescue Error
false
end
|