chilean-rutify 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
  SHA256:
3
- metadata.gz: 60d3949c205f93cdaf3618c7271177764f78dbd5097c25b72a2c055bd65deb0e
4
- data.tar.gz: e80a9caa926f3430ea758fb29d48e86cf78948538059eda187039d4c62586ee6
3
+ metadata.gz: 5b63da664877c8905b8c146816577ffe3f69b818db5e6b91658db3f426ec4d48
4
+ data.tar.gz: 6a73c231171848f1b1ce297fe37aa994eb5fca90cd7a6b383144118d9b2e1302
5
5
  SHA512:
6
- metadata.gz: 9eb7cf105967447830e4a37c456e77ea1ed9ca2cccf6a0394258429f9a8b57e38f552eaf0f37b5bc1e9a41d6eecbc8fad7951cb2ecc8c066cac87d405bdbee0e
7
- data.tar.gz: 8cb92baef56379fd575909252e90834ad6d4f715e18897acec8dd69215ccc9ba38a5164a4284c7768ae04c2a5e99c0d42e6f108be1065ddc6d640329b7b39728
6
+ metadata.gz: 9a5c582617c794b05a43dd322a11c620357188699bb6bec0021e26f8f7b9081eacc5ead75e70f391e6969e83cf61d0ec192978b74e0ed9e9b0a51340a779f4ad
7
+ data.tar.gz: eb9b7cf3c0eddad4973f6a5f7f9823b45381e682129af7b9bda6cb27aa38a85ae41d5e9047b18bc8c808922fea23133b052ad391e6fc42957063fb0d3f3d13a0
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: "[BUG]"
5
+ labels: bug
6
+ assignees: mihailpozarski
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: "[Feature]"
5
+ labels: enhancement
6
+ assignees: mihailpozarski
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Chilean::Rutify
2
2
 
3
- Chilean Rutify makes rut validations and manipulation easy
3
+ Chilean Rutify is a Ruby on Rails gem that makes rut validations and manipulation easy
4
4
 
5
5
  ## Installation
6
6
 
@@ -38,6 +38,8 @@ You can use the available utility methods directly form the module:
38
38
  # => "8"
39
39
  ```
40
40
 
41
+ ### Validator
42
+
41
43
  You can also use the available validator as any other custom Rails validator:
42
44
 
43
45
  ```ruby
@@ -46,6 +48,29 @@ class Person < ActiveRecord::Base
46
48
  end
47
49
  ```
48
50
 
51
+ ### Concern
52
+ Or include the new Rutifiable concern (it asumes you created a `rut` column):
53
+
54
+ ```ruby
55
+ class Person < ActiveRecord::Base
56
+ includes Chilean::Rutifiable
57
+ self.rut_format = :classic #(optional) options: [:classic, :normalized, :nil] (nil = no override, default: :classic)
58
+ end
59
+ ```
60
+
61
+ the last one is equivalent to:
62
+
63
+ ```ruby
64
+ class Person < ActiveRecord::Base
65
+ validates :rut, presence: true, uniqueness: { case_sensitive: false }, rut: true
66
+
67
+ def rut=(value)
68
+ value = Chilean::Rutify.format_rut(value)
69
+ super(value)
70
+ end
71
+ end
72
+ ```
73
+
49
74
  ## Development
50
75
 
51
76
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-minimal
@@ -25,5 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.require_paths = ["lib"]
26
26
 
27
27
  spec.add_development_dependency "activerecord", "~> 6.0"
28
+ spec.add_development_dependency "active_support", "~> 6.0"
28
29
  spec.add_development_dependency "rspec", "~> 3.6"
29
30
  end
@@ -3,21 +3,25 @@
3
3
  require_relative "rutify/version"
4
4
  require_relative "rutify/rut_validator"
5
5
  require_relative "rutify/errors"
6
+ require_relative "rutify/rutifiable"
6
7
 
7
8
  module Chilean
8
9
  # Chilean rut utils module
9
10
  module Rutify
10
11
  # checks if the passed value is valid as a rut character
11
12
  def valid_rut_value?(rv)
12
- ['0','1','2','3','4','5','6','7','8','9','k','K'].include?(rv)
13
+ ['0','1','2','3','4','5','6','7','8','9','k','K','.','-'].include?(rv.to_s)
13
14
  end
14
15
 
15
16
  # returns the rut verifier value
16
- def get_verifier(rut)
17
+ def get_verifier(raw_rut)
18
+ rut = stringify_rut(raw_rut)
19
+ return unless rut.present? && valid_rut_values?(rut)
20
+
17
21
  sum = 0
18
22
  mul = 2
19
23
 
20
- rut.to_s.reverse.split("").each do |i|
24
+ rut.reverse.split("").each do |i|
21
25
  sum += i.to_i * mul
22
26
  mul = mul == 7 ? 2 : mul + 1
23
27
  end
@@ -37,15 +41,16 @@ module Chilean
37
41
  # checks if the passed rut has a proper verifier value
38
42
  def valid_rut_verifier?(raw_rut)
39
43
  rut = normalize_rut(raw_rut)
40
- return false if rut.empty? || rut.size < 2 || !rut.is_a?(String)
44
+ return false unless rut.present? && valid_rut_values?(rut)
41
45
 
42
46
  r = rut[0..(rut.size - 2)]
43
47
  get_verifier(r) == rut[-1]
44
48
  end
45
49
 
46
50
  # returns the rut value without any sepecial caracter and upcased
47
- def normalize_rut(rut)
48
- return if rut.nil? || !rut.is_a?(String)
51
+ def normalize_rut(raw_rut)
52
+ rut = stringify_rut(raw_rut)
53
+ return unless rut.present? && valid_rut_values?(rut)
49
54
 
50
55
  rut = rut.delete "."
51
56
  rut = rut.delete "-"
@@ -55,7 +60,7 @@ module Chilean
55
60
  # returns the rut value with the chilean format
56
61
  def format_rut(raw_rut)
57
62
  rut = normalize_rut(raw_rut)
58
- return if rut.nil? || rut.empty? || rut.size < 2
63
+ return unless rut.present? && valid_rut_values?(rut)
59
64
 
60
65
  verifier = rut[-1]
61
66
  temp_rut = rut[0..-2]
@@ -73,15 +78,31 @@ module Chilean
73
78
  # checks if the passed rut is valid
74
79
  def valid_rut?(raw_rut)
75
80
  rut = normalize_rut(raw_rut)
76
- return false if rut.nil? || rut.empty? || rut.size < 2
81
+ return false unless rut.present? && valid_rut_values?(rut)
82
+
83
+ valid_rut_verifier?(rut)
84
+ end
85
+
86
+ # checks if all the rut values are valid
87
+ def valid_rut_values?(raw_rut)
88
+ rut = stringify_rut(raw_rut)
89
+ return false unless rut.present?
77
90
 
78
91
  rut.split("").each do |i|
79
92
  return false unless valid_rut_value?(i)
80
93
  end
81
94
 
82
- valid_rut_verifier?(rut)
95
+ true
96
+ end
97
+
98
+ # checks rut minimum requirements and transform value to string if posible
99
+ def stringify_rut(raw_rut)
100
+ return if raw_rut.nil? || (!raw_rut.is_a?(String) && !raw_rut.is_a?(Integer))
101
+
102
+ raw_rut.to_s
83
103
  end
84
104
 
85
- module_function :normalize_rut, :format_rut, :valid_rut_value?, :get_verifier, :valid_rut?, :valid_rut_verifier?
105
+ module_function :normalize_rut, :format_rut, :valid_rut_value?, :get_verifier, :valid_rut?, :valid_rut_verifier?,
106
+ :valid_rut_values?, :stringify_rut
86
107
  end
87
108
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chilean
4
+ # Handles rut validation and formating on models (asumes rut column on model)
5
+ #
6
+ # Options:
7
+ #
8
+ # - :rut_format - The rut setter format, can be [:classic, :normalized, :nil], defaults to `:classic`.
9
+ module Rutifiable
10
+ # TODO: Make Rspec tests
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ class_attribute :rut_format
15
+ self.rut_format = :classic
16
+
17
+ # Validations
18
+ validates :rut, presence: true, uniqueness: { case_sensitive: false }, rut: true
19
+ end
20
+
21
+ # Override rails setter for rut column
22
+ def rut=(value)
23
+ value = Chilean::Rutify.format_rut(value) if rut_format == :classic
24
+ value = Chilean::Rutify.normalize_rut(value) if rut_format == :normalized
25
+ super(value)
26
+ end
27
+ end
28
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Chilean
4
4
  module Rutify
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chilean-rutify
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
  - Mihail Pozarski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-10 00:00:00.000000000 Z
11
+ date: 2021-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_support
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -45,6 +59,8 @@ executables: []
45
59
  extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
62
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
63
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
48
64
  - ".gitignore"
49
65
  - ".rspec"
50
66
  - ".rubocop.yml"
@@ -53,12 +69,14 @@ files:
53
69
  - LICENSE.txt
54
70
  - README.md
55
71
  - Rakefile
72
+ - _config.yml
56
73
  - bin/console
57
74
  - bin/setup
58
75
  - chilean-rutify.gemspec
59
76
  - lib/chilean/rutify.rb
60
77
  - lib/chilean/rutify/errors.rb
61
78
  - lib/chilean/rutify/rut_validator.rb
79
+ - lib/chilean/rutify/rutifiable.rb
62
80
  - lib/chilean/rutify/version.rb
63
81
  homepage: https://github.com/mihailpozarski/chilean-rutify
64
82
  licenses: