biggs 0.4.0 → 0.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
  SHA256:
3
- metadata.gz: e01c45617723b2d38b705c737fedf6bd560e9211c1ef834272cd9c4fd8f09814
4
- data.tar.gz: 63763d95d695fe3078517c2f1e4cc80246dec8a53e6e8191c69fd71feb49d222
3
+ metadata.gz: 72743e6a2efe86f4e3954f6783380627afa89d944c0f23eb8f5e05ae271c24aa
4
+ data.tar.gz: eadb5928335ad7a9dfe362fcfa3bf7bc6b156ad0a0051c046c680c39188cd020
5
5
  SHA512:
6
- metadata.gz: 891ec94ea1236687a03296b4904740b5b28f391bc5de769ad5d78d6f9cb4fd3225399823c60e1de18145706b29940d0cea0a556cc1ec2b5f0a4a832645c14f92
7
- data.tar.gz: 10383285215245585fca93d53377804c759beb43484f333421e7874768455dfbdb47b117885c466708a45f1501a0d410890c725ae0871db6c476553ac63b2205
6
+ metadata.gz: 5deb05a75009544a7439169f7fb12bacff1ec3643b51b9715f44eb0866961dee09138ceb87702c8d1977b4643f41531be85646fd0bd91b551030b5855ff0c8a2
7
+ data.tar.gz: 17bc4a86075d4c56b3128d0a1d209f06446ca9437095cdb921393179d6335a3559b30524ee4d11e9ba8da747900859e95370748cc8c627d88d1e81ddc1f2ced3
data/CHANGES.md CHANGED
@@ -1,5 +1,22 @@
1
1
  ### dev
2
2
 
3
+ ### 0.6.0 / 2022-04-08
4
+
5
+ * Remove not only trailing but also leading newline from formatted address
6
+ * Biggs::Concern Allow passing false as field to add blank string
7
+
8
+ ### 0.5.1 / 2022-04-08
9
+
10
+ * Biggs::Extractor: small refactorings + don't delete options
11
+
12
+ ### 0.5.0 / 2022-04-08
13
+
14
+ * Refactored Biggs::Concern to use Biggs::Extractor
15
+ * Removed activerecord dependency and use only activesupport instead
16
+ * Renamed Biggs::ActiveRecordAdapter to Biggs::Concern, include with 'include Biggs'
17
+ * Optimization: Added Biggs::Formatter::FIELDS_WO_COUNTRY
18
+ * Moved Biggs.formats and Biggs.country_names to Constants Biggs::FORMATS & Biggs::COUNTRY_NAMES
19
+
3
20
  ### 0.4.0 / 2022-04-08
4
21
 
5
22
  * [BREAKING]: Do not include to ActiveRecord::Base per default, use 'include Biggs::ActiveRecordAdapter' in your classes
data/README.md CHANGED
@@ -37,10 +37,10 @@ With the data from the above example this would return:
37
37
  Musterallee 12
38
38
  12345 Ausgedacht"
39
39
 
40
- ### Usage with Rails and ActiveRecord
40
+ ### Usage with Class
41
41
 
42
- Address < ActiveRecord::Base
43
- include Biggs::ActiveRecordAdapter
42
+ class Address
43
+ include Biggs
44
44
 
45
45
  biggs :postal_address
46
46
  end
@@ -49,8 +49,8 @@ This adds the method postal_address to your Address-model, and assumes the prese
49
49
 
50
50
  You can customize the method-names biggs will use by passing in a hash of options:
51
51
 
52
- Address < ActiveRecord::Base
53
- include Biggs::ActiveRecordAdapter
52
+ class Address
53
+ include Biggs
54
54
 
55
55
  biggs :postal_address,
56
56
  :zip => :postal_code,
@@ -62,8 +62,8 @@ You can pass in a symbol to let biggs call a different method on your Address-mo
62
62
 
63
63
  You can even pass in a array of symbols:
64
64
 
65
- Address < ActiveRecord::Base
66
- include Biggs::ActiveRecordAdapter
65
+ class Address
66
+ include Biggs
67
67
 
68
68
  biggs :postal_address,
69
69
  :recipient => [:company_name, :person_name]
@@ -73,7 +73,7 @@ This will call the methods company_name and person_name on your address-instance
73
73
 
74
74
  To access the formatted address string, simply call the provided method on an address instance:
75
75
 
76
- Address.find(1).postal_address
76
+ Address.new.postal_address
77
77
 
78
78
  If you pass in a ISO alpha 2 code as :country that is not supported by biggs, it will choose the US-format for addresses with an state specified, and the french/german format for addresses without an state.
79
79
 
@@ -147,6 +147,6 @@ biggs knows how to format addresses of over 60 different countries. If you are m
147
147
  * United States of America
