nagios_check 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,8 +38,8 @@ require "nagios_check"
38
38
  class SimpleCheck
39
39
  include NagiosCheck
40
40
 
41
- on "-H HOST", :mandatory, &store(:host)
42
- on "-P PORT", &store(:port, default: 8080, transform: :to_i)
41
+ on "--host HOST", "-H HOST", :mandatory
42
+ on "--port PORT", "-P PORT", Integer, default: 8080
43
43
 
44
44
  enable_warning
45
45
  enable_critical
data/lib/nagios_check.rb CHANGED
@@ -63,17 +63,15 @@ module NagiosCheck
63
63
 
64
64
  module ClassMethods
65
65
  def on(*args, &block)
66
- mandatory = args.delete(:mandatory)
67
- @option_specs[option_name(args.first)] = [args, mandatory, block]
68
- end
69
-
70
- def store(name, opts = {})
71
- @defaults[name] = opts[:default] if opts.has_key?(:default)
72
- transform = opts[:transform]
73
- Proc::new do |value|
74
- value = value.send transform if transform
75
- self.options.send "#{name}=", value
66
+ name = option_name(args.first)
67
+ option_params = {
68
+ mandatory: args.delete(:mandatory) ? true : false
69
+ }
70
+ if args.last.respond_to? :has_key?
71
+ option_params.merge! args.pop
76
72
  end
73
+ @defaults[name] = option_params[:default] if option_params.has_key? :default
74
+ @option_specs[name] = [args, option_params, block]
77
75
  end
78
76
 
79
77
  def defaults
@@ -100,7 +98,8 @@ module NagiosCheck
100
98
 
101
99
  def check_options!(options)
102
100
  @option_specs.each do |name, spec|
103
- if spec[1] == :mandatory && options.send(name).nil?
101
+ _, option_params, _ = spec
102
+ if option_params[:mandatory] && options.send(name).nil?
104
103
  raise MissingOption.new(name)
105
104
  end
106
105
  end
@@ -135,7 +134,12 @@ module NagiosCheck
135
134
  unless @opt_parse
136
135
  opt_parse = OptionParser::new
137
136
  self.class.instance_variable_get("@option_specs").each do |name, spec|
138
- args, mand, block = spec
137
+ args, option_params, block = spec
138
+ if block.nil?
139
+ block = Proc::new do |value|
140
+ self.options.send "#{name}=", value
141
+ end
142
+ end
139
143
  opt_parse.on(*args) do |value|
140
144
  instance_exec(value, &block)
141
145
  end
@@ -1,3 +1,3 @@
1
1
  module NagiosCheck
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/nagios_check.gemspec CHANGED
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_development_dependency "rspec", ">= 2.0.0"
22
+ s.add_development_dependency "rake"
22
23
  end
data/spec/options_spec.rb CHANGED
@@ -60,8 +60,8 @@ describe NagiosCheck do
60
60
  subject do
61
61
  Class::new do
62
62
  include NagiosCheck
63
- on "-a VALUE", &store(:a)
64
- on "-b VALUE", &store(:b, transform: :to_i)
63
+ on "-a VALUE"
64
+ on "-b VALUE", Integer
65
65
  end.new
66
66
  end
67
67
 
@@ -90,11 +90,11 @@ describe NagiosCheck do
90
90
  end
91
91
  end
92
92
 
93
- context "when a mandatory option is specified" do
93
+ context "when a mandatory option is specified in arg list" do
94
94
  subject do
95
95
  Class::new do
96
96
  include NagiosCheck
97
- on "-a VALUE", :mandatory, &store(:a)
97
+ on "-a VALUE", :mandatory, Float
98
98
  end.new
99
99
  end
100
100
 
@@ -104,6 +104,63 @@ describe NagiosCheck do
104
104
  }.should raise_error(NagiosCheck::MissingOption)
105
105
  end
106
106
 
107
- specify { subject.send :parse_options, %w{-a foo} }
107
+ it "parses option a 3.14" do
108
+ subject.send :parse_options, %w{-a 3.14}
109
+ subject.options.a.should == 3.14
110
+ end
111
+ end
112
+
113
+ context "when a mandatory option is specified in option params hash" do
114
+ subject do
115
+ Class::new do
116
+ include NagiosCheck
117
+ on "-a VALUE", Float, mandatory: true
118
+ end.new
119
+ end
120
+
121
+ it "fails if -a is not given" do
122
+ lambda {
123
+ subject.send :parse_options, %w{}
124
+ }.should raise_error(NagiosCheck::MissingOption)
125
+ end
126
+
127
+ it "parses option a 3.14" do
128
+ subject.send :parse_options, %w{-a 3.14}
129
+ subject.options.a.should == 3.14
130
+ end
131
+ end
132
+
133
+ shared_examples_for "default provided" do
134
+ it "defaults to 3.14" do
135
+ subject.send :parse_options, %w{}
136
+ subject.options.a.should == 3.14
137
+ end
138
+
139
+ it "parses option a at 1.4142" do
140
+ subject.send :parse_options, %w{-a 1.4142}
141
+ subject.options.a.should == 1.4142
142
+ end
143
+ end
144
+
145
+ context "when a default is provided" do
146
+ subject do
147
+ Class::new do
148
+ include NagiosCheck
149
+ on "-a VALUE", Float, default: 3.14
150
+ end.new
151
+ end
152
+
153
+ it_behaves_like "default provided"
154
+ end
155
+
156
+ context "when a default is provided and option is mandatory" do
157
+ subject do
158
+ Class::new do
159
+ include NagiosCheck
160
+ on "-a VALUE", Float, default: 3.14, mandatory: true
161
+ end.new
162
+ end
163
+
164
+ it_behaves_like "default provided"
108
165
  end
109
166
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagios_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-09 00:00:00.000000000Z
12
+ date: 2011-12-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2152787360 !ruby/object:Gem::Requirement
16
+ requirement: &2152780360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,18 @@ dependencies:
21
21
  version: 2.0.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2152787360
24
+ version_requirements: *2152780360
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2152779940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152779940
25
36
  description: An easy to use DSL for building custom probes for the Nagios monitoring
26
37
  system
27
38
  email: