snail 2.0.0 → 2.1.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: 262c3219f21ad619b858424c360885d11d3f1522
4
- data.tar.gz: c6f39b55987d70ac3715c808e6c1b2aa45e8a747
3
+ metadata.gz: 812732750415ef2957431d92fcf8da7660d78402
4
+ data.tar.gz: 1b71a4117e610bd5c67db6b8b139c7235c9bc7b7
5
5
  SHA512:
6
- metadata.gz: d6269557deae28f0d52d330824fdc99123e447e95937185412761a61782518ab6f195491e19f86017649b3421420b5ba66fe2f931e6975ff2aa34e60f5bb66c7
7
- data.tar.gz: d1a6b9356bf78d26f5480cf65be7a8bdb26dbd57c5e1c7f3c33354ef5ca2556e2a784bfdac932c4cd2c0c61755f162e0ff1272ac82e0ff4d562f0b0b11d88910
6
+ metadata.gz: 80159381c9ad565e003cd5f001f3ad7d0125f05378235301b35306b71679bb363602538910d7dfc8f99138892c586eee8efdc61a75ce1ade8950137f18f8c32b
7
+ data.tar.gz: 2d95a236f10890ec2c1fb570e70fae2aeb6df23de9b1789a8df715325180654668aa1cb4eae07a18ef0bc6b6876cabd6c3e1942e49e7307e06a644582a157b47
data/README.md CHANGED
@@ -48,6 +48,23 @@ Snail.new(
48
48
  => "Jon Doe\n12345 Somewhere Ln\nBENTLEY WA 6102"
49
49
  ```
50
50
 
51
+ You can override this default behavior by specifying `with_country` as `true` or `false`:
52
+
53
+ ```ruby
54
+ Snail.home_country = "Australia"
55
+ Snail.new(
56
+ :name => "Jon Doe",
57
+ :line_1 => "12345 Somewhere Ln",
58
+ :line_2 => nil,
59
+ :city => "Bentley",
60
+ :region => "WA",
61
+ :postal_code => "6102",
62
+ :country => "AU"
63
+ ).to_s(with_country: true)
64
+
65
+ => "Jon Doe\n12345 Somewhere Ln\nBentley WA 6102\nAUSTRALIA"
66
+ ```
67
+
51
68
  See the test cases for more.
52
69
 
53
70
  ## Fun Times
@@ -5,7 +5,6 @@ require 'snail/helpers'
5
5
 
6
6
  require 'cgi'
7
7
  require 'active_support/core_ext/string/output_safety'
8
- require 'yaml'
9
8
 
10
9
  class Snail
11
10
  include Snail::Initializable
@@ -90,12 +89,24 @@ class Snail
90
89
  @origin ||= Snail.home_country
91
90
  end
92
91
 
93
- def to_s
94
- [name, line_1, line_2, city_line, country_line].select{|line| !(line.nil? or line.empty?)}.join("\n")
92
+ def international?
93
+ country && self.origin != country
95
94
  end
96
95
 
97
- def to_html
98
- CGI.escapeHTML(to_s).gsub("\n", '<br />').html_safe
96
+ def to_s(with_country: nil)
97
+ with_country = true if with_country.nil? && international?
98
+
99
+ [
100
+ name,
101
+ line_1,
102
+ line_2,
103
+ city_line,
104
+ (country_line if with_country)
105
+ ].select{|line| !(line.nil? or line.empty?)}.join("\n")
106
+ end
107
+
108
+ def to_html(with_country: nil)
109
+ CGI.escapeHTML(to_s(with_country: with_country)).gsub("\n", '<br />').html_safe
99
110
  end
100
111
 
101
112
  # this method will get much larger. completeness is out of my scope at this time.
@@ -151,9 +162,7 @@ class Snail
151
162
  end
152
163
 
153
164
  def country_line
154
- if country and self.origin != country
155
- (translated_country(self.origin, country) || translated_country("US", country))
156
- end
165
+ (translated_country(self.origin, @country) || translated_country("US", @country))
157
166
  end
158
167
 
159
168
  def translated_country(origin, country)
@@ -1,3 +1,3 @@
1
1
  class Snail
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -72,20 +72,48 @@ class SnailTest < Snail::TestCase
72
72
  assert s.city_line.match(/NY 12345/)
73
73
  end
74
74
 
75
- test "does not include country name for domestic addresses" do
75
+ test "does not include country name for domestic addresses by default" do
76
76
  s = Snail.new(@us.merge(:origin => 'US'))
77
77
  assert !s.to_s.match(/United States/i)
78
78
  s = Snail.new(@ca.merge(:origin => 'CA'))
79
79
  assert !s.to_s.match(/Canada/i)
80
80
  end
81
81
 
82
- test "includes country name for international addresses" do
82
+ test "includes country name for domestic addresses if with_country parameter is true" do
83
+ s = Snail.new(@us.merge(:origin => 'US'))
84
+ assert s.to_s(with_country: true).match(/United States/i)
85
+ s = Snail.new(@ca.merge(:origin => 'CA'))
86
+ assert s.to_s(with_country: true).match(/Canada/i)
87
+ end
88
+
89
+ test "does not include country name for domestic addresses if with_country parameter is false" do
90
+ s = Snail.new(@us.merge(:origin => 'US'))
91
+ assert !s.to_s(with_country: false).match(/United States/i)
92
+ s = Snail.new(@ca.merge(:origin => 'CA'))
93
+ assert !s.to_s(with_country: false).match(/Canada/i)
94
+ end
95
+
96
+ test "includes country name for international addresses by default" do
83
97
  s = Snail.new(@us.merge(:origin => 'CA'))
84
98
  assert s.to_s.match(/United States/i)
85
99
  s = Snail.new(@ca.merge(:origin => 'US'))
86
100
  assert s.to_s.match(/Canada/i)
87
101
  end
88
102
 
103
+ test "includes country name for international addresses if with_country parameter is true" do
104
+ s = Snail.new(@us.merge(:origin => 'CA'))
105
+ assert s.to_s(with_country: true).match(/United States/i)
106
+ s = Snail.new(@ca.merge(:origin => 'US'))
107
+ assert s.to_s(with_country: true).match(/Canada/i)
108
+ end
109
+
110
+ test "does not include country name for international addresses if with_country parameter is false" do
111
+ s = Snail.new(@us.merge(:origin => 'CA'))
112
+ assert !s.to_s(with_country: false).match(/United States/i)
113
+ s = Snail.new(@ca.merge(:origin => 'US'))
114
+ assert !s.to_s(with_country: false).match(/Canada/i)
115
+ end
116
+
89
117
  test "includes translated country name for international addresses" do
90
118
  s = Snail.new(@us.merge(:origin => 'FR'))
91
119
  assert s.to_s.match(/ÉTATS-UNIS/i)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snail
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lance Ivy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-13 00:00:00.000000000 Z
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport