run_cl 1.2.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82262100b00def3416d849bfbb1a71f7b3bfa681
4
- data.tar.gz: 079229386e79ed2906fada0ae743e9113784dcfd
3
+ metadata.gz: b3b2acf238e7f70d7ccd620a89b0442d41dcbe9f
4
+ data.tar.gz: b81bb124df655014b996bda9ec506c0505ecf016
5
5
  SHA512:
6
- metadata.gz: 91dae2aa8813072d8f8affd0690dd3bda83bb13c88f6aa0aea01e7bbb8d56ef33a65ebd03c26b502b89b28e68151b53578b9adeb6e636ee69337d22ea9469cbb
7
- data.tar.gz: 9683a37d9cb9eb4e4fd661cfda282733fcb3aadacd4fc87049f3e1d54abb1d76b7b5d197ab398c25d9fc1b008088479990479cb7a4a1d3263403c4f4c5f751c0
6
+ metadata.gz: ddc46964bdcc71f716c79a1a4da2deeced75a9f301c1af606ed36614bc4561aff080173e437825d8cd5ba19018cec1e826fb93762a4ca633b6bd16a666f98e02
7
+ data.tar.gz: 0537a47a9377a4e5620813bbccceb99b47d7a36d1102895529659013069daa0e74c8d0cb211de7d9e680dd8a3d5a6dd1f4be9c05bd5c2c92e26f619f6d1f2ff1
data/.gitignore CHANGED
@@ -15,3 +15,6 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+
19
+ # Mac finder artifacts
20
+ .DS_Store
data/README.md CHANGED
@@ -82,11 +82,21 @@ If you need to format a RUN:
82
82
  Run.format('111111111')
83
83
  ====>> 11.111.111-1
84
84
 
85
+ , or remove the verifying digit:
86
+
87
+ Run.format('111111111', false)
88
+ ====>> 11.111.111
89
+
85
90
  If you need to remove a RUN format:
86
91
 
87
92
  Run.remove_format('11.111.111-1')
88
93
  ====>> 111111111
89
94
 
95
+ , or remove the verifying digit:
96
+
97
+ Run.remove_format('11.111.111-1', false)
98
+ ====>> 11111111
99
+
90
100
  ## Assumptions
91
101
 
92
102
  You are saving the RUN as a string, and that is keeping raw (without dots and dashes).
@@ -15,23 +15,21 @@ module RunCl
15
15
  module ClassMethods
16
16
  # include InstanceMethods
17
17
  def has_run_cl(name, options = {})
18
- if options[:run].nil? or options[:run]
19
- if options[:on].present?
20
- validates name, run: true, on: options[:on]
21
- else
22
- validates name, run: true
23
- end
18
+ run = options.delete(:run)
19
+ uniq_run = options.delete(:uniq_run)
20
+ skip_db_format_clear = options.delete(:skip_db_format_clear)
21
+
22
+ if run.nil? or run
23
+ validates name, options.merge({run: true})
24
24
  end
25
25
 
26
- if options[:uniq_run].nil? or options[:uniq_run]
27
- if options[:on].present?
28
- validates name, uniq_run: true, on: options[:on]
29
- else
30
- validates name, uniq_run: true
31
- end
26
+ if uniq_run.class == Hash and uniq_run.keys.any?
27
+ validates name, options.merge({uniq_run: uniq_run})
28
+ elsif uniq_run.nil? or uniq_run == true
29
+ validates name, options.merge({uniq_run: true})
32
30
  end
33
31
 
34
- if options[:skip_db_format_clear].nil? or options[:skip_db_format_clear] == false
32
+ if skip_db_format_clear.nil? or skip_db_format_clear == false
35
33
  before_validation "make_#{name}_format!"
36
34
 
37
35
  define_method "make_#{name}_format!" do
@@ -2,7 +2,7 @@ module Run
2
2
  extend self
3
3
 
4
4
  # Formatea un run
5
- def self.format run
5
+ def self.format run, digit=false
6
6
  return nil if run.nil?
7
7
  pos = 0
8
8
  run.reverse
@@ -17,13 +17,23 @@ module Run
17
17
  end
18
18
  pos += 1
19
19
  end
20
- reverse.reverse
20
+
21
+ if digit
22
+ reverse.reverse[0..-3]
23
+ else
24
+ reverse.reverse
25
+ end
21
26
  end
22
27
 
23
28
  # Quita el formato de un run
24
- def self.remove_format run
29
+ def self.remove_format run, digit=false
25
30
  return nil if run.nil?
26
- run.gsub("-", "").gsub(".", "")
31
+
32
+ if digit
33
+ run.gsub("-", "").gsub(".", "")[0..-3]
34
+ else
35
+ run.gsub("-", "").gsub(".", "")
36
+ end
27
37
  end
28
38
 
29
39
  # Generar un run valido segun reglas chilenas y que ademas es unico para el modelo entregado
@@ -8,6 +8,6 @@ class RunValidator < ActiveModel::Validator
8
8
  end
9
9
 
10
10
  private
11
+ # TODO - locales
11
12
  # record.errors[attribute.to_sym] << I18n.t('run.invalid') unless Run.valid? record.send(attribute)
12
-
13
13
  end
@@ -4,14 +4,25 @@ class UniqRunValidator < ActiveModel::Validator
4
4
  def validate record
5
5
  options[:attributes].each do |attribute|
6
6
  run = Run.remove_format record.send(attribute)
7
+
8
+ matchers = []
7
9
  if record.new_record?
8
- record.errors[attribute.to_sym] << 'ya está en uso!!!' if record.class.where(attribute => run).any?
10
+ matchers = record.class.where(attribute => run)
9
11
  else
10
- record.errors[attribute.to_sym] << 'ya está en uso!!!' if record.class.where('? = ? AND id != ?', attribute, run, record.id).any?
12
+ matchers = record.class.where('? = ? AND id != ?', attribute, run, record.id)
13
+ end
14
+
15
+ if options[:scope]
16
+ matchers = matchers.where(options[:scope] => record.send(options[:scope]))
11
17
  end
18
+
19
+ matchers.any?
20
+
21
+ record.errors[attribute.to_sym] << 'ya está en uso!!!' if matchers.any?
12
22
  end
13
23
  end
14
24
 
15
25
  private
26
+ # TODO - locales
16
27
  # record.errors[attribute.to_sym] << I18n.t('run.in_use') unless Run.valid? record.send(attribute)
17
28
  end
@@ -1,3 +1,3 @@
1
1
  module RunCl
2
- VERSION = "1.2.0"
2
+ VERSION = "1.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: run_cl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mespina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-30 00:00:00.000000000 Z
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Formateador/Desformateador, Generador, Validador de Rut Chilenos
14
14
  email:
@@ -17,7 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - .gitignore
20
+ - ".gitignore"
21
21
  - Gemfile
22
22
  - LICENSE.txt
23
23
  - README.md
@@ -37,17 +37,17 @@ require_paths:
37
37
  - lib
38
38
  required_ruby_version: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - '>='
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 2.2.0
50
+ rubygems_version: 2.2.2
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: Rut Chilenos