formkeeper 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module FormKeeper
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
data/lib/formkeeper.rb CHANGED
@@ -49,9 +49,10 @@ module FormKeeper
49
49
  @encoding = encoding
50
50
  end
51
51
  def process(value)
52
- value.force_encoding(@encoding)
53
- return value.encode('UTF-8') if value.valid_encoding?
54
- value.encode('UTF-16', :invalid => :replace, :undef => :repace, :replace => '').encode('UTF-8')
52
+ v = value.dup
53
+ v.force_encoding(@encoding)
54
+ return v.encode('UTF-8') if v.valid_encoding?
55
+ value.encode('UTF-16', :invalid => :replace, :undef => :replace, :replace => '').encode('UTF-8')
55
56
  end
56
57
  end
57
58
 
@@ -626,15 +627,20 @@ module FormKeeper
626
627
  end
627
628
  end
628
629
 
629
- attr_reader :default_filters, :fields, :checkboxes, :combinations
630
+ attr_reader :default_filters, :fields, :checkboxes, :combinations, :encoding_filter
630
631
 
631
632
  def initialize
632
633
  @default_filters = []
634
+ @encoding_filter = nil
633
635
  @fields = {}
634
636
  @checkboxes = {}
635
637
  @combinations = {}
636
638
  end
637
639
 
640
+ def encoding(code)
641
+ @encoding_filter = Filter::ToUTF8.new(code)
642
+ end
643
+
638
644
  def filters(*args)
639
645
  @default_filters = args
640
646
  end
@@ -684,10 +690,6 @@ module FormKeeper
684
690
  @@filter_store[name] = filter
685
691
  end
686
692
 
687
- def self.register_utf8_encoding_filter(name, encoding)
688
- @@filter_store[name] = Filter::ToUTF8.new(encoding)
689
- end
690
-
691
693
  def self.register_constraint(name, constraint)
692
694
  @@constraint_store[name] = constraint
693
695
  end
@@ -701,8 +703,6 @@ module FormKeeper
701
703
  register_filter :upcase, Filter::UpCase.new
702
704
  register_filter :capitalize, Filter::Capitalize.new
703
705
 
704
- register_utf8_encoding_filter :utf8, 'UTF-8'
705
-
706
706
  register_constraint :ascii, Constraint::Ascii.new
707
707
  register_constraint :ascii_space, Constraint::AsciiSpace.new
708
708
  register_constraint :regexp, Constraint::Regexp.new
@@ -730,36 +730,36 @@ module FormKeeper
730
730
  report = Report.new(messages)
731
731
  rule.fields.each do |name, criteria|
732
732
  criteria.filters.concat(rule.default_filters)
733
- report << validate_field(name, criteria, params)
733
+ report << validate_field(name, criteria, params, rule.encoding_filter)
734
734
  end
735
735
  rule.checkboxes.each do |name, criteria|
736
736
  criteria.filters.concat(rule.default_filters)
737
- report << validate_checkbox(name, criteria, params)
737
+ report << validate_checkbox(name, criteria, params, rule.encoding_filter)
738
738
  end
739
739
  rule.combinations.each do |name, criteria|
740
740
  criteria.filters.concat(rule.default_filters)
741
- report << validate_combination(name, criteria, params)
741
+ report << validate_combination(name, criteria, params, rule.encoding_filter)
742
742
  end
743
743
  return report
744
744
  end
745
745
 
746
746
  private
747
- def validate_combination(name, criteria, params)
747
+ def validate_combination(name, criteria, params, encoding)
748
748
  record = Record.new(name)
749
749
  values = criteria.fields.collect { |name| params[name.to_s] }
750
- values = filter_combination_values(values, criteria.filters)
750
+ values = filter_combination_values(values, criteria.filters, encoding)
751
751
  constraint = find_combination_constraint(criteria.constraint)
752
752
  result = constraint.validate(values, criteria.arg)
753
753
  record.fail(name) unless result
754
754
  record
755
755
  end
756
756
 
757
- def validate_checkbox(name, criteria, params)
757
+ def validate_checkbox(name, criteria, params, encoding)
758
758
  record = Record.new(name)
759
759
  if params.has_key?(name.to_s)
760
760
  values = params[name.to_s]
761
761
  if values.kind_of?(Array)
762
- values = filter_checkbox_values(values, criteria.filters)
762
+ values = filter_checkbox_values(values, criteria.filters, encoding)
763
763
  record.value = values
764
764
  if criteria.count.nil?
765
765
  if values.size == 0
@@ -791,13 +791,13 @@ module FormKeeper
791
791
  record
792
792
  end
793
793
 
794
- def filter_combination_values(values, filters)
795
- values = values.collect{ |v| filter_value(v, filters) }
794
+ def filter_combination_values(values, filters, encoding)
795
+ values = values.collect{ |v| filter_value(v, filters, encoding) }
796
796
  values
797
797
  end
798
798
 
799
- def filter_checkbox_values(values, filters)
800
- values = filter_combination_values(values, filters)
799
+ def filter_checkbox_values(values, filters, encoding)
800
+ values = filter_combination_values(values, filters, encoding)
801
801
  values.delete_if { |v| v.nil? or v.empty? }
802
802
  values
803
803
  end
@@ -810,12 +810,12 @@ module FormKeeper
810
810
  end
811
811
  end
812
812
 
813
- def validate_field(name, criteria, params)
813
+ def validate_field(name, criteria, params, encoding)
814
814
  record = Record.new name
815
815
  if params.has_key?(name.to_s)
816
816
  value = params[name.to_s]
817
817
  unless value.kind_of?(Array)
818
- value = filter_field_value(value, criteria.filters)
818
+ value = filter_field_value(value, criteria.filters, encoding)
819
819
  record.value = value
820
820
  if value.empty?
821
821
  handle_missing_field(criteria, record)
@@ -831,8 +831,8 @@ module FormKeeper
831
831
  record
832
832
  end
833
833
 
834
- def filter_field_value(value, filters)
835
- filter_value(value, filters)
834
+ def filter_field_value(value, filters, encoding)
835
+ filter_value(value, filters, encoding)
836
836
  end
837
837
 
838
838
  def handle_missing_field(criteria, record)
@@ -858,7 +858,8 @@ module FormKeeper
858
858
  @@combination_constraint_store[type]
859
859
  end
860
860
 
861
- def filter_value(value, filters)
861
+ def filter_value(value, filters, encoding)
862
+ value = encoding.process(value) unless encoding.nil?
862
863
  filters.each { |f| value = find_filter(f).process(value) }
863
864
  value
864
865
  end
@@ -10,6 +10,13 @@ describe FormKeeper::Filter::ToUTF8 do
10
10
  filter.process(value).should == "ほげほげ";
11
11
  end
12
12
 
13
+ it "handles invalid encoding value correctly" do
14
+
15
+ value = File.open(File.dirname(__FILE__) + '/asset/euc.txt') { |f| f.read.chomp }
16
+ filter = FormKeeper::Filter::ToUTF8.new('Shift_JIS')
17
+ filter.process(value).should_not == "ほげほげ";
18
+ end
19
+
13
20
  it "handles euc-jp value correctly" do
14
21
 
15
22
  value = File.open(File.dirname(__FILE__) + '/asset/euc.txt') { |f| f.read.chomp }
@@ -1,3 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  require 'spec_helper'
2
3
 
3
4
  describe FormKeeper::Validator do
@@ -26,6 +27,61 @@ describe FormKeeper::Validator do
26
27
 
27
28
  end
28
29
 
30
+ it "validates valid utf-8 encoding according to rule" do
31
+
32
+ rule = FormKeeper::Rule.new
33
+ rule.encoding 'UTF-8'
34
+ rule.filters :strip
35
+ rule.field :username, :present => true, :length => 8..16
36
+ rule.field :password, :present => true, :length => 8..16
37
+ rule.field :nickname, :length => 4..8
38
+
39
+ value = File.open(File.dirname(__FILE__) + '/asset/utf8.txt') { |f| f.read.chomp }
40
+
41
+ params = {}
42
+ params['username'] = ' hogehogefoo'
43
+ params['password'] = 'hogehogebar '
44
+ params['nickname'] = value
45
+
46
+ validator = FormKeeper::Validator.new
47
+ report = validator.validate(params, rule)
48
+
49
+ report.failed?.should_not be_true
50
+
51
+ report[:username].should == 'hogehogefoo'
52
+ report[:password].should == 'hogehogebar'
53
+ report[:nickname].should == 'ほげほげ'
54
+
55
+ end
56
+
57
+ it "validates valid non utf-8 encoding according to rule" do
58
+
59
+ rule = FormKeeper::Rule.new
60
+ rule.encoding 'EUC-JP'
61
+ rule.filters :strip
62
+ rule.field :username, :present => true, :length => 8..16
63
+ rule.field :password, :present => true, :length => 8..16
64
+ rule.field :nickname, :length => 4..8
65
+
66
+ value = File.open(File.dirname(__FILE__) + '/asset/euc.txt') { |f| f.read.chomp }
67
+
68
+ params = {}
69
+ params['username'] = ' hogehogefoo'
70
+ params['password'] = 'hogehogebar '
71
+ params['nickname'] = value
72
+
73
+ validator = FormKeeper::Validator.new
74
+ report = validator.validate(params, rule)
75
+
76
+ report.failed?.should_not be_true
77
+
78
+ report[:username].should == 'hogehogefoo'
79
+ report[:password].should == 'hogehogebar'
80
+ report[:nickname].should == 'ほげほげ'
81
+
82
+ end
83
+
84
+
29
85
  it "validates unpresent params according to rule" do
30
86
 
31
87
  rule = FormKeeper::Rule.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formkeeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -74,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
74
  version: '0'
75
75
  segments:
76
76
  - 0
77
- hash: -2542690134374013236
77
+ hash: 2207335481153174056
78
78
  required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  none: false
80
80
  requirements:
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  segments:
85
85
  - 0
86
- hash: -2542690134374013236
86
+ hash: 2207335481153174056
87
87
  requirements: []
88
88
  rubyforge_project:
89
89
  rubygems_version: 1.8.24