active_comparison_validator 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 555696c870e739aab3bf4157be8d60fe0d6b058c
4
- data.tar.gz: 9ab7a249371e28f1be8183463ce9895de0c14461
3
+ metadata.gz: a59f4a00d296945bf26f7e4ee8b191a4731c5884
4
+ data.tar.gz: 471bf88a1db0919b8d6eda3b0dc82c731763e345
5
5
  SHA512:
6
- metadata.gz: a1df0185130513ee3f518ce8ac6c4e06ad2832ed2c2678668ff84d57bb2d87ad3d512fd914c9aa5afdcc026c965b979f4e1f483830f86ce9c288ce72c9db95d1
7
- data.tar.gz: 611ad287aa04ce18339bb4ae89e3f8ee9a268e5b7d912bbfc1d8f1ab766acad1eacde027f2df4a78480694f9691b1381163add564154211b92d326b03033ab46
6
+ metadata.gz: 0076fb69858daf2157634bd2f26b1e64329b4a213246a2597099bfeaa3b1444ecb445bbbe8e85f41f3b527898a4eaea7a7106abf63a1be15379eb0a4cf1b55f8
7
+ data.tar.gz: 5cfb7d9ef64fbea5324150e5e141a151cba437ed48431797404b21b8f745fa9fc0b5559b61e4514609761524fbfee1d9ff59b74674ac481918183b0eaca37349
@@ -0,0 +1,16 @@
1
+ machine:
2
+ timezone:
3
+ Asia/Tokyo
4
+
5
+ # Version of ruby to use
6
+ ruby:
7
+ version:
8
+ 2.2.3
9
+
10
+ dependencies:
11
+ pre:
12
+ - gem install bundler
13
+
14
+ override:
15
+ - bundle install:
16
+ timeout: 180
@@ -1,66 +1,12 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'active_comparison_validator/version.rb'
3
+ require 'i18n'
3
4
  require 'active_record'
4
5
  require 'active_support/concern'
5
6
  require 'active_support/core_ext/object/with_options'
6
7
  require 'active_support/core_ext/array/access'
7
- require 'i18n'
8
+ require 'active_comparison_validator/class_methods.rb'
8
9
 
9
10
  module ActiveComparisonValidator
10
11
  extend ActiveSupport::Concern
11
- module ClassMethods
12
- # Verified: that A is greater than B.
13
- # @param [String] field_a_<_field_b This string is field_name, operator_name and field_name
14
- # @return Define a custom validator in the context of the model.
15
- # @example open_at < close_at
16
- # class Shop < ActiveRecord::Base
17
- # include OriginValidator
18
- # comparison_validator 'open_at < close_at'
19
- # end
20
- # @note
21
- # You can use their operator.
22
- # - '<'
23
- # - '<='
24
- # - '>'
25
- # - '>='
26
- # - '=='
27
- # - '!='
28
- # @note
29
- # And localization.
30
- # - Dedault is used errors.messages
31
- # - greater_than
32
- # - less_than
33
- # - greater_than_or_equal_to
34
- # - less_than_or_equal_to
35
- # - confirmation
36
- # - other_than
37
- def comparison_validator(a_operator_b)
38
- a_attr, operator, b_attr = *a_operator_b.split(/\s/).map(&:to_sym)
39
- method_name = "comparison_validator_for_#{a_attr}_and_#{b_attr}"
40
- define_method(method_name) do
41
- a_value = send(a_attr)
42
- to_value = send(b_attr)
43
- return unless a_value && to_value
44
- locals = {
45
- :< => [:greater_than, :less_than, :count],
46
- :<= => [:greater_than_or_equal_to, :less_than_or_equal_to, :count],
47
- :> => [:less_than, :greater_than, :count],
48
- :>= => [:less_than_or_equal_to, :greater_than_or_equal_to, :count],
49
- :== => [:confirmation, :confirmation, :attribute],
50
- :!= => [:other_than, :other_than, :attribute]
51
- }
52
- return unless locals.key?(operator)
53
- return if a_value.send(operator, to_value)
54
- a_value_human = { locals[operator].last => self.class.human_attribute_name(a_attr) }
55
- b_value_human = { locals[operator].last => self.class.human_attribute_name(b_attr) }
56
-
57
- I18n.with_options scope: 'errors.messages' do |locale|
58
- errors.add(b_attr, locale.t(locals[operator].first, a_value_human))
59
- errors.add(a_attr, locale.t(locals[operator].second, b_value_human))
60
- end
61
- end
62
- config = %(validate :#{method_name})
63
- class_eval(config)
64
- end
65
- end
66
12
  end
@@ -0,0 +1,57 @@
1
+ module ActiveComparisonValidator
2
+ module ClassMethods
3
+ # Verified: that A is greater than B.
4
+ # @param [String] field_a_<_field_b This string is field_name, operator_name and field_name
5
+ # @return Define a custom validator in the context of the model.
6
+ # @example open_at < close_at
7
+ # class Shop < ActiveRecord::Base
8
+ # include OriginValidator
9
+ # comparison_validator 'open_at < close_at'
10
+ # end
11
+ # @note
12
+ # You can use their operator.
13
+ # - '<'
14
+ # - '<='
15
+ # - '>'
16
+ # - '>='
17
+ # - '=='
18
+ # - '!='
19
+ # @note
20
+ # And localization.
21
+ # - Dedault is used errors.messages
22
+ # - greater_than
23
+ # - less_than
24
+ # - greater_than_or_equal_to
25
+ # - less_than_or_equal_to
26
+ # - confirmation
27
+ # - other_than
28
+ def comparison_validator(a_operator_b)
29
+ a_attr, operator, b_attr = *a_operator_b.split(/\s/).map(&:to_sym)
30
+ method_name = "comparison_validator_for_#{a_attr}_and_#{b_attr}"
31
+ define_method(method_name) do
32
+ a_value = send(a_attr)
33
+ to_value = send(b_attr)
34
+ return unless a_value && to_value
35
+ locals = {
36
+ :< => [:greater_than, :less_than, :count],
37
+ :<= => [:greater_than_or_equal_to, :less_than_or_equal_to, :count],
38
+ :> => [:less_than, :greater_than, :count],
39
+ :>= => [:less_than_or_equal_to, :greater_than_or_equal_to, :count],
40
+ :== => [:confirmation, :confirmation, :attribute],
41
+ :!= => [:other_than, :other_than, :attribute]
42
+ }
43
+ return unless locals.key?(operator)
44
+ return if a_value.send(operator, to_value)
45
+ a_value_human = { locals[operator].last => self.class.human_attribute_name(a_attr) }
46
+ b_value_human = { locals[operator].last => self.class.human_attribute_name(b_attr) }
47
+
48
+ I18n.with_options scope: 'errors.messages' do |locale|
49
+ errors.add(b_attr, locale.t(locals[operator].first, a_value_human))
50
+ errors.add(a_attr, locale.t(locals[operator].second, b_value_human))
51
+ end
52
+ end
53
+ config = %(validate :#{method_name})
54
+ class_eval(config)
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveComparisonValidator
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_comparison_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - onodera
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-24 00:00:00.000000000 Z
11
+ date: 2016-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -128,7 +128,9 @@ files:
128
128
  - active_comparison_validator.gemspec
129
129
  - bin/console
130
130
  - bin/setup
131
+ - circle.yml
131
132
  - lib/active_comparison_validator.rb
133
+ - lib/active_comparison_validator/class_methods.rb
132
134
  - lib/active_comparison_validator/version.rb
133
135
  homepage: https://github.com/s1160054/active_comparison_validator
134
136
  licenses: