biggs 0.4.0 → 0.5.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: 1a72bbb059021e59607a682f9fbb7afb229c8eb36a4ac7b08ba8cf867455f050
4
+ data.tar.gz: acec60971691c1ca1f72380e7ac1b81bb49ff14b02db4bea188a7a8ac57c5451
5
5
  SHA512:
6
- metadata.gz: 891ec94ea1236687a03296b4904740b5b28f391bc5de769ad5d78d6f9cb4fd3225399823c60e1de18145706b29940d0cea0a556cc1ec2b5f0a4a832645c14f92
7
- data.tar.gz: 10383285215245585fca93d53377804c759beb43484f333421e7874768455dfbdb47b117885c466708a45f1501a0d410890c725ae0871db6c476553ac63b2205
6
+ metadata.gz: 1399a47f9f556a04f1362ac940b12965d233cc628d5197e7800f24c42400390b369dbcf88e20e2f16424638b704f83df9ace8d42420f213eb197e9618f4a457b
7
+ data.tar.gz: 67ed056b8cf7ace58a9426f493816cbb1575cd2af0c4c86bc1543a599246fd27725503c8035bef493ff4c7e51f910f037892a9dcf64a5831a040368eefe4a2cb
data/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ### dev
2
2
 
3
+ ### 0.5.0 / 2022-04-08
4
+
5
+ * Refactored Biggs::Concern to use Biggs::Extractor
6
+ * Removed activerecord dependency and use only activesupport instead
7
+ * Renamed Biggs::ActiveRecordAdapter to Biggs::Concern, include with 'include Biggs'
8
+ * Optimization: Added Biggs::Formatter::FIELDS_WO_COUNTRY
9
+ * Moved Biggs.formats and Biggs.country_names to Constants Biggs::FORMATS & Biggs::COUNTRY_NAMES
10
+
3
11
  ### 0.4.0 / 2022-04-08
4
12
 
5
13
  * [BREAKING]: Do not include to ActiveRecord::Base per default, use 'include Biggs::ActiveRecordAdapter' in your classes
data/README.md CHANGED
@@ -40,7 +40,7 @@ With the data from the above example this would return:
40
40
  ### Usage with Rails and ActiveRecord
41
41
 
42
42
  Address < ActiveRecord::Base
43
- include Biggs::ActiveRecordAdapter
43
+ include Biggs
44
44
 
45
45
  biggs :postal_address
46
46
  end
@@ -50,7 +50,7 @@ This adds the method postal_address to your Address-model, and assumes the prese
50
50
  You can customize the method-names biggs will use by passing in a hash of options:
51
51
 
52
52
  Address < ActiveRecord::Base
53
- include Biggs::ActiveRecordAdapter
53
+ include Biggs
54
54
 
55
55
  biggs :postal_address,
56
56
  :zip => :postal_code,
@@ -63,7 +63,7 @@ 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
66
+ include Biggs
67
67
 
68
68
  biggs :postal_address,
69
69
  :recipient => [:company_name, :person_name]
@@ -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,37 @@
1
+ module Biggs
2
+ class Extractor
3
+ def initialize(options)
4
+ @value_methods = {}
5
+ Biggs::Formatter::FIELDS.each do |field|
6
+ @value_methods[field] = options.delete(field) if options[field]
7
+ end
8
+ end
9
+
10
+ def get_values(instance)
11
+ values = {}
12
+ Biggs::Formatter::FIELDS_WO_COUNTRY.each do |field|
13
+ values[field] = get_value(instance, field)
14
+ end
15
+ values
16
+ end
17
+
18
+ def get_value(instance, field)
19
+ key = @value_methods[field] || field
20
+
21
+ case key
22
+ when Symbol
23
+ instance.send(key.to_sym)
24
+ when Proc
25
+ key.call(instance).to_s
26
+ when Array
27
+ if key.all?{|it| it.is_a?(Symbol) }
28
+ key.map{|method| instance.send(method) }.reject(&:blank?).join("\n")
29
+ else
30
+ raise "Biggs: Can't handle #{field} type Array with non-symbolic entries"
31
+ end
32
+ else
33
+ raise "Biggs: Can't handle #{field} type #{key.class}"
34
+ end
35
+ end
36
+ end
37
+ 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
22
  format_string.gsub(/\n$/, "")
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.5.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,7 +72,7 @@ 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
78
  country: :alt_country
@@ -86,8 +84,8 @@ end
86
84
 
87
85
  describe "ActiveRecord Class" do
88
86
 
89
- it "should include Biggs::ActiveRecordAdapter" do
90
- FooBar.included_modules.should be_include(Biggs::ActiveRecordAdapter)
87
+ it "should include Biggs::Concern" do
88
+ FooBar.included_modules.should be_include(Biggs::Concern)
91
89
  end
92
90
 
93
91
  it "should set class value biggs_config" do
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.5.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