rutcl 0.0.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTYxODdjYzJiNjcyM2YzZDEwNzBkZTgyZDg3NTg1MWU2NWM1ZTBhMw==
5
+ data.tar.gz: !binary |-
6
+ ZjI5MmQzYWI4MWVlMmQ2OTNkNWE4NzZkYjBlYjVjODQ5OTliZGQ1Zg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZDk3ZjQ4ODVkY2FmOGVkZDJiYzUzZDE2ZDhlODM0ZmZmY2Y0OThlM2NkZTUz
10
+ MGNmMDVjNzhkMzg3YzQxZDMxN2Y3ZmJmMzY4YTMxZjNhM2I1OGY4ODY0Mzcy
11
+ NGY1Yjg5MTg4ZDUxZmM2NTExMDBjNWY5OGUyNzZjNGEwZGExZTU=
12
+ data.tar.gz: !binary |-
13
+ YWM1MGEwYzc4NGVlNzM4MTNkZGFiMTdiMWVmMDdhYTkzNjQ0NjM1ZmE1Mzc3
14
+ ZjdjYmI5MDlmYzE0N2FmZWI3ZTQ3OTZjZGRhNzc3NjE2ZDJjYjY1NmU4MDM0
15
+ YjhlNzA1NDMxZTVjNzA3N2RmNmRjNjYyOTg4YWVlYzZhMzdiNGU=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p448@rut"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.23.8 (stable)" # 1.10.1 seems like a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 1 )) # display automatically
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
37
+ " # show the user the ruby and gemset they are using in green
38
+ else printf "%b" "Using: $GEM_HOME
39
+ " # don't use colors in non-interactive shells
40
+ fi
41
+ fi
42
+ else
43
+ # If the environment file has not yet been created, use the RVM CLI to select.
44
+ rvm --create use "$environment_id" || {
45
+ echo "Failed to create RVM environment '${environment_id}'."
46
+ return 1
47
+ }
48
+ fi
49
+
50
+ # If you use bundler, this might be useful to you:
51
+ # if [[ -s Gemfile ]] && {
52
+ # ! builtin command -v bundle >/dev/null ||
53
+ # builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
54
+ # }
55
+ # then
56
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
57
+ # gem install bundler
58
+ # fi
59
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
60
+ # then
61
+ # bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
62
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rut.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Pablo Marambio
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Rut
2
+
3
+ Valida y formatea RUTs chilenos.
4
+
5
+ ## Instalación
6
+
7
+ Incluye esta línea en tu Gemfile
8
+
9
+ ```ruby
10
+ gem 'rut'
11
+ ```
12
+
13
+ Y luego ejecuta `bundle` en la carpeta de tu proyecto.
14
+
15
+ ## Uso
16
+
17
+ La gema habilita la clase Rut, la que cuenta con tres métodos:
18
+
19
+ - `valid?(rut)` retorna `true` si el rut es válido.
20
+ - `dv(rut)` retorna el dígito verificador del rut. El argumento debe ser un rut **sin dígito verificador**.
21
+ - `pretty(rut, validar=true)` formatea el rut recibido como argumento.
22
+
23
+ ## Ejemplos
24
+
25
+ ```ruby
26
+ # Validación
27
+ Rut.valid?("16365637k") #rut válido 16.365.637-k
28
+ #=> true
29
+ Rut.valid?("163656370") #rut inválido 16.365.637-0
30
+ #=> false
31
+
32
+ # Cómputo de dígito verificador
33
+ Rut.dv(1) #rut 1-9
34
+ #=> 9
35
+ Rut.dv(16365637) #rut 16.365.637-k
36
+ #=> "k"
37
+
38
+ # Formateo
39
+ Rut.pretty("16365637k")
40
+ #=> "16.365.637-k"
41
+ Rut.pretty("14569484-1")
42
+ #=> "14.569.484-1"
43
+ Rut.pretty("14569484-0")
44
+ ArgumentError: "El rut 14569484-0 es inválido"
45
+ Rut.pretty("14569484-0", false)
46
+ #=> "14.569.484-0"
47
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ end
data/lib/rut.rb ADDED
@@ -0,0 +1,42 @@
1
+ #coding: utf-8
2
+
3
+ class Rut
4
+
5
+ VERSION = "0.0.1"
6
+
7
+ class << self
8
+ def dv rut
9
+ raise ArgumentError, "El rut es nulo" if rut.nil?
10
+ if rut.is_a? String
11
+ raise ArgumentError, "El rut #{rut} debe ser un número" unless rut =~ /^\d{1,8}$/
12
+ end
13
+ rut = rut.to_s
14
+ x=9
15
+ t=0
16
+ rut.reverse.split(//).each do |d|
17
+ t+=d.to_i*x
18
+ x=(x==4) ? 9 : x - 1
19
+ end
20
+ r= t % 11
21
+ (r==10) ? "k" : r
22
+ end
23
+
24
+ def valid? rut
25
+ return true if pretty(rut) rescue false
26
+ end
27
+
28
+ def pretty rut, validate=true
29
+ if rut.is_a? String
30
+ rut.gsub! /[.\-\ ]/, ""
31
+ raise ArgumentError, "El rut está vacío" unless rut.length > 0
32
+ raise ArgumentError, "El rut '#{rut}' contiene caracteres inválidos" unless rut =~ /^\d{1,8}[\dkK]$/
33
+ end
34
+ rut = rut.to_s
35
+ thisdv = rut[-1, 1]
36
+ rut = rut[0, rut.length - 1]
37
+ raise ArgumentError, "El rut #{rut}-#{thisdv} es inválido" if validate && dv(rut).to_s.upcase != thisdv.upcase
38
+ rut = rut.reverse.gsub(/.{3}/, '\0.').gsub(/\.$/, '').reverse
39
+ "#{rut}-#{thisdv}"
40
+ end
41
+ end
42
+ end
data/rut.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rut"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rutcl"
8
+ spec.version = Rut::VERSION
9
+ spec.authors = ["Pablo Marambio"]
10
+ spec.email = ["yo@pablomarambio.cl"]
11
+ spec.description = "Validación y formato de RUTs chilenos"
12
+ spec.summary = "Validación y formato de RUTs chilenos"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,3 @@
1
+ #coding: utf-8
2
+ require 'minitest/autorun'
3
+ require "rut"
@@ -0,0 +1,53 @@
1
+ #coding: utf-8
2
+ require 'test_helper'
3
+
4
+ describe Rut do
5
+ describe "dv" do
6
+ it "should only accept numbers" do
7
+ assert_raises(ArgumentError) { Rut.dv "hola" }
8
+ assert_raises(ArgumentError) { Rut.dv "1234a5678" }
9
+ end
10
+ describe "calculations" do
11
+ describe "for numeric ruts" do
12
+ it "should work with short ruts" do
13
+ Rut.dv(1).must_equal 9
14
+ Rut.dv(2).must_equal 7
15
+ Rut.dv(12).must_equal 4
16
+ Rut.dv(104).must_equal "k"
17
+ Rut.dv(1000).must_equal 6
18
+ Rut.dv(10000).must_equal 5
19
+ Rut.dv(100001).must_equal 2
20
+ end
21
+ it "should work with large ruts" do
22
+ Rut.dv(7956772).must_equal 8
23
+ Rut.dv(8457638).must_equal 7
24
+ Rut.dv(5954893).must_equal "k"
25
+ Rut.dv(16365637).must_equal "k"
26
+ Rut.dv(18439790).must_equal 0
27
+ Rut.dv(11111111).must_equal 1
28
+ Rut.dv(14569484).must_equal 1
29
+ end
30
+ end
31
+ describe "for string ruts" do
32
+ it "should work correct dvs" do
33
+ Rut.dv("1").must_equal 9
34
+ Rut.dv("2").must_equal 7
35
+ Rut.dv("12").must_equal 4
36
+ Rut.dv("104").must_equal "k"
37
+ Rut.dv("1000").must_equal 6
38
+ Rut.dv("10000").must_equal 5
39
+ Rut.dv("100001").must_equal 2
40
+ end
41
+ it "should work with large ruts" do
42
+ Rut.dv("7956772").must_equal 8
43
+ Rut.dv("8457638").must_equal 7
44
+ Rut.dv("5954893").must_equal "k"
45
+ Rut.dv("16365637").must_equal "k"
46
+ Rut.dv("18439790").must_equal 0
47
+ Rut.dv("11111111").must_equal 1
48
+ Rut.dv("14569484").must_equal 1
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,62 @@
1
+ #coding: utf-8
2
+ require 'test_helper'
3
+
4
+ describe Rut do
5
+ describe "pretty" do
6
+ describe "validations" do
7
+ it "should detect empty ruts" do
8
+ assert_raises(ArgumentError) { Rut.pretty "" }
9
+ assert_raises(ArgumentError) { Rut.pretty nil }
10
+ end
11
+ it "should detect non-ruts" do
12
+ assert_raises(ArgumentError) { Rut.pretty "hola" }
13
+ assert_raises(ArgumentError) { Rut.pretty "14x569-484-1" }
14
+ assert_raises(ArgumentError) { Rut.pretty "14569a484-1" }
15
+ assert_raises(ArgumentError) { Rut.pretty "14569484/1" }
16
+ assert_raises(ArgumentError) { Rut.pretty "14 569x484 1" }
17
+ end
18
+ it "should detect invalid ruts" do
19
+ assert_raises(ArgumentError) { Rut.pretty "1-8" }
20
+ assert_raises(ArgumentError) { Rut.pretty "100.001-1" }
21
+ assert_raises(ArgumentError) { Rut.pretty "14.569.484-2" }
22
+ end
23
+ it "should dismiss invalid ruts if told so" do
24
+ Rut.pretty("1-8", false).must_be_instance_of String
25
+ Rut.pretty("100.001-1", false).must_be_instance_of String
26
+ Rut.pretty("14.569.484-2", false).must_be_instance_of String
27
+ end
28
+ end
29
+ describe "formatting" do
30
+ it "should work with unformatted ruts" do
31
+ Rut.pretty(19).must_equal "1-9"
32
+ Rut.pretty(124).must_equal "12-4"
33
+ Rut.pretty("104k").must_equal "104-k"
34
+ Rut.pretty("10006").must_equal "1.000-6"
35
+ Rut.pretty("100005").must_equal "10.000-5"
36
+ Rut.pretty("1000012").must_equal "100.001-2"
37
+ Rut.pretty("79567728").must_equal "7.956.772-8"
38
+ Rut.pretty("145694841").must_equal "14.569.484-1"
39
+ end
40
+ it "should work with partially formatted ruts" do
41
+ Rut.pretty("1000-6").must_equal "1.000-6"
42
+ Rut.pretty("10000-5").must_equal "10.000-5"
43
+ Rut.pretty("100001-2").must_equal "100.001-2"
44
+ Rut.pretty("7956772-8").must_equal "7.956.772-8"
45
+ Rut.pretty("14569484-1").must_equal "14.569.484-1"
46
+ end
47
+ it "should work with fully formatted ruts" do
48
+ Rut.pretty("1.000-6").must_equal "1.000-6"
49
+ Rut.pretty("10.000-5").must_equal "10.000-5"
50
+ Rut.pretty("100.001-2").must_equal "100.001-2"
51
+ Rut.pretty("7.956.772-8").must_equal "7.956.772-8"
52
+ Rut.pretty("14.569.484-1").must_equal "14.569.484-1"
53
+ end
54
+ it "should work with ruts with spaces" do
55
+ Rut.pretty("1 9").must_equal "1-9"
56
+ Rut.pretty(" 12 4").must_equal "12-4"
57
+ Rut.pretty("104 k").must_equal "104-k"
58
+ Rut.pretty("100 0 6").must_equal "1.000-6"
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,27 @@
1
+ #coding: utf-8
2
+ require 'test_helper'
3
+
4
+ describe Rut do
5
+ describe "validations" do
6
+ it "should detect invalid ruts" do
7
+ Rut.valid?(1).must_equal false
8
+ Rut.valid?(123).must_equal false
9
+ Rut.valid?("1040").must_equal false
10
+ Rut.valid?("10005").must_equal false
11
+ Rut.valid?("100003").must_equal false
12
+ Rut.valid?("1000011").must_equal false
13
+ Rut.valid?("79567729").must_equal false
14
+ Rut.valid?("14569484k").must_equal false
15
+ end
16
+ it "should recognize valid ruts" do
17
+ Rut.valid?(19).must_equal true
18
+ Rut.valid?(124).must_equal true
19
+ Rut.valid?("104k").must_equal true
20
+ Rut.valid?("10006").must_equal true
21
+ Rut.valid?("100005").must_equal true
22
+ Rut.valid?("1000012").must_equal true
23
+ Rut.valid?("79567728").must_equal true
24
+ Rut.valid?("145694841").must_equal true
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rutcl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Marambio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Validación y formato de RUTs chilenos
56
+ email:
57
+ - yo@pablomarambio.cl
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rvmrc
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rut.rb
69
+ - rut.gemspec
70
+ - test/test_helper.rb
71
+ - test/unit/rut_computations_test.rb
72
+ - test/unit/rut_formatting_test.rb
73
+ - test/unit/rut_validations_test.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.7
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Validación y formato de RUTs chilenos
98
+ test_files:
99
+ - test/test_helper.rb
100
+ - test/unit/rut_computations_test.rb
101
+ - test/unit/rut_formatting_test.rb
102
+ - test/unit/rut_validations_test.rb