Skip to content

Commit

Permalink
Merge pull request #1 from podemos-info/enhancement/like_pattern_from…
Browse files Browse the repository at this point in the history
…_parts

#like_pattern_from_parts method.
  • Loading branch information
leio10 committed Nov 3, 2017
2 parents 8a470a0 + 4c4c22c commit 1dbe02d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 36 deletions.
6 changes: 0 additions & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,6 @@ Layout/EmptyLinesAroundModuleBody:
# /#.*coding\s?[:=]\s?(?:UTF|utf)-8/
Style/Encoding:
Enabled: true
EnforcedStyle: never
SupportedStyles:
- when_needed
- always
- never
AutoCorrectEncodingComment: '# encoding: utf-8'

Layout/ExtraSpacing:
# When true, allows most uses of extra spacing if the intent is to align
Expand Down
47 changes: 23 additions & 24 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
iban_bic (1.1.0)
iban_bic (1.2.0)
rails (~> 5.1)
regexp-examples (~> 1.3)

Expand Down Expand Up @@ -66,24 +66,23 @@ GEM
diff-lcs (1.3)
docile (1.1.5)
equalizer (0.0.11)
erubi (1.6.1)
erubi (1.7.0)
generator_spec (0.9.4)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
globalid (0.4.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (0.8.6)
i18n (0.9.1)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
json (2.1.0)
loofah (2.1.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.6.6)
mime-types (>= 1.16, < 4)
mail (2.7.0)
mini_mime (>= 0.1.1)
method_source (0.9.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_mime (0.1.4)
mini_portile2 (2.3.0)
minitest (5.10.3)
nio4r (2.1.0)
Expand Down Expand Up @@ -121,26 +120,26 @@ GEM
thor (>= 0.18.1, < 2.0)
rainbow (2.2.2)
rake
rake (12.1.0)
rake (12.2.1)
regexp-examples (1.4.0)
rspec-core (3.6.0)
rspec-support (~> 3.6.0)
rspec-expectations (3.6.0)
rspec-core (3.7.0)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0)
rspec-mocks (3.6.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0)
rspec-rails (3.6.1)
rspec-support (~> 3.7.0)
rspec-rails (3.7.1)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.6.0)
rspec-expectations (~> 3.6.0)
rspec-mocks (~> 3.6.0)
rspec-support (~> 3.6.0)
rspec-support (3.6.0)
rubocop (0.50.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.0)
rubocop (0.51.0)
parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
Expand All @@ -163,7 +162,7 @@ GEM
sqlite3 (1.3.13)
thor (0.20.0)
thread_safe (0.3.6)
tzinfo (1.2.3)
tzinfo (1.2.4)
thread_safe (~> 0.1)
unicode-display_width (1.3.0)
url (0.3.2)
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ validates :iban, iban: { tags: [:sepa] }
```ruby
2.4.1 :001 > IbanBic.like_pattern("ES8700030000300000000000", :country, :bank)
=> "ES__0003________________"
2.4.1 :002 > IbanBic.like_pattern_from_parts(country: "ES", bank: "0003")
=> "ES__0003________________"
```

7. Random IBAN generation
Expand Down Expand Up @@ -124,6 +126,10 @@ $ bundle exec rails generate iban_bic:install --with-static-data
4. Customize initializer if needed, adding validations for new countries, or overriding YAML files.

## Changelog
#### 1.2.0

* Added `like_pattern_from_parts` method. Not a very performant version.
* Fixed portuguese IBAN validation.

#### 1.1.0

Expand Down
2 changes: 1 addition & 1 deletion iban_bic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Gem::Specification.new do |s|

s.add_development_dependency "codecov", "~> 0.1"
s.add_development_dependency "generator_spec", "~> 0.9.3"
s.add_development_dependency "rubocop", "~> 0.50"
s.add_development_dependency "rspec-rails", "~> 3.6"
s.add_development_dependency "rubocop", "~> 0.50"
s.add_development_dependency "sqlite3", "~> 1.3"
s.add_development_dependency "virtus", "~> 1.0"
end
9 changes: 9 additions & 0 deletions lib/iban_bic/core.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "regexp-examples"

module IbanBic
@cached_variables = []

Expand Down Expand Up @@ -98,6 +100,13 @@ def like_pattern(iban, *keep_parts)
parts.map { |part, value| keep_parts.include?(part.to_sym) ? value : value.tr("^_", "_") } .join
end

def like_pattern_from_parts(country:, **parts)
ia_parts = parts.with_indifferent_access
parser[country].to_s.scan(/\(?\<([^\>]+)\>([^\)]*)\)/).map do |part, regex|
(part == "country" && country) || ia_parts[part] || Regexp.new(regex).examples.first.tr("^_", "_")
end.join
end

def clear_cache
@cached_variables.each { |variable| instance_variable_set(variable, nil) }
@cached_variables.clear
Expand Down
2 changes: 0 additions & 2 deletions lib/iban_bic/random.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "regexp-examples"

module IbanBic
module_function

Expand Down
2 changes: 1 addition & 1 deletion lib/iban_bic/validators/countries/pt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
add "PT" do |parts|
nib = "#{parts[:bank]}#{parts[:branch]}#{parts[:account]}"
sum = nib.chars.map(&:to_i).zip(IBAN_BIC_PT_WEIGHTS_INDEX).map { |a| a.inject(:*) } .sum
parts[:check] = 98 - sum % 97
parts[:check] = (98 - sum % 97).to_s.rjust(2, "0")
end
end
2 changes: 1 addition & 1 deletion lib/iban_bic/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module IbanBic
VERSION = "1.1.0"
VERSION = "1.2.0"
end
6 changes: 6 additions & 0 deletions spec/iban_bic/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
it { is_expected.to eq("____0003____#{country_digits}__________") }
end

describe "#like_pattern_from_parts" do
subject(:method) { IbanBic.like_pattern_from_parts(country: "ES", bank: "0003") }

it { is_expected.to eq("ES__0003________________") }
end

describe "#parse" do
subject(:method) { IbanBic.parse(iban) }

Expand Down
2 changes: 1 addition & 1 deletion spec/iban_bic/random_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
RSpec.describe(::IbanBic) do
describe "#random_iban" do
subject(:method) { IbanBic.random_iban params }
let(:params) { {} }
let(:params) { { country: "ES" } }

it "returns a valid IBAN" do
expect(IbanBic.valid?(subject)).to be_truthy
Expand Down

0 comments on commit 1dbe02d

Please sign in to comment.