binarylogic-addresslogic 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/CHANGELOG.rdoc +15 -0
- data/LICENSE +20 -0
- data/README.rdoc +60 -0
- data/Rakefile +48 -0
- data/init.rb +1 -0
- data/lib/addresslogic.rb +77 -0
- data/rails/init.rb +1 -0
- data/test/addresslogic_test.rb +75 -0
- data/test/test_helper.rb +16 -0
- metadata +64 -0
data/.gitignore
ADDED
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
== 1.1.2
|
2
|
+
|
3
|
+
* Update defaults to use changes in 1.1.1
|
4
|
+
|
5
|
+
== 1.1.1
|
6
|
+
|
7
|
+
* Clean up code to use recursion and allow fields to be specified like: [:street1, :street2, [:city, [:state, :zip]], :country]. Which is the standard american way of displaying address. Which looks like: [street1, street2, "#{city}, #{state} #{zip}", country]
|
8
|
+
|
9
|
+
== 1.1.0
|
10
|
+
|
11
|
+
* Changed inclusion method to use apply_addresslogic instead of include Addresslogic. This allows you to pass options and specify alternate fields.
|
12
|
+
|
13
|
+
== 1.0.0
|
14
|
+
|
15
|
+
* Initial release
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ben Johnson of Binary Logic (binarylogic.com)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
= Address Logic
|
2
|
+
|
3
|
+
This is a simple library that takes away the annoyances of displaying addresses in a view. Since various parts of an address can sometimes be optional, such as "street2", displaying addresses was not fun:
|
4
|
+
|
5
|
+
# some HAML view
|
6
|
+
= address.street1
|
7
|
+
%br
|
8
|
+
- if !address.street2.blank?
|
9
|
+
= address.street2
|
10
|
+
%br
|
11
|
+
== #{address.city}, #{address.state} #{address.zip}
|
12
|
+
%br
|
13
|
+
= address.country
|
14
|
+
|
15
|
+
That's just ugly. Instead, you can do this with Address Logic:
|
16
|
+
|
17
|
+
= address.address_parts.join("<br />")
|
18
|
+
|
19
|
+
Or, what about a single line address?
|
20
|
+
|
21
|
+
= address.address_parts.join(", ")
|
22
|
+
|
23
|
+
Maybe you only want city and state:
|
24
|
+
|
25
|
+
= address.address_parts(:only => [:city, :state]).join(", ")
|
26
|
+
|
27
|
+
Granted the above is probably easier without using AddressLogic, but it will purge any blank items in the address, so if city or state are optional then it would be cleaner to use AddressLogic.
|
28
|
+
|
29
|
+
== Install and use
|
30
|
+
|
31
|
+
Install from rubyforge
|
32
|
+
|
33
|
+
$ sudo gem install addresslogic
|
34
|
+
|
35
|
+
Install from github:
|
36
|
+
|
37
|
+
$ sudo gem install binarylogic-addresslogic
|
38
|
+
|
39
|
+
Or as a plugin
|
40
|
+
|
41
|
+
$ sudo script/plugin install git://github.com/binarylogic/addresslogic.git
|
42
|
+
|
43
|
+
Then just include the AddressLogic module into any class of your choice. All that it assumes is that you have the street1, street2, city, state, zip, country (optional) methods.
|
44
|
+
|
45
|
+
class Address
|
46
|
+
apply_addresslogic
|
47
|
+
end
|
48
|
+
|
49
|
+
You can specify the fields too:
|
50
|
+
|
51
|
+
class Address
|
52
|
+
apply_addresslogic :fields => [:street1, :street2, :city, [:state, :zip], :country]
|
53
|
+
end
|
54
|
+
|
55
|
+
== Helpful links
|
56
|
+
|
57
|
+
* <b>Documentation:</b> http://rdoc.info/projects/binarylogic/addresslogic
|
58
|
+
|
59
|
+
|
60
|
+
Copyright (c) 2009 Ben Johnson of [Binary Logic](http://www.binarylogic.com), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "addresslogic"
|
8
|
+
gem.summary = "Creates a meaningful array of address parts for easy displaying."
|
9
|
+
gem.email = "bjohnson@binarylogic.com"
|
10
|
+
gem.homepage = "http://github.com/binarylogic/addresslogic"
|
11
|
+
gem.authors = ["Ben Johnson of Binary Logic"]
|
12
|
+
gem.rubyforge_project = "addresslogic"
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/testtask'
|
19
|
+
Rake::TestTask.new(:test) do |test|
|
20
|
+
test.libs << 'lib' << 'test'
|
21
|
+
test.pattern = 'test/**/*_test.rb'
|
22
|
+
test.verbose = true
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'rcov/rcovtask'
|
27
|
+
Rcov::RcovTask.new do |test|
|
28
|
+
test.libs << 'test'
|
29
|
+
test.pattern = 'test/**/*_test.rb'
|
30
|
+
test.verbose = true
|
31
|
+
end
|
32
|
+
rescue LoadError
|
33
|
+
task :rcov do
|
34
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
task :default => :test
|
39
|
+
|
40
|
+
begin
|
41
|
+
require 'rake/contrib/sshpublisher'
|
42
|
+
namespace :rubyforge do
|
43
|
+
desc "Release gem to RubyForge"
|
44
|
+
task :release => ["rubyforge:release:gem"]
|
45
|
+
end
|
46
|
+
rescue LoadError
|
47
|
+
puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
|
48
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init.rb"
|
data/lib/addresslogic.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'activerecord'
|
2
|
+
|
3
|
+
# = Address Logic
|
4
|
+
#
|
5
|
+
# This is a simple module that you can include into any classm as long as it has a street1, street2, city, state, zip, and country (optional)
|
6
|
+
# methods. Just include it into your class like so:
|
7
|
+
#
|
8
|
+
# class Address
|
9
|
+
# apply_addresslogic :fields => [:street1, :street2, :city, [:state, :zip], :country]
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# The above will return:
|
13
|
+
# ["Street1", "Street2", "City", "State Zip", "Country"]
|
14
|
+
#
|
15
|
+
# This adds a sigle method: address_parts. More on this method below...
|
16
|
+
module Addresslogic
|
17
|
+
|
18
|
+
def self.included(base)
|
19
|
+
base.extend ClassMethods
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
attr_accessor :address_parts_fields
|
24
|
+
|
25
|
+
def apply_addresslogic(args = {})
|
26
|
+
self.address_parts_fields = args[:fields] || [:street1, :street2, [:city, [:state, :zip]], :country]
|
27
|
+
include Addresslogic::InstanceMethods
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module InstanceMethods
|
32
|
+
# Returns the parts of an address in an array. Example:
|
33
|
+
#
|
34
|
+
# ["Street1", "Street2", "City", "State Zip", "Country"]
|
35
|
+
#
|
36
|
+
# This makes displaying addresses on your view pretty simple:
|
37
|
+
#
|
38
|
+
# address.address_parts.join("<br />")
|
39
|
+
#
|
40
|
+
# === Options
|
41
|
+
#
|
42
|
+
# * <tt>only:</tt> fields you want included in the result
|
43
|
+
# * <tt>except:</tt> any fields you want excluded from the result
|
44
|
+
def address_parts(*args)
|
45
|
+
options = args.extract_options!
|
46
|
+
options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
|
47
|
+
options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
|
48
|
+
fields = args[0] || address_parts_fields
|
49
|
+
level = args[1] || 0
|
50
|
+
|
51
|
+
parts = []
|
52
|
+
fields.each do |field|
|
53
|
+
if field.is_a?(Array)
|
54
|
+
has_sub_array = field.find { |item| item.is_a?(Array) }
|
55
|
+
separator = has_sub_array ? ", " : " "
|
56
|
+
sub_parts = address_parts(field, level + 1, options).join(separator)
|
57
|
+
next if sub_parts.blank?
|
58
|
+
parts << sub_parts
|
59
|
+
else
|
60
|
+
next if !respond_to?(field)
|
61
|
+
value = send(field)
|
62
|
+
next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(field)) || (options[:except] && options[:except].include?(field))
|
63
|
+
parts << value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
parts
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
def address_parts_fields
|
72
|
+
self.class.address_parts_fields
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
ActiveRecord::Base.send(:include, Addresslogic)
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "addresslogic"
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestAddresslogic < Test::Unit::TestCase
|
4
|
+
def test_uk_uk_address_parts
|
5
|
+
assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"], uk_address.address_parts
|
6
|
+
|
7
|
+
uk_address.street2 = ""
|
8
|
+
assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], uk_address.address_parts
|
9
|
+
|
10
|
+
uk_address.country = ""
|
11
|
+
assert_equal ["12 Fancy House", "London W1 8AJ"], uk_address.address_parts
|
12
|
+
|
13
|
+
uk_address.city = ""
|
14
|
+
assert_equal ["12 Fancy House", "W1 8AJ"], uk_address.address_parts
|
15
|
+
|
16
|
+
uk_address.street1 = ""
|
17
|
+
assert_equal ["W1 8AJ"], uk_address.address_parts
|
18
|
+
|
19
|
+
uk_address.zip = ""
|
20
|
+
assert_equal [], uk_address.address_parts
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_american_address_parts
|
24
|
+
assert_equal ["12 Fancy House", "Bond Street", "Tampa, Florida 45334", "United States"], american_address.address_parts
|
25
|
+
|
26
|
+
american_address.street2 = ""
|
27
|
+
assert_equal ["12 Fancy House", "Tampa, Florida 45334", "United States"], american_address.address_parts
|
28
|
+
|
29
|
+
american_address.country = ""
|
30
|
+
assert_equal ["12 Fancy House", "Tampa, Florida 45334"], american_address.address_parts
|
31
|
+
|
32
|
+
american_address.city = ""
|
33
|
+
assert_equal ["12 Fancy House", "Florida 45334"], american_address.address_parts
|
34
|
+
|
35
|
+
american_address.state = ""
|
36
|
+
assert_equal ["12 Fancy House", "45334"], american_address.address_parts
|
37
|
+
|
38
|
+
american_address.street1 = ""
|
39
|
+
assert_equal ["45334"], american_address.address_parts
|
40
|
+
|
41
|
+
american_address.zip = ""
|
42
|
+
assert_equal [], american_address.address_parts
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_options
|
46
|
+
assert_equal ["Bond Street"], uk_address.address_parts(:only => :street2)
|
47
|
+
assert_equal ["12 Fancy House", "Bond Street"], uk_address.address_parts(:only => [:street1, :street2])
|
48
|
+
assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], uk_address.address_parts(:except => :street2)
|
49
|
+
assert_equal ["London W1 8AJ", "United Kingdom"], uk_address.address_parts(:except => [:street1, :street2])
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def uk_address
|
54
|
+
return @uk_address if @uk_address
|
55
|
+
@uk_address = UKAddress.new
|
56
|
+
@uk_address.street1 = "12 Fancy House"
|
57
|
+
@uk_address.street2 = "Bond Street"
|
58
|
+
@uk_address.city = "London"
|
59
|
+
@uk_address.zip = "W1 8AJ"
|
60
|
+
@uk_address.country = "United Kingdom"
|
61
|
+
@uk_address
|
62
|
+
end
|
63
|
+
|
64
|
+
def american_address
|
65
|
+
return @american_address if @american_address
|
66
|
+
@american_address = AmericanAddress.new
|
67
|
+
@american_address.street1 = "12 Fancy House"
|
68
|
+
@american_address.street2 = "Bond Street"
|
69
|
+
@american_address.city = "Tampa"
|
70
|
+
@american_address.zip = "45334"
|
71
|
+
@american_address.state = "Florida"
|
72
|
+
@american_address.country = "United States"
|
73
|
+
@american_address
|
74
|
+
end
|
75
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "rubygems"
|
3
|
+
require "ruby-debug"
|
4
|
+
require File.dirname(__FILE__) + "/../lib/addresslogic"
|
5
|
+
|
6
|
+
class UKAddress
|
7
|
+
attr_accessor :street1, :street2, :city, :zip, :country
|
8
|
+
include Addresslogic
|
9
|
+
apply_addresslogic :fields => [:street1, :street2, [:city, :zip], :country]
|
10
|
+
end
|
11
|
+
|
12
|
+
class AmericanAddress
|
13
|
+
attr_accessor :street1, :street2, :city, :state, :zip, :country
|
14
|
+
include Addresslogic
|
15
|
+
apply_addresslogic :fields => [:street1, :street2, [:city, [:state, :zip]], :country]
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: binarylogic-addresslogic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Johnson of Binary Logic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-28 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: bjohnson@binarylogic.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- CHANGELOG.rdoc
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- init.rb
|
32
|
+
- lib/addresslogic.rb
|
33
|
+
- rails/init.rb
|
34
|
+
- test/addresslogic_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
has_rdoc: false
|
37
|
+
homepage: http://github.com/binarylogic/addresslogic
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project: addresslogic
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Creates a meaningful array of address parts for easy displaying.
|
62
|
+
test_files:
|
63
|
+
- test/addresslogic_test.rb
|
64
|
+
- test/test_helper.rb
|