148
148
  * Yemen
149
149
 
150
- biggs is tested to behave well with Rails 3 to 7
150
+ biggs is tested to behave well with ActiveSupport 3 to 7
151
151
 
152
152
  Copyright (c) 2009-2022 Yolk Sebastian Munz & Julia Soergel GbR
data/biggs.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency 'activerecord', '>= 3.0'
22
+ s.add_dependency 'activesupport', '>= 3.0'
23
23
  s.add_development_dependency 'rake'
24
24
  s.add_development_dependency 'rspec', '>= 3.0'
25
25
  s.add_development_dependency 'rspec-its'
@@ -0,0 +1,34 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/class/attribute'
3
+ require 'biggs/extractor'
4
+ require 'biggs/formatter'
5
+
6
+ module Biggs
7
+ module Concern
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ class_attribute :biggs_config, default: {}
12
+ end
13
+
14
+ class_methods do
15
+ def biggs(method_name=:postal_address, options={})
16
+ self.define_method(method_name) do
17
+ formatter = self.biggs_config[method_name][:formatter]
18
+ extractor = self.biggs_config[method_name][:extractor]
19
+ formatter.format(
20
+ extractor.get_value(self, :country),
21
+ extractor.get_values(self)
22
+ )
23
+ end
24
+
25
+ self.biggs_config = self.biggs_config.dup
26
+
27
+ self.biggs_config[method_name] = {
28
+ formatter: Biggs::Formatter.new(options),
29
+ extractor: Biggs::Extractor.new(options)
30
+ }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ module Biggs
2
+ class Extractor
3
+ def initialize(options)
4
+ @value_methods = Biggs::Formatter::FIELDS.reduce({}) do |methods, field|
5
+ methods[field] = options[field] if options[field] || options[field] == false
6
+ methods
7
+ end
8
+ end
9
+
10
+ def get_values(instance)
11
+ Biggs::Formatter::FIELDS_WO_COUNTRY.inject({}) do |values, field|
12
+ values[field] = get_value(instance, field)
13
+ values
14
+ end
15
+ end
16
+
17
+ def get_value(instance, field)
18
+ return if @value_methods[field] == false
19
+
20
+ key = @value_methods[field] || field
21
+
22
+ case key
23
+ when Symbol
24
+ instance.send(key.to_sym)
25
+ when Proc
26
+ key.call(instance).to_s
27
+ when Array
28
+ if key.all?{|it| it.is_a?(Symbol) }
29
+ key.map{|method| instance.send(method) }.reject(&:blank?).join("\n")
30
+ else
31
+ raise "Biggs: Can't handle #{field} type Array with non-symbolic entries"
32
+ end
33
+ else
34
+ raise "Biggs: Can't handle #{field} type #{key.class}"
35
+ end
36
+ end
37
+ end
38
+ end
data/lib/biggs/format.rb CHANGED
@@ -4,8 +4,8 @@ module Biggs
4
4
 
5
5
  def initialize(iso_code)
6
6
  @iso_code = iso_code.to_s.downcase
7
- @country_name = Biggs.country_names[@iso_code]
8
- @format_string = Biggs.formats[@iso_code]
7
+ @country_name = Biggs::COUNTRY_NAMES[@iso_code]
8
+ @format_string = Biggs::FORMATS[@iso_code]
9
9
  end
10
10
 
11
11
  class << self
@@ -20,4 +20,4 @@ module Biggs
20
20
  end
21
21
  end
22
22
  end
23
- end
23
+ end
@@ -1,12 +1,13 @@
1
1
  module Biggs
2
2
  class Formatter
3
-
4
- FIELDS = [:recipient, :street, :city, :state, :zip, :country]
5
-
3
+
4
+ FIELDS_WO_COUNTRY = [:recipient, :street, :city, :state, :zip].freeze
5
+ FIELDS = (FIELDS_WO_COUNTRY + [:country]).freeze
6
+
6
7
  def initialize(options={})
7
8
  @blank_country_on = [options[:blank_country_on]].compact.flatten.map{|s| s.to_s.downcase}
8
9
  end
9
-
10
+
10
11
  def format(iso_code, values={})
11
12
  values.symbolize_keys! if values.respond_to?(:symbolize_keys!)
12
13
 
@@ -14,22 +15,22 @@ module Biggs
14
15
  format_string = (format.format_string || default_format_string(values[:state])).dup.to_s
15
16
  country_name = blank_country_on.include?(format.iso_code) ? "" : format.country_name || format.iso_code
16
17
 
17
- (FIELDS - [:country]).each do |key|
18
+ FIELDS_WO_COUNTRY.each do |key|
18
19
  format_string.gsub!(/\{\{#{key}\}\}/, (values[key] || "").to_s)
19
20
  end
20
21
  format_string.gsub!(/\{\{country\}\}/, country_name)
21
- format_string.gsub(/\n$/, "")
22
+ format_string.strip
22
23
  end
23
-
24
+
24
25
  attr_accessor :blank_country_on, :default_country_without_state, :default_country_with_state
25
-
26
+
26
27
  private
27
-
28
+
28
29
  def default_format_string(state)
29
30
  state && state != "" ?
30
- Biggs.formats[default_country_with_state || "us"] :
31
- Biggs.formats[default_country_without_state || "fr"]
31
+ Biggs::FORMATS[default_country_with_state || "us"] :
32
+ Biggs::FORMATS[default_country_without_state || "fr"]
32
33
  end
33
34
  end
34
-
35
- end
35
+
36
+ end
data/lib/biggs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Biggs
2
- VERSION = "0.4.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/biggs.rb CHANGED
@@ -1,19 +1,16 @@
1
+ require 'active_support'
1
2
  require 'biggs/format'
2
3
  require 'biggs/formatter'
4
+ require 'biggs/concern'
3
5
  require 'yaml'
4
6
 
5
7
  module Biggs
6
- class << self
7
- def formats
8
- @@formats ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', 'formats.yml')) || {}
9
- end
8
+ FORMATS = (YAML.load_file(File.join(File.dirname(__FILE__), '..', 'formats.yml')) || {}).freeze
9
+ COUNTRY_NAMES = (YAML.load_file(File.join(File.dirname(__FILE__), '..', 'country_names.yml')) || {}).freeze
10
10
 
11
- def country_names
12
- @@country_names ||= YAML.load_file(File.join(File.dirname(__FILE__), '..', 'country_names.yml')) || {}
13
- end
14
- end
15
- end
11
+ extend ActiveSupport::Concern
16
12
 
17
- if defined?(ActiveRecord) and defined?(ActiveRecord::Base) and !ActiveRecord::Base.respond_to?(:biggs_formatter)
18
- require 'biggs/activerecord'
13
+ included do
14
+ include Biggs::Concern
15
+ end
19
16
  end
data/spec/spec_helper.rb CHANGED
@@ -5,16 +5,6 @@ require 'rspec/its'
5
5
  require File.join(File.dirname(__FILE__), '..', 'lib', 'biggs')
6
6
  require 'logger'
7
7
 
8
- ActiveRecord::Base.logger = Logger.new('/tmp/biggs.log')
9
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => '/tmp/biggs.sqlite')
10
- ActiveRecord::Migration.verbose = false
11
-
12
- ActiveRecord::Schema.define do
13
- create_table :base_tables, :force => true do |table|
14
- table.string :name
15
- end
16
- end
17
-
18
8
  RSpec.configure do |config|
19
9
  config.expect_with(:rspec) { |c| c.syntax = :should }
20
10
  end
@@ -1,9 +1,7 @@
1
- require 'rubygems'
2
- require 'active_record'
3
1
  require File.join(File.dirname(__FILE__), '..', 'spec_helper')
4
2
 
5
- class BaseTable < ActiveRecord::Base
6
- include Biggs::ActiveRecordAdapter
3
+ class BaseClass
4
+ include Biggs
7
5
 
8
6
  Biggs::Formatter::FIELDS.each do |field|
9
7
  define_method(field) do
@@ -12,13 +10,13 @@ class BaseTable < ActiveRecord::Base
12
10
  end
13
11
  end
14
12
 
15
- class FooBarEmpty < BaseTable;end
13
+ class FooBarEmpty < BaseClass;end
16
14
 
17
- class FooBar < BaseTable
15
+ class FooBar < BaseClass
18
16
  biggs :postal_address
19
17
  end
20
18
 
21
- class FooBarCustomFields < BaseTable
19
+ class FooBarCustomFields < BaseClass
22
20
  biggs :postal_address, :country => :my_custom_country_method,
23
21
  :city => :my_custom_city_method
24
22
 
@@ -31,7 +29,7 @@ class FooBarCustomFields < BaseTable
31
29
  end
32
30
  end
33
31
 
34
- class FooBarCustomBlankDECountry < BaseTable
32
+ class FooBarCustomBlankDECountry < BaseClass
35
33
  biggs :postal_address, :blank_country_on => "de"
36
34
 
37
35
  def country
@@ -39,11 +37,11 @@ class FooBarCustomBlankDECountry < BaseTable
39
37
  end
40
38
  end
41
39
 
42
- class FooBarCustomMethod < BaseTable
40
+ class FooBarCustomMethod < BaseClass
43
41
  biggs :my_postal_address_method
44
42
  end
45
43
 
46
- class FooBarCustomProc < BaseTable
44
+ class FooBarCustomProc < BaseClass
47
45
  biggs :postal_address,
48
46
  :country => Proc.new {|it| it.method_accessed_from_proc + "XX"},
49
47
  :state => Proc.new {|it| it.state.downcase }
@@ -53,7 +51,7 @@ class FooBarCustomProc < BaseTable
53
51
  end
54
52
  end
55
53
 
56
- class FooBarCustomArray < BaseTable
54
+ class FooBarCustomArray < BaseClass
57
55
  biggs :postal_address,
58
56
  :street => [:address_1, :address_empty, :address_nil, :address_2]
59
57
 
@@ -74,20 +72,17 @@ class FooBarCustomArray < BaseTable
74
72
  end
75
73
  end
76
74
 
77
- class FooBarMultiple < BaseTable
75
+ class FooBarMultiple < BaseClass
78
76
  biggs :postal_address_one
79
77
  biggs :postal_address_two,
80
- country: :alt_country
81
-
82
- def alt_country
83
- "Alt country"
84
- end
78
+ country: false,
79
+ recipient: false
85
80
  end
86
81
 
87
- describe "ActiveRecord Class" do
82
+ describe "Extended Class" do
88
83
 
89
- it "should include Biggs::ActiveRecordAdapter" do
90
- FooBar.included_modules.should be_include(Biggs::ActiveRecordAdapter)
84
+ it "should include Biggs::Concern" do
85
+ FooBar.included_modules.should be_include(Biggs::Concern)
91
86
  end
92
87
 
93
88
  it "should set class value biggs_config" do
@@ -99,7 +94,7 @@ describe "ActiveRecord Class" do
99
94
  end
100
95
  end
101
96
 
102
- describe "ActiveRecord Instance" do
97
+ describe "Extended Class Instance" do
103
98
 
104
99
  describe "Empty" do
105
100
  it "should not have postal_address method" do
@@ -158,7 +153,7 @@ describe "ActiveRecord Instance" do
158
153
  end
159
154
 
160
155
  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")
156
+ FooBarMultiple.new.postal_address_two.should eql("STREET\nCITY STATE ZIP")
162
157
  end
163
158
  end
164
159
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biggs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Munz
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2022-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activerecord
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -100,13 +100,14 @@ files:
100
100
  - formats.yml
101
101
  - init.rb
102
102
  - lib/biggs.rb
103
- - lib/biggs/activerecord.rb
103
+ - lib/biggs/concern.rb
104
+ - lib/biggs/extractor.rb
104
105
  - lib/biggs/format.rb
105
106
  - lib/biggs/formatter.rb
106
107
  - lib/biggs/version.rb
107
108
  - spec/spec_helper.rb
108
- - spec/unit/activerecord_spec.rb
109
109
  - spec/unit/biggs_spec.rb
110
+ - spec/unit/concern_spec.rb
110
111
  - spec/unit/format_spec.rb
111
112
  homepage: https://github.com/yolk/biggs
112
113
  licenses: []
@@ -133,6 +134,6 @@ summary: biggs is a small ruby gem/rails plugin for formatting postal addresses
133
134
  over 60 countries.
134
135
  test_files:
135
136
  - spec/spec_helper.rb
136
- - spec/unit/activerecord_spec.rb
137
137
  - spec/unit/biggs_spec.rb
138
+ - spec/unit/concern_spec.rb
138
139
  - spec/unit/format_spec.rb
@@ -1,64 +0,0 @@
1
- require 'active_support/core_ext/class/attribute'
2
-
3
- module Biggs
4
- module ActiveRecordAdapter
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- class_attribute :biggs_config, default: {}
9
- end
10
-
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
20
-
21
- value_methods = {}
22
- Biggs::Formatter::FIELDS.each do |field|
23
- value_methods[field] = options.delete(field) if options[field]
24
- end
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
- }
32
- end
33
- end
34
-
35
- module Helpers
36
- def self.biggs_values(instance, value_methods)
37
- values = {}
38
- (Biggs::Formatter::FIELDS - [:country]).each do |field|
39
- values[field] = self.biggs_get_value(instance, field, value_methods)
40
- end
41
- values
42
- end
43
-
44
- def self.biggs_get_value(instance, field, value_methods)
45
- key = value_methods[field] || field
46
-
47
- case key
48
- when Symbol
49
- instance.send(key.to_sym)
50
- when Proc
51
- key.call(instance).to_s
52
- when Array
53
- if key.all?{|it| it.is_a?(Symbol) }
54
- key.map{|method| instance.send(method) }.reject(&:blank?).join("\n")
55
- else
56
- raise "Biggs: Can't handle #{field} type Array with non-symbolic entries"
57
- end
58
- else
59
- raise "Biggs: Can't handle #{field} type #{key.class}"
60
- end
61
- end
62
- end
63
- end
64
- end