chilean-rutify 0.1.3 → 1.1.0

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: 5b63da664877c8905b8c146816577ffe3f69b818db5e6b91658db3f426ec4d48
4
- data.tar.gz: 6a73c231171848f1b1ce297fe37aa994eb5fca90cd7a6b383144118d9b2e1302
3
+ metadata.gz: dae494bb8c26db7167d95a6ce08008ec3a7f915068b38f675477846ef3a17a0b
4
+ data.tar.gz: 7d0bdb3e436e61f575749cc4effde5cd259673715fd967652b658e5760c78e92
5
5
  SHA512:
6
- metadata.gz: 9a5c582617c794b05a43dd322a11c620357188699bb6bec0021e26f8f7b9081eacc5ead75e70f391e6969e83cf61d0ec192978b74e0ed9e9b0a51340a779f4ad
7
- data.tar.gz: eb9b7cf3c0eddad4973f6a5f7f9823b45381e682129af7b9bda6cb27aa38a85ae41d5e9047b18bc8c808922fea23133b052ad391e6fc42957063fb0d3f3d13a0
6
+ metadata.gz: 419f8612722b649dd267b99c892ae392c9d6d131a376caf26dc453919aae42d4c5944cce46b39b8928e636838e971dc73d2d42a6eeb60df227253ba7359b800d
7
+ data.tar.gz: 44cb02fcf491453df11419a07678e073be40a9dd588b614e34ee28acc8c67445a508427ea29799acbe8f9ecae11ea64b58f2df8bdd32de4ccbadc984ec9e4a6d
@@ -0,0 +1,22 @@
1
+ name: reviewdog
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ jobs:
7
+ rubocop:
8
+ name: rubocop
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - name: Check out code
12
+ uses: actions/checkout@v1
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.7.4
16
+ - name: rubocop
17
+ uses: reviewdog/action-rubocop@v1
18
+ with:
19
+ rubocop_version: gemfile
20
+ rubocop_extensions: rubocop-rails:gemfile rubocop-rspec:gemfile rubocop-performance:gemfile
21
+ github_token: ${{ secrets.github_token }}
22
+ reporter: github-check
@@ -0,0 +1,21 @@
1
+ name: rspec
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ jobs:
7
+ rspec:
8
+ name: rspec
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - name: Check out code
12
+ uses: actions/checkout@v1
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.7.4
16
+ - name: Install bundler
17
+ run: |
18
+ gem install bundler -v 2.4.22
19
+ bundle install --retry=2
20
+ - name: Run rspec
21
+ run: bundle exec rspec
data/.rubocop.yml CHANGED
@@ -8,3 +8,10 @@ Style/StringLiteralsInInterpolation:
8
8
 
9
9
  Layout/LineLength:
10
10
  Max: 120
11
+
12
+ Metrics/BlockLength:
13
+ IgnoredMethods: ['describe', 'context']
14
+
15
+ AllCops:
16
+ NewCops: enable
17
+ TargetRubyVersion: 2.5
data/Gemfile CHANGED
@@ -2,9 +2,13 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
+ gem "activerecord', '~> 6.0"
6
+ gem "activesupport', '~> 6.0"
7
+ gem "rake', '~> 13.0"
8
+ gem "rspec', '~> 3.6"
9
+ gem "rubocop', '~> 1.0"
10
+ gem "rubocop-performance', '~> 1.11.4"
11
+ gem "rubocop-rails', '~> 2.11.3"
12
+ gem "rubocop-rspec', '~> 2.4.0"
5
13
  # Specify your gem's dependencies in chilean-rutify.gemspec
6
14
  gemspec
7
-
8
- gem "rake", "~> 13.0"
9
- gem "rspec", "~> 3.0"
10
- gem "rubocop", "~> 0.80"
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Chilean::Rutify
2
2
 
3
+ [![reviewdog](https://github.com/mihailpozarski/chilean-rutify/workflows/reviewdog/badge.svg?branch=main&event=push)](https://github.com/mihailpozarski/chilean-rutify/actions?query=workflow%3Areviewdog+event%3Apush+branch%3Amain)
4
+ [![rspec](https://github.com/mihailpozarski/chilean-rutify/workflows/rspec/badge.svg?branch=main&event=push)](https://github.com/mihailpozarski/chilean-rutify/actions?query=workflow%3Arspec+event%3Apush+branch%3Amain)
5
+
3
6
  Chilean Rutify is a Ruby on Rails gem that makes rut validations and manipulation easy
4
7
 
5
8
  ## Installation
@@ -27,12 +30,18 @@ You can use the available utility methods directly form the module:
27
30
  rut = '36408368-8'
28
31
  Chilean::Rutify.normalize_rut(rut)
29
32
  # => "364083688"
30
- Chilean::Rutify.format_rut(rut)
31
- # => ""36.408.368-8"
33
+ Chilean::Rutify.format_rut(rut) # alias Chilean::Rutify.classic_rut(rut)
34
+ # => "36.408.368-8"
35
+ Chilean::Rutify.format_rut(rut, :dash_only) # alias Chilean::Rutify.dash_only_rut(rut)
36
+ # => "36408368-8"
37
+ Chilean::Rutify.format_rut(rut, :normalized) # alias Chilean::Rutify.normalize_rut(rut)
38
+ # => "364083688"
32
39
  Chilean::Rutify.valid_rut?(rut)
33
40
  # => true
34
41
  Chilean::Rutify.valid_rut_verifier?(rut)
35
42
  # => true
43
+ Chilean::Rutify.valid_rut_values?(rut)
44
+ # => true
36
45
  rut = "36408368"
37
46
  Chilean::Rutify.get_verifier(rut)
38
47
  # => "8"
@@ -54,7 +63,7 @@ Or include the new Rutifiable concern (it asumes you created a `rut` column):
54
63
  ```ruby
55
64
  class Person < ActiveRecord::Base
56
65
  includes Chilean::Rutifiable
57
- self.rut_format = :classic #(optional) options: [:classic, :normalized, :nil] (nil = no override, default: :classic)
66
+ self.rut_format = :classic #(optional) options: [:classic, :normalized, :dash_only, :nil] (nil = no override, default: :classic)
58
67
  end
59
68
  ```
60
69
 
data/bin/console CHANGED
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "bundler/setup"
5
+ require "active_model"
5
6
  require "chilean/rutify"
6
7
 
7
8
  # You can add fixtures and/or initialization code here to make experimenting
@@ -12,10 +12,11 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "A useful chilean rut validator and formater for Rails"
13
13
  spec.homepage = "https://github.com/mihailpozarski/chilean-rutify"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
18
  spec.metadata["source_code_uri"] = "https://github.com/mihailpozarski/chilean-rutify"
19
+ spec.metadata["rubygems_mfa_required"] = "true"
19
20
 
20
21
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
22
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
@@ -23,8 +24,4 @@ Gem::Specification.new do |spec|
23
24
  spec.bindir = "exe"
24
25
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
26
  spec.require_paths = ["lib"]
26
-
27
- spec.add_development_dependency "activerecord", "~> 6.0"
28
- spec.add_development_dependency "active_support", "~> 6.0"
29
- spec.add_development_dependency "rspec", "~> 3.6"
30
27
  end
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Error Class for gem errors
1
4
  module Chilean
2
5
  module Rutify
3
6
  class Error < StandardError; end
@@ -1,12 +1,11 @@
1
- ##
2
- # Validates that the given string has the correct rut syntax
1
+ # frozen_string_literal: true
3
2
 
3
+ # Validates that the given string has the correct rut syntax
4
4
  class RutValidator < ActiveModel::EachValidator
5
5
  def validate_each(record, attribute, value)
6
- if value.nil? || !value.is_a?(String)
7
- record.errors.add attribute, (options[:message] || "is a empty rut")
8
- elsif !Chilean::Rutify.valid_rut?(value)
9
- record.errors.add attribute, (options[:message] || "is not a valid rut")
10
- end
6
+ return if value.blank?
7
+ return if Chilean::Rutify.valid_rut?(value)
8
+
9
+ record.errors.add attribute, (options[:message] || "is not a valid rut")
11
10
  end
12
11
  end
@@ -5,7 +5,7 @@ module Chilean
5
5
  #
6
6
  # Options:
7
7
  #
8
- # - :rut_format - The rut setter format, can be [:classic, :normalized, :nil], defaults to `:classic`.
8
+ # - :rut_format - The rut setter format, can be [:classic, :normalized, :dash_only, :nil], defaults to `:classic`.
9
9
  module Rutifiable
10
10
  # TODO: Make Rspec tests
11
11
  extend ActiveSupport::Concern
@@ -20,8 +20,9 @@ module Chilean
20
20
 
21
21
  # Override rails setter for rut column
22
22
  def rut=(value)
23
- value = Chilean::Rutify.format_rut(value) if rut_format == :classic
23
+ value = Chilean::Rutify.classic_rut(value) if rut_format == :classic
24
24
  value = Chilean::Rutify.normalize_rut(value) if rut_format == :normalized
25
+ value = Chilean::Rutify.dash_only_rut(value) if rut_format == :dash_only
25
26
  super(value)
26
27
  end
27
28
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Chilean
4
4
  module Rutify
5
- VERSION = "0.1.3"
5
+ VERSION = "1.1.0"
6
6
  end
7
7
  end
@@ -9,8 +9,8 @@ module Chilean
9
9
  # Chilean rut utils module
10
10
  module Rutify
11
11
  # checks if the passed value is valid as a rut character
12
- def valid_rut_value?(rv)
13
- ['0','1','2','3','4','5','6','7','8','9','k','K','.','-'].include?(rv.to_s)
12
+ def valid_rut_value?(value)
13
+ ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "k", "K", ".", "-"].include?(value.to_s)
14
14
  end
15
15
 
16
16
  # returns the rut verifier value
@@ -21,20 +21,25 @@ module Chilean
21
21
  sum = 0
22
22
  mul = 2
23
23
 
24
- rut.reverse.split("").each do |i|
24
+ rut.reverse.chars.each do |i|
25
25
  sum += i.to_i * mul
26
26
  mul = mul == 7 ? 2 : mul + 1
27
27
  end
28
28
 
29
29
  res = sum % 11
30
30
 
31
- case res
31
+ translate_verifier_result(res)
32
+ end
33
+
34
+ # translate the result of the rut calculations
35
+ def translate_verifier_result(result)
36
+ case result
32
37
  when 1
33
38
  "K"
34
39
  when 0
35
40
  "0"
36
41
  else
37
- (11 - res).to_s
42
+ (11 - result).to_s
38
43
  end
39
44
  end
40
45
 
@@ -57,8 +62,16 @@ module Chilean
57
62
  rut.upcase
58
63
  end
59
64
 
65
+ # returns the rut value with only the dash
66
+ def dash_only_rut(raw_rut)
67
+ rut = normalize_rut(raw_rut)
68
+ return unless rut.present? && valid_rut_values?(rut)
69
+
70
+ "#{rut[0..-2]}-#{rut[-1]}"
71
+ end
72
+
60
73
  # returns the rut value with the chilean format
61
- def format_rut(raw_rut)
74
+ def classic_rut(raw_rut)
62
75
  rut = normalize_rut(raw_rut)
63
76
  return unless rut.present? && valid_rut_values?(rut)
64
77
 
@@ -67,12 +80,26 @@ module Chilean
67
80
  init_rut = ""
68
81
 
69
82
  while temp_rut.length > 3
70
- init_rut = "." + temp_rut[(temp_rut.size - 3)..temp_rut.size] + init_rut
83
+ init_rut = ".#{temp_rut[(temp_rut.size - 3)..temp_rut.size]}#{init_rut}"
71
84
  temp_rut = temp_rut[0..-4]
72
85
  end
73
86
 
74
- rut = temp_rut + init_rut + "-" + verifier
75
- rut.upcase
87
+ "#{temp_rut}#{init_rut}-#{verifier}".upcase
88
+ end
89
+
90
+ # returns the rut value with the specified format (default is classic )
91
+ def format_rut(raw_rut, format = :classic)
92
+ rut = stringify_rut(raw_rut)
93
+ return unless rut.present? && valid_rut_values?(rut)
94
+
95
+ case format
96
+ when :normalized
97
+ normalize_rut(rut)
98
+ when :dash_only
99
+ dash_only_rut(rut)
100
+ when :classic
101
+ classic_rut(rut)
102
+ end
76
103
  end
77
104
 
78
105
  # checks if the passed rut is valid
@@ -88,7 +115,7 @@ module Chilean
88
115
  rut = stringify_rut(raw_rut)
89
116
  return false unless rut.present?
90
117
 
91
- rut.split("").each do |i|
118
+ rut.chars.each do |i|
92
119
  return false unless valid_rut_value?(i)
93
120
  end
94
121
 
@@ -103,6 +130,6 @@ module Chilean
103
130
  end
104
131
 
105
132
  module_function :normalize_rut, :format_rut, :valid_rut_value?, :get_verifier, :valid_rut?, :valid_rut_verifier?,
106
- :valid_rut_values?, :stringify_rut
133
+ :valid_rut_values?, :stringify_rut, :translate_verifier_result, :dash_only_rut, :classic_rut
107
134
  end
108
135
  end
metadata CHANGED
@@ -1,57 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chilean-rutify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.1.0
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-04-28 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activerecord
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '6.0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
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'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.6'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '3.6'
11
+ date: 2024-02-22 00:00:00.000000000 Z
12
+ dependencies: []
55
13
  description: A useful chilean rut validator and formater for Rails
56
14
  email:
57
15
  - mihailpozarski@outlook.com
@@ -61,6 +19,8 @@ extra_rdoc_files: []
61
19
  files:
62
20
  - ".github/ISSUE_TEMPLATE/bug_report.md"
63
21
  - ".github/ISSUE_TEMPLATE/feature_request.md"
22
+ - ".github/workflows/reviewdog.yml"
23
+ - ".github/workflows/rspec.yml"
64
24
  - ".gitignore"
65
25
  - ".rspec"
66
26
  - ".rubocop.yml"
@@ -84,6 +44,7 @@ licenses:
84
44
  metadata:
85
45
  homepage_uri: https://github.com/mihailpozarski/chilean-rutify
86
46
  source_code_uri: https://github.com/mihailpozarski/chilean-rutify
47
+ rubygems_mfa_required: 'true'
87
48
  post_install_message:
88
49
  rdoc_options: []
89
50
  require_paths:
@@ -92,14 +53,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
53
  requirements:
93
54
  - - ">="
94
55
  - !ruby/object:Gem::Version
95
- version: 2.4.0
56
+ version: 2.5.0
96
57
  required_rubygems_version: !ruby/object:Gem::Requirement
97
58
  requirements:
98
59
  - - ">="
99
60
  - !ruby/object:Gem::Version
100
61
  version: '0'
101
62
  requirements: []
102
- rubygems_version: 3.2.3
63
+ rubygems_version: 3.1.4
103
64
  signing_key:
104
65
  specification_version: 4
105
66
  summary: Rails chilean rut validator formater