biggs 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e01c45617723b2d38b705c737fedf6bd560e9211c1ef834272cd9c4fd8f09814
4
+ data.tar.gz: 63763d95d695fe3078517c2f1e4cc80246dec8a53e6e8191c69fd71feb49d222
5
+ SHA512:
6
+ metadata.gz: 891ec94ea1236687a03296b4904740b5b28f391bc5de769ad5d78d6f9cb4fd3225399823c60e1de18145706b29940d0cea0a556cc1ec2b5f0a4a832645c14f92
7
+ data.tar.gz: 10383285215245585fca93d53377804c759beb43484f333421e7874768455dfbdb47b117885c466708a45f1501a0d410890c725ae0871db6c476553ac63b2205
data/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ### dev
2
2
 
3
+ ### 0.4.0 / 2022-04-08
4
+
5
+ * [BREAKING]: Do not include to ActiveRecord::Base per default, use 'include Biggs::ActiveRecordAdapter' in your classes
6
+ * Allow multiple biggs methods per class
7
+
3
8
  ### 0.3.3 / 2013-05-06
4
9
 
5
10
  * Added support for Rails 4 (by [mdemare](https://github.com/mdemare))
data/README.md CHANGED
@@ -4,11 +4,7 @@ biggs is a small ruby gem/rails plugin for formatting postal addresses from over
4
4
 
5
5
  As a ruby gem:
6
6
 
7
- sudo gem install biggs
8
-
9
- If your rather prefer to install it as a plugin for rails, from your application directory simply run:
10
-
11
- script/plugin install git://github.com/yolk/biggs.git
7
+ gem install biggs
12
8
 
13
9
  ### Standalone usage
14
10
 
@@ -16,34 +12,36 @@ If your rather prefer to install it as a plugin for rails, from your application
16
12
 
17
13
  f.format("de", # <= ISO alpha 2 code
18
14
  :recipient => "Yolk Sebastian Munz & Julia Soergel GbR",
19
- :street => "Adalbertstr. 11", # <= street + house number
20
- :city => "Berlin",
21
- :zip => 10999,
22
- :state => "Berlin" # <= state/province/region
15
+ :street => "Musterallee 12", # <= street + house number
16
+ :city => "Ausgedacht",
17
+ :zip => 12345,
18
+ :state => "Nowhere" # <= state/province/region
23
19
  )
24
20
 
25
21
  returns
26
22
 
27
23
  "Yolk Sebastian Munz & Julia Soergel GbR
28
- Adalbertstr. 11
29
- 10999 Berlin
24
+ Musterallee 12
25
+ 12345 Ausgedacht
30
26
  Germany"
31
27
 
32
28
  At the moment Biggs::Formatter.new accepts only one option:
33
29
 
34
- *blank_county_on* ISO alpha 2 code (single string or array) of countries the formatter should skip the line "country" (for national shipping).
30
+ *blank_country_on* ISO alpha 2 code (single string or array) of countries the formatter should skip the line "country" (for national shipping).
35
31
 
36
- Biggs::Formatter.new(:blank_county_on => "de")
32
+ Biggs::Formatter.new(blank_country_on: "de")
37
33
 
38
34
  With the data from the above example this would return:
39
35
 
40
36
  "Yolk Sebastian Munz & Julia Soergel GbR
41
- Adalbertstr. 11
42
- 10999 Berlin"
37
+ Musterallee 12
38
+ 12345 Ausgedacht"
43
39
 
44
40
  ### Usage with Rails and ActiveRecord
45
41
 
46
42
  Address < ActiveRecord::Base
43
+ include Biggs::ActiveRecordAdapter
44
+
47
45
  biggs :postal_address
48
46
  end
49
47
 
@@ -52,6 +50,8 @@ This adds the method postal_address to your Address-model, and assumes the prese
52
50
  You can customize the method-names biggs will use by passing in a hash of options:
53
51
 
54
52
  Address < ActiveRecord::Base
53
+ include Biggs::ActiveRecordAdapter
54
+
55
55
  biggs :postal_address,
56
56
  :zip => :postal_code,
57
57
  :country => :country_code,
@@ -63,6 +63,8 @@ You can pass in a symbol to let biggs call a different method on your Address-mo
63
63
  You can even pass in a array of symbols:
64
64
 
65
65
  Address < ActiveRecord::Base
66
+ include Biggs::ActiveRecordAdapter
67
+
66
68
  biggs :postal_address,
67
69
  :recipient => [:company_name, :person_name]
68
70
  end
@@ -145,6 +147,6 @@ biggs knows how to format addresses of over 60 different countries. If you are m
145
147
  * United States of America
146
148
  * Yemen
147
149
 
148
- biggs is tested to behave well with Rails 3.0, 3.1, 3.2 and 4.0
150
+ biggs is tested to behave well with Rails 3 to 7
149
151
 
150
- Copyright (c) 2009-2013 Yolk Sebastian Munz & Julia Soergel GbR
152
+ Copyright (c) 2009-2022 Yolk Sebastian Munz & Julia Soergel GbR
data/biggs.gemspec CHANGED
@@ -21,7 +21,8 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_dependency 'activerecord', '>= 3.0'
23
23
  s.add_development_dependency 'rake'
24
- s.add_development_dependency 'rspec', '>= 2.4.0'
24
+ s.add_development_dependency 'rspec', '>= 3.0'
25
+ s.add_development_dependency 'rspec-its'
25
26
  s.add_development_dependency 'sqlite3', '>= 1.3.5'
26
27
  end
27
28
 
@@ -2,60 +2,56 @@ require 'active_support/core_ext/class/attribute'
2
2
 
3
3
  module Biggs
4
4
  module ActiveRecordAdapter
5
-
6
- def self.included(base)
7
- base.extend(InitialClassMethods)
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :biggs_config, default: {}
8
9
  end
9
-
10
- module InitialClassMethods
11
- def biggs(method_name=nil, options={})
12
- self.class_attribute :biggs_value_methods
13
- self.class_attribute :biggs_instance
14
10
 
15
- self.send(:include, Biggs::ActiveRecordAdapter::InstanceMethods)
16
- alias_method(method_name || :postal_address, :biggs_postal_address)
11
+ class_methods do
12
+ def biggs(method_name=:postal_address, options={})
13
+ self.define_method(method_name) do
14
+ value_methods = self.biggs_config[method_name][:value_methods]
15
+ self.biggs_config[method_name][:instance].format(
16
+ Helpers.biggs_get_value(self, :country, value_methods),
17
+ Helpers.biggs_values(self, value_methods)
18
+ )
19
+ end
17
20
 
18
21
  value_methods = {}
19
22
  Biggs::Formatter::FIELDS.each do |field|
20
23
  value_methods[field] = options.delete(field) if options[field]
21
24
  end
22
-
23
- self.biggs_value_methods = value_methods
24
- self.biggs_instance = Biggs::Formatter.new(options)
25
+
26
+ self.biggs_config = self.biggs_config.dup
27
+
28
+ self.biggs_config[method_name] = {
29
+ instance: Biggs::Formatter.new(options),
30
+ value_methods: value_methods
31
+ }
25
32
  end
26
33
  end
27
-
28
- module InstanceMethods
29
-
30
- def biggs_postal_address
31
- self.class.biggs_instance.format(biggs_country, biggs_values)
32
- end
33
-
34
- private
35
-
36
- def biggs_country
37
- biggs_get_value(:country)
38
- end
39
-
40
- def biggs_values
34
+
35
+ module Helpers
36
+ def self.biggs_values(instance, value_methods)
41
37
  values = {}
42
38
  (Biggs::Formatter::FIELDS - [:country]).each do |field|
43
- values[field] = biggs_get_value(field)
39
+ values[field] = self.biggs_get_value(instance, field, value_methods)
44
40
  end
45
41
  values
46
42
  end
47
-
48
- def biggs_get_value(field)
49
- key = self.class.biggs_value_methods[field.to_sym] || field.to_sym
50
-
43
+
44
+ def self.biggs_get_value(instance, field, value_methods)
45
+ key = value_methods[field] || field
46
+
51
47
  case key
52
48
  when Symbol
53
- self.send(key.to_sym)
49
+ instance.send(key.to_sym)
54
50
  when Proc
55
- key.call(self).to_s
51
+ key.call(instance).to_s
56
52
  when Array
57
53
  if key.all?{|it| it.is_a?(Symbol) }
58
- key.map{|method| self.send(method) }.reject(&:blank?).join("\n")
54
+ key.map{|method| instance.send(method) }.reject(&:blank?).join("\n")
59
55
  else
60
56
  raise "Biggs: Can't handle #{field} type Array with non-symbolic entries"
61
57
  end
@@ -65,4 +61,4 @@ module Biggs
65
61
  end
66
62
  end
67
63
  end
68
- end
64
+ end
data/lib/biggs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Biggs
2
- VERSION = "0.3.3"
3
- end
2
+ VERSION = "0.4.0"
3
+ end
data/lib/biggs.rb CHANGED
@@ -7,7 +7,7 @@ module Biggs
7
7
  def formats
8
8
  @@formats ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', 'formats.yml')) || {}
9
9
  end
10
-
10
+
11
11
  def country_names
12
12
  @@country_names ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', 'country_names.yml')) || {}
13
13
  end
@@ -16,5 +16,4 @@ end
16
16
 
17
17
  if defined?(ActiveRecord) and defined?(ActiveRecord::Base) and !ActiveRecord::Base.respond_to?(:biggs_formatter)
18
18
  require 'biggs/activerecord'
19
- ActiveRecord::Base.send :include, Biggs::ActiveRecordAdapter
20
- end
19
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  require 'rubygems'
3
3
  require 'rspec'
4
+ require 'rspec/its'
4
5
  require File.join(File.dirname(__FILE__), '..', 'lib', 'biggs')
5
6
  require 'logger'
6
7
 
@@ -12,4 +13,8 @@ ActiveRecord::Schema.define do
12
13
  create_table :base_tables, :force => true do |table|
13
14
  table.string :name
14
15
  end
15
- end
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ config.expect_with(:rspec) { |c| c.syntax = :should }
20
+ end
@@ -3,6 +3,8 @@ require 'active_record'
3
3
  require File.join(File.dirname(__FILE__), '..', 'spec_helper')
4
4
 
5
5
  class BaseTable < ActiveRecord::Base
6
+ include Biggs::ActiveRecordAdapter
7
+
6
8
  Biggs::Formatter::FIELDS.each do |field|
7
9
  define_method(field) do
8
10
  field == :country ? "us" : field.to_s.upcase
@@ -19,11 +21,11 @@ end
19
21
  class FooBarCustomFields < BaseTable
20
22
  biggs :postal_address, :country => :my_custom_country_method,
21
23
  :city => :my_custom_city_method
22
-
24
+
23
25
  def my_custom_country_method
24
26
  "de"
25
27
  end
26
-
28
+
27
29
  def my_custom_city_method
28
30
  "Hamburg"
29
31
  end
@@ -31,7 +33,7 @@ end
31
33
 
32
34
  class FooBarCustomBlankDECountry < BaseTable
33
35
  biggs :postal_address, :blank_country_on => "de"
34
-
36
+
35
37
  def country
36
38
  "DE"
37
39
  end
@@ -42,69 +44,74 @@ class FooBarCustomMethod < BaseTable
42
44
  end
43
45
 
44
46
  class FooBarCustomProc < BaseTable
45
- biggs :postal_address,
46
- :country => Proc.new {|it| it.method_accessed_from_proc + "XX"},
47
+ biggs :postal_address,
48
+ :country => Proc.new {|it| it.method_accessed_from_proc + "XX"},
47
49
  :state => Proc.new {|it| it.state.downcase }
48
-
50
+
49
51
  def method_accessed_from_proc
50
52
  "DE"
51
53
  end
52
54
  end
53
55
 
54
56
  class FooBarCustomArray < BaseTable
55
- biggs :postal_address,
57
+ biggs :postal_address,
56
58
  :street => [:address_1, :address_empty, :address_nil, :address_2]
57
-
59
+
58
60
  def address_1
59
61
  "Address line 1"
60
62
  end
61
-
63
+
62
64
  def address_2
63
65
  "Address line 2"
64
66
  end
65
-
67
+
66
68
  def address_empty
67
69
  ""
68
70
  end
69
-
71
+
70
72
  def address_nil
71
73
  nil
72
74
  end
73
75
  end
74
76
 
77
+ class FooBarMultiple < BaseTable
78
+ biggs :postal_address_one
79
+ biggs :postal_address_two,
80
+ country: :alt_country
81
+
82
+ def alt_country
83
+ "Alt country"
84
+ end
85
+ end
86
+
75
87
  describe "ActiveRecord Class" do
76
-
88
+
77
89
  it "should include Biggs::ActiveRecordAdapter" do
78
90
  FooBar.included_modules.should be_include(Biggs::ActiveRecordAdapter)
79
91
  end
80
-
81
- it "should set class value biggs_value_methods" do
82
- FooBar.class_eval("biggs_value_methods").should be_is_a(Hash)
83
- FooBar.class_eval("biggs_value_methods").size.should be_zero
84
- end
85
-
86
- it "should set class value biggs_instance" do
87
- FooBar.class_eval("biggs_instance").should be_is_a(Biggs::Formatter)
92
+
93
+ it "should set class value biggs_config" do
94
+ FooBar.class_eval("biggs_config").should be_is_a(Hash)
88
95
  end
89
-
96
+
90
97
  it "should respond to biggs" do
91
98
  FooBar.should be_respond_to(:biggs)
92
99
  end
93
100
  end
94
101
 
95
102
  describe "ActiveRecord Instance" do
96
-
103
+
97
104
  describe "Empty" do
98
105
  it "should not have postal_address method" do
99
106
  FooBarEmpty.new.should_not be_respond_to(:postal_address)
100
107
  end
101
108
  end
102
-
109
+
103
110
  describe "Standard" do
104
111
  it "should have postal_address method" do
105
112
  FooBar.new.should be_respond_to(:postal_address)
106
113
  end
107
-
114
+
108
115
  it "should return postal_address on postal_address" do
109
116
  FooBar.new.postal_address.should eql("RECIPIENT\nSTREET\nCITY STATE ZIP\nUnited States of America")
110
117
  end
@@ -121,27 +128,38 @@ describe "ActiveRecord Instance" do
121
128
  FooBarCustomBlankDECountry.new.postal_address.should eql("RECIPIENT\nSTREET\nZIP CITY")
122
129
  end
123
130
  end
124
-
131
+
125
132
  describe "Customized Method name" do
126
133
  it "should have my_postal_address_method" do
127
134
  FooBarCustomMethod.new.should be_respond_to(:my_postal_address_method)
128
135
  end
129
-
136
+
130
137
  it "should return formatted address on my_postal_address_method" do
131
138
  FooBarCustomMethod.new.my_postal_address_method.should eql("RECIPIENT\nSTREET\nCITY STATE ZIP\nUnited States of America")
132
139
  end
133
140
  end
134
-
141
+
135
142
  describe "Customized Proc as Param" do
136
143
  it "should return formatted address for unknown-country DEXX" do
137
144
  FooBarCustomProc.new.postal_address.should eql("RECIPIENT\nSTREET\nCITY state ZIP\ndexx")
138
145
  end
139
146
  end
140
-
147
+
141
148
  describe "Customized array of symbols" do
142
149
  it "should return formatted address with two lines for street" do
143
150
  FooBarCustomArray.new.postal_address.should eql("RECIPIENT\nAddress line 1\nAddress line 2\nCITY STATE ZIP\nUnited States of America")
144
151
  end
145
152
  end
146
153
 
147
- end
154
+ describe "Multiple" do
155
+
156
+ it "should return postal_address on postal_address_one" do
157
+ FooBarMultiple.new.postal_address_one.should eql("RECIPIENT\nSTREET\nCITY STATE ZIP\nUnited States of America")
158
+ end
159
+
160
+ it "should return postal_address with alt country on postal_address_two" do
161
+ FooBarMultiple.new.postal_address_two.should eql("RECIPIENT\nSTREET\nCITY STATE ZIP\nalt country")
162
+ end
163
+ end
164
+
165
+ end
metadata CHANGED
@@ -1,80 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biggs
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.3.3
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sebastian Munz
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-06 00:00:00.000000000 Z
11
+ date: 2022-04-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- version_requirements: !ruby/object:Gem::Requirement
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
- none: false
21
- name: activerecord
22
20
  type: :runtime
23
21
  prerelease: false
24
- requirement: !ruby/object:Gem::Requirement
22
+ version_requirements: !ruby/object:Gem::Requirement
25
23
  requirements:
26
- - - ! '>='
24
+ - - ">="
27
25
  - !ruby/object:Gem::Version
28
26
  version: '3.0'
29
- none: false
30
27
  - !ruby/object:Gem::Dependency
31
- version_requirements: !ruby/object:Gem::Requirement
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
32
30
  requirements:
33
- - - ! '>='
31
+ - - ">="
34
32
  - !ruby/object:Gem::Version
35
33
  version: '0'
36
- none: false
37
- name: rake
38
34
  type: :development
39
35
  prerelease: false
40
- requirement: !ruby/object:Gem::Requirement
36
+ version_requirements: !ruby/object:Gem::Requirement
41
37
  requirements:
42
- - - ! '>='
38
+ - - ">="
43
39
  - !ruby/object:Gem::Version
44
40
  version: '0'
45
- none: false
46
41
  - !ruby/object:Gem::Dependency
47
- version_requirements: !ruby/object:Gem::Requirement
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
48
44
  requirements:
49
- - - ! '>='
45
+ - - ">="
50
46
  - !ruby/object:Gem::Version
51
- version: 2.4.0
52
- none: false
53
- name: rspec
47
+ version: '3.0'
54
48
  type: :development
55
49
  prerelease: false
56
- requirement: !ruby/object:Gem::Requirement
50
+ version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
- - - ! '>='
52
+ - - ">="
59
53
  - !ruby/object:Gem::Version
60
- version: 2.4.0
61
- none: false
54
+ version: '3.0'
62
55
  - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
63
64
  version_requirements: !ruby/object:Gem::Requirement
64
65
  requirements:
65
- - - ! '>='
66
+ - - ">="
66
67
  - !ruby/object:Gem::Version
67
- version: 1.3.5
68
- none: false
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
69
70
  name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.5
70
76
  type: :development
71
77
  prerelease: false
72
- requirement: !ruby/object:Gem::Requirement
78
+ version_requirements: !ruby/object:Gem::Requirement
73
79
  requirements:
74
- - - ! '>='
80
+ - - ">="
75
81
  - !ruby/object:Gem::Version
76
82
  version: 1.3.5
77
- none: false
78
83
  description: biggs is a small ruby gem/rails plugin for formatting postal addresses
79
84
  from over 60 countries.
80
85
  email:
@@ -83,8 +88,8 @@ executables: []
83
88
  extensions: []
84
89
  extra_rdoc_files: []
85
90
  files:
86
- - .gitignore
87
- - .rvmrc
91
+ - ".gitignore"
92
+ - ".rvmrc"
88
93
  - CHANGES.md
89
94
  - Gemfile
90
95
  - LICENSE
@@ -105,27 +110,25 @@ files:
105
110
  - spec/unit/format_spec.rb
106
111
  homepage: https://github.com/yolk/biggs
107
112
  licenses: []
108
- post_install_message:
113
+ metadata: {}
114
+ post_install_message:
109
115
  rdoc_options: []
110
116
  require_paths:
111
117
  - lib
112
118
  required_ruby_version: !ruby/object:Gem::Requirement
113
119
  requirements:
114
- - - ! '>='
120
+ - - ">="
115
121
  - !ruby/object:Gem::Version
116
122
  version: '0'
117
- none: false
118
123
  required_rubygems_version: !ruby/object:Gem::Requirement
119
124
  requirements:
120
- - - ! '>='
125
+ - - ">="
121
126
  - !ruby/object:Gem::Version
122
127
  version: '0'
123
- none: false
124
128
  requirements: []
125
- rubyforge_project: biggs
126
- rubygems_version: 1.8.23
127
- signing_key:
128
- specification_version: 3
129
+ rubygems_version: 3.1.6
130
+ signing_key:
131
+ specification_version: 4
129
132
  summary: biggs is a small ruby gem/rails plugin for formatting postal addresses from
130
133
  over 60 countries.
131
134
  test_files: