anodator 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -15,27 +15,24 @@ module Anodator
15
15
  if @message.split(//).size.zero?
16
16
  raise ArgumentError.new("message cannot be blank")
17
17
  end
18
- unless [Rule::LEVEL_ERROR, Rule::LEVEL_WARNING].include?(level)
19
- raise ArgumentError.new("level must be ERROR or WARNING")
18
+ unless Rule::ERROR_LEVELS.values.include?(level)
19
+ raise ArgumentError.new("level must be #{Rule::ERROR_LEVEL_NAMES.join(", ")}")
20
20
  end
21
21
  end
22
22
 
23
23
  def to_s
24
- buf = "[ ]\t"
25
- if @level == Rule::LEVEL_WARNING
26
- buf = "[WARING]\t"
27
- else
28
- buf = "[ERROR ]\t"
29
- end
24
+ buf = "[#{Rule.level_expression(@level)}]\t"
30
25
  buf += @message + " |#{@target_numbers.join(", ")}|"
31
26
  end
32
27
 
33
- def error?
34
- return Rule::LEVEL_ERROR == @level
35
- end
28
+ def method_missing(message, *args)
29
+ if message.to_s =~ /(\A[a-zA-Z_]+)\?\Z/
30
+ if Rule::ERROR_LEVELS.keys.include?($1.to_sym)
31
+ return Rule::ERROR_LEVELS[$1.to_sym] == @level
32
+ end
33
+ end
36
34
 
37
- def warning?
38
- return Rule::LEVEL_WARNING == @level
35
+ super
39
36
  end
40
37
  end
41
38
  end
data/lib/anodator/rule.rb CHANGED
@@ -8,14 +8,54 @@ module Anodator
8
8
  # Rule has target expressions, prerequisite, validator and message.
9
9
  # "Prerequisite" is represented by the Validator.
10
10
  class Rule
11
- # Check level ERROR
12
- LEVEL_ERROR = 2
13
- # Check level WARNING
14
- LEVEL_WARNING = 1
11
+ # Check levels
12
+ #
13
+ # default levels are error and warning.
14
+ # You can add any levels.
15
+ ERROR_LEVELS = {
16
+ :error => 2, # ERROR
17
+ :warning => 1, # WARNING
18
+ }
19
+
20
+ # Check level names
21
+ #
22
+ # Check level name labels
23
+ ERROR_LEVEL_NAMES = {
24
+ :error => "ERROR",
25
+ :warning => "WARNING",
26
+ }
15
27
 
16
28
  attr_reader :target_expressions, :message, :validator, :prerequisite, :level, :description
17
29
 
18
- def initialize(target_expressions, message, validator, prerequisite = nil, level = LEVEL_ERROR, description = nil)
30
+ def self.add_error_level(value, symbol, label)
31
+ # value check
32
+ raise "Error level value must be Integer" unless value.is_a? Integer
33
+ raise "Error level value must be greater than zero" unless value > 0
34
+ raise "Error level value #{value} already exists" if ERROR_LEVELS.values.include?(value)
35
+ # symbol check
36
+ raise "Error level symbol must be symbol" unless symbol.is_a? Symbol
37
+ raise "Error level symbol #{symbol} already exists" if ERROR_LEVELS.keys.include?(symbol)
38
+ # label check
39
+ raise "Error level label must be string" unless label.is_a? String
40
+ raise "Error level label #{label} already exists" if ERROR_LEVEL_NAMES.values.include?(label)
41
+
42
+ # check OK
43
+ ERROR_LEVELS[symbol] = value
44
+ ERROR_LEVEL_NAMES[symbol] = label
45
+ end
46
+
47
+ def self.remove_error_level(symbol)
48
+ # symbol check
49
+ raise "Unknown rror level symbol #{symbol}" unless ERROR_LEVELS.keys.include?(symbol)
50
+ # count check
51
+ raise "Error levels must be atleast one value" if ERROR_LEVELS.size == 1
52
+
53
+ # check OK
54
+ ERROR_LEVELS.delete(symbol)
55
+ ERROR_LEVEL_NAMES.delete(symbol)
56
+ end
57
+
58
+ def initialize(target_expressions, message, validator, prerequisite = nil, level = ERROR_LEVELS.values.sort.last, description = nil)
19
59
  @target_expressions = target_expressions.to_a
20
60
  @message = message
21
61
  @validator = validator
@@ -32,8 +72,8 @@ module Anodator
32
72
  if @validator.nil?
33
73
  raise ArgumentError.new("validator cannot be blank")
34
74
  end
35
- unless [LEVEL_ERROR, LEVEL_WARNING].include?(@level)
36
- raise ArgumentError.new("level must be ERROR or WARNING")
75
+ unless ERROR_LEVELS.values.include?(@level)
76
+ raise ArgumentError.new("level must be #{ERROR_LEVEL_NAMES.join(", ")}.")
37
77
  end
38
78
  end
39
79
 
@@ -73,11 +113,15 @@ module Anodator
73
113
  end
74
114
 
75
115
  def level_expression
76
- if @level == LEVEL_ERROR
77
- return "ERROR"
78
- elsif @level == LEVEL_WARNING
79
- return "WARNING"
116
+ return Rule.level_expression(@level)
117
+ end
118
+
119
+ def self.level_expression(level)
120
+ if ERROR_LEVELS.values.include?(level)
121
+ return ERROR_LEVEL_NAMES[ERROR_LEVELS.index(level)]
80
122
  end
123
+
124
+ return nil
81
125
  end
82
126
 
83
127
  def to_s
@@ -209,14 +209,11 @@ module Anodator
209
209
  if !row[4].nil? && prerequisite.nil?
210
210
  raise "Unknown validator identifier '#{row[4]}'"
211
211
  end
212
- level = case row[5]
213
- when "ERROR"
214
- Rule::LEVEL_ERROR
215
- when "WARNING"
216
- Rule::LEVEL_WARNING
217
- else
218
- raise "Unknown error type '#{row[5]}'"
219
- end
212
+ if Rule::ERROR_LEVEL_NAMES.values.include?(row[5])
213
+ level = Rule::ERROR_LEVELS[Rule::ERROR_LEVEL_NAMES.index(row[5])]
214
+ else
215
+ raise "Unknown error type '#{row[5]}'"
216
+ end
220
217
  message = Message.new(row[6])
221
218
 
222
219
  rule_set <<
@@ -36,7 +36,7 @@ describe CheckResult, ".new" do
36
36
  context "with target_numbers, message and level" do
37
37
  before(:each) do
38
38
  @new_proc = lambda {
39
- CheckResult.new(["1", "2"], "An error occured for 1 and 2 values.", Rule::LEVEL_ERROR)
39
+ CheckResult.new(["1", "2"], "An error occured for 1 and 2 values.", Rule::ERROR_LEVELS[:error])
40
40
  }
41
41
  end
42
42
 
@@ -58,7 +58,7 @@ describe CheckResult, ".new" do
58
58
  end
59
59
 
60
60
  it "#level should be set" do
61
- @check_result.level.should == Rule::LEVEL_ERROR
61
+ @check_result.level.should == Rule::ERROR_LEVELS[:error]
62
62
  end
63
63
  end
64
64
  end
@@ -67,7 +67,7 @@ end
67
67
  describe CheckResult, "#error?" do
68
68
  context "when level is error" do
69
69
  before(:each) do
70
- @check_result = CheckResult.new("1", "message", Rule::LEVEL_ERROR)
70
+ @check_result = CheckResult.new("1", "message", Rule::ERROR_LEVELS[:error])
71
71
  end
72
72
 
73
73
  it { @check_result.should be_error }
@@ -75,7 +75,7 @@ describe CheckResult, "#error?" do
75
75
 
76
76
  context "when level is warning" do
77
77
  before(:each) do
78
- @check_result = CheckResult.new("1", "message", Rule::LEVEL_WARNING)
78
+ @check_result = CheckResult.new("1", "message", Rule::ERROR_LEVELS[:warning])
79
79
  end
80
80
 
81
81
  it { @check_result.should_not be_error }
@@ -85,7 +85,7 @@ end
85
85
  describe CheckResult, "#warning?" do
86
86
  context "when level is error" do
87
87
  before(:each) do
88
- @check_result = CheckResult.new("1", "message", Rule::LEVEL_ERROR)
88
+ @check_result = CheckResult.new("1", "message", Rule::ERROR_LEVELS[:error])
89
89
  end
90
90
 
91
91
  it { @check_result.should_not be_warning }
@@ -93,7 +93,7 @@ describe CheckResult, "#warning?" do
93
93
 
94
94
  context "when level is warning" do
95
95
  before(:each) do
96
- @check_result = CheckResult.new("1", "message", Rule::LEVEL_WARNING)
96
+ @check_result = CheckResult.new("1", "message", Rule::ERROR_LEVELS[:warning])
97
97
  end
98
98
 
99
99
  it { @check_result.should be_warning }
@@ -203,7 +203,7 @@ describe OutputSpec, "#generate" do
203
203
  @rule_set << Rule.new("3",
204
204
  Message.new("[[3::name]] Cannot be blank"),
205
205
  Validator::PresenceValidator.new("3"),
206
- nil, Rule::LEVEL_WARNING)
206
+ nil, Rule::ERROR_LEVELS[:warning])
207
207
  @rule_set << Rule.new(["3", "4"],
208
208
  Message.new("[[4::name]] Must be 'M' or 'F'"),
209
209
  Validator::InclusionValidator.new("4",
@@ -237,10 +237,10 @@ describe OutputSpec, "#generate" do
237
237
  it "should generate error list datas" do
238
238
  @output_spec.generate(@input_spec, @rule_set.results).should ==
239
239
  [
240
- ["", "", "", "", "1", "first name", "first name Cannot be blank", Rule::LEVEL_ERROR.to_s],
241
- ["", "", "", "", "2", "family name", "family name Cannot be blank", Rule::LEVEL_ERROR.to_s],
242
- ["", "", "", "", "3", "nickname", "nickname Cannot be blank", Rule::LEVEL_WARNING.to_s],
243
- ["", "", "", "", "3 4", "nickname sex", "sex Must be 'M' or 'F'", Rule::LEVEL_ERROR.to_s],
240
+ ["", "", "", "", "1", "first name", "first name Cannot be blank", Rule::ERROR_LEVELS[:error].to_s],
241
+ ["", "", "", "", "2", "family name", "family name Cannot be blank", Rule::ERROR_LEVELS[:error].to_s],
242
+ ["", "", "", "", "3", "nickname", "nickname Cannot be blank", Rule::ERROR_LEVELS[:warning].to_s],
243
+ ["", "", "", "", "3 4", "nickname sex", "sex Must be 'M' or 'F'", Rule::ERROR_LEVELS[:error].to_s],
244
244
  ]
245
245
  end
246
246
  end
@@ -53,7 +53,7 @@ describe RuleSet, "after generated" do
53
53
  @rule_set.add_rule(Rule.new("phone",
54
54
  Message.new("[[phone::name]] is invalid format."),
55
55
  Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
56
- Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING))
56
+ Validator::PresenceValidator.new("phone"), Rule::ERROR_LEVELS[:warning]))
57
57
  # mail address
58
58
  @rule_set.add_rule(Rule.new("4",
59
59
  Message.new("[[4:name]] is invalid format."),
@@ -87,7 +87,7 @@ describe RuleSet, "after generated" do
87
87
  @rule_set << Rule.new("phone",
88
88
  Message.new("[[phone::name]] is invalid format."),
89
89
  Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
90
- Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING)
90
+ Validator::PresenceValidator.new("phone"), Rule::ERROR_LEVELS[:warning])
91
91
  # mail address
92
92
  @rule_set << Rule.new("4",
93
93
  Message.new("[[4::name]] is invalid format."),
@@ -121,7 +121,7 @@ describe RuleSet, "after generated" do
121
121
  @rule_set << Rule.new("phone",
122
122
  Message.new("[[phone::name]] is invalid format."),
123
123
  Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
124
- Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING)
124
+ Validator::PresenceValidator.new("phone"), Rule::ERROR_LEVELS[:warning])
125
125
  # mail address
126
126
  @rule_set << Rule.new("4",
127
127
  Message.new("[[4::name]] is invalid format."),
@@ -165,7 +165,7 @@ describe RuleSet, "after generated" do
165
165
  @rule_set << Rule.new("phone",
166
166
  Message.new("[[phone::name]] is invalid format.([[phone::value]])"),
167
167
  Validator::FormatValidator.new("phone", :format => /^\d+-\d+-\d+$/),
168
- Validator::PresenceValidator.new("phone"), Rule::LEVEL_WARNING)
168
+ Validator::PresenceValidator.new("phone"), Rule::ERROR_LEVELS[:warning])
169
169
  # mail address
170
170
  @rule_set << Rule.new("4",
171
171
  Message.new("[[4::name]] is invalid format.([[4::value]])"),
@@ -67,8 +67,8 @@ describe Rule, "#new" do
67
67
  @rule.prerequisite.should be_nil
68
68
  end
69
69
 
70
- it "#level should equal by default(LEVEL_ERROR)" do
71
- @rule.level.should == Rule::LEVEL_ERROR
70
+ it "#level should equal by default(ERROR_LEVELS[:error])" do
71
+ @rule.level.should == Rule::ERROR_LEVELS[:error]
72
72
  end
73
73
  end
74
74
  end
@@ -81,7 +81,7 @@ describe Rule, "#new" do
81
81
  @message = Message.new("'[[1::name]]' and '[[2::name]]' cannot be blank")
82
82
  @validator = Validator::ComplexValidator.new(:validators => [v1, v2])
83
83
  @prerequisite = Validator::BlankValidator.new("3")
84
- Rule.new(["1", "2"], @message, @validator, @prerequisite, Rule::LEVEL_WARNING)
84
+ Rule.new(["1", "2"], @message, @validator, @prerequisite, Rule::ERROR_LEVELS[:warning])
85
85
  }
86
86
  end
87
87
 
@@ -111,7 +111,7 @@ describe Rule, "#new" do
111
111
  end
112
112
 
113
113
  it "#level should equal by initialize" do
114
- @rule.level.should == Rule::LEVEL_WARNING
114
+ @rule.level.should == Rule::ERROR_LEVELS[:warning]
115
115
  end
116
116
  end
117
117
  end
@@ -167,3 +167,35 @@ describe Rule, "#check" do
167
167
  it { @rule.check.should be_nil }
168
168
  end
169
169
  end
170
+
171
+ describe Rule, ".add_error_level" do
172
+ context "when new valid error level" do
173
+ before(:each) do
174
+ @proc = lambda {
175
+ Rule.add_error_level(3, :fatal, "FATAL")
176
+ }
177
+ end
178
+
179
+ after(:each) do
180
+ Rule.remove_error_level(:fatal)
181
+ end
182
+
183
+ it "should not raise error" do
184
+ lambda {
185
+ @proc.call
186
+ }.should_not raise_error
187
+ end
188
+
189
+ it "should 3 error levels" do
190
+ lambda {
191
+ @proc.call
192
+ }.should change(Rule::ERROR_LEVELS, :size).from(2).to(3)
193
+ end
194
+
195
+ it "should 3 error level names" do
196
+ lambda {
197
+ @proc.call
198
+ }.should change(Rule::ERROR_LEVEL_NAMES, :size).from(2).to(3)
199
+ end
200
+ end
201
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anodator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
9
+ - 2
10
10
  segments_generated: true
11
- version: 0.0.1
11
+ version: 0.0.2
12
12
  platform: ruby
13
13
  authors:
14
14
  - Tetsuhisa MAKINO