addresslogic 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/Manifest +9 -0
- data/README.rdoc +50 -0
- data/Rakefile +13 -0
- data/addresslogic.gemspec +38 -0
- data/init.rb +2 -0
- data/lib/addresslogic/version.rb +56 -0
- data/lib/addresslogic.rb +42 -0
- data/test/test_addresslogic.rb +45 -0
- data/test/test_helper.rb +9 -0
- metadata +89 -0
data/MIT-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/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
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
|
+
As a gem:
|
32
|
+
|
33
|
+
$ sudo gem install addresslogic
|
34
|
+
|
35
|
+
Or as a plugin
|
36
|
+
|
37
|
+
$ sudo script/plugin install url
|
38
|
+
|
39
|
+
Then ust 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.
|
40
|
+
|
41
|
+
class Address
|
42
|
+
include Addresslogic
|
43
|
+
end
|
44
|
+
|
45
|
+
== Helpful links
|
46
|
+
|
47
|
+
* <b>Documentation:</b> http://addresslogic.rubyforge.org
|
48
|
+
|
49
|
+
|
50
|
+
Copyright (c) 2009 Ben Johnson of [Binary Logic](http://www.binarylogic.com), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.dirname(__FILE__) << "/lib/addresslogic/version"
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new 'addresslogic' do |p|
|
6
|
+
p.version = Addresslogic::Version::STRING
|
7
|
+
p.author = "Ben Johnson of Binary Logic"
|
8
|
+
p.email = 'bjohnson@binarylogic.com'
|
9
|
+
p.project = 'addresslogic'
|
10
|
+
p.summary = "Tools for displaying addresses"
|
11
|
+
p.url = "http://github.com/binarylogic/addresslogic"
|
12
|
+
p.dependencies = %w(echoe)
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{addresslogic}
|
5
|
+
s.version = "1.0.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Ben Johnson of Binary Logic"]
|
9
|
+
s.date = %q{2009-01-16}
|
10
|
+
s.description = %q{Tools for displaying addresses}
|
11
|
+
s.email = %q{bjohnson@binarylogic.com}
|
12
|
+
s.extra_rdoc_files = ["lib/addresslogic/version.rb", "lib/addresslogic.rb", "README.rdoc"]
|
13
|
+
s.files = ["init.rb", "lib/addresslogic/version.rb", "lib/addresslogic.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.rdoc", "test/test_addresslogic.rb", "test/test_helper.rb", "addresslogic.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/binarylogic/addresslogic}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Addresslogic", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{addresslogic}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Tools for displaying addresses}
|
21
|
+
s.test_files = ["test/test_addresslogic.rb", "test/test_helper.rb"]
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 2
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_runtime_dependency(%q<echoe>, [">= 0"])
|
29
|
+
s.add_development_dependency(%q<echoe>, [">= 0"])
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
32
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
36
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
37
|
+
end
|
38
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Addresslogic
|
2
|
+
# = Version
|
3
|
+
#
|
4
|
+
# A class for describing the current version of a library. The version
|
5
|
+
# consists of three parts: the +major+ number, the +minor+ number, and the
|
6
|
+
# +tiny+ (or +patch+) number.
|
7
|
+
class Version
|
8
|
+
|
9
|
+
include Comparable
|
10
|
+
|
11
|
+
# A convenience method for instantiating a new Version instance with the
|
12
|
+
# given +major+, +minor+, and +tiny+ components.
|
13
|
+
def self.[](major, minor, tiny)
|
14
|
+
new(major, minor, tiny)
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :major, :minor, :tiny
|
18
|
+
|
19
|
+
# Create a new Version object with the given components.
|
20
|
+
def initialize(major, minor, tiny)
|
21
|
+
@major, @minor, @tiny = major, minor, tiny
|
22
|
+
end
|
23
|
+
|
24
|
+
# Compare this version to the given +version+ object.
|
25
|
+
def <=>(version)
|
26
|
+
to_i <=> version.to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
# Converts this version object to a string, where each of the three
|
30
|
+
# version components are joined by the '.' character. E.g., 2.0.0.
|
31
|
+
def to_s
|
32
|
+
@to_s ||= [@major, @minor, @tiny].join(".")
|
33
|
+
end
|
34
|
+
|
35
|
+
# Converts this version to a canonical integer that may be compared
|
36
|
+
# against other version objects.
|
37
|
+
def to_i
|
38
|
+
@to_i ||= @major * 1_000_000 + @minor * 1_000 + @tiny
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_a
|
42
|
+
[@major, @minor, @tiny]
|
43
|
+
end
|
44
|
+
|
45
|
+
MAJOR = 1
|
46
|
+
MINOR = 0
|
47
|
+
TINY = 0
|
48
|
+
|
49
|
+
# The current version as a Version instance
|
50
|
+
CURRENT = new(MAJOR, MINOR, TINY)
|
51
|
+
# The current version as a String
|
52
|
+
STRING = CURRENT.to_s
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/lib/addresslogic.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# = Address Logic
|
2
|
+
#
|
3
|
+
# 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)
|
4
|
+
# methods. Just include it into your class like so:
|
5
|
+
#
|
6
|
+
# class Address
|
7
|
+
# include AddressLogic
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# This adds a sigle method: address_parts. More on this method below...
|
11
|
+
module Addresslogic
|
12
|
+
# Returns the parts of an address in an array. Example:
|
13
|
+
#
|
14
|
+
# ["Street1", "Street2", "City, State, Zip", "Country"]
|
15
|
+
#
|
16
|
+
# This makes displaying addresses on your view pretty simple:
|
17
|
+
#
|
18
|
+
# address.address_parts.join("<br />")
|
19
|
+
#
|
20
|
+
# === Options
|
21
|
+
#
|
22
|
+
# * <tt>only:</tt> fields you want included in the result
|
23
|
+
# * <tt>except:</tt> any fields you want excluded from the result
|
24
|
+
def address_parts(options = {})
|
25
|
+
options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
|
26
|
+
options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
|
27
|
+
|
28
|
+
parts = {}
|
29
|
+
[:street1, :street2, :city, :state, :zip, :country].each do |part|
|
30
|
+
next if !respond_to?(part)
|
31
|
+
value = send(part)
|
32
|
+
next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(part)) || (options[:except] && options[:except].include?(part))
|
33
|
+
parts[part] = value
|
34
|
+
end
|
35
|
+
|
36
|
+
state_parts = [parts[:state], parts[:zip]].compact.join(" ")
|
37
|
+
state_parts = nil if state_parts.strip == ""
|
38
|
+
city_parts = [parts[:city], state_parts].compact.join(", ")
|
39
|
+
city_parts = nil if city_parts.strip == ""
|
40
|
+
[parts[:street1], parts[:street2], city_parts, parts[:country]].compact
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestAddresslogix < Test::Unit::TestCase
|
4
|
+
def test_address_parts
|
5
|
+
assert_equal ["Street1", "Street2", "City, State 22933", "United States of America"], address.address_parts
|
6
|
+
|
7
|
+
address.street2 = ""
|
8
|
+
assert_equal ["Street1", "City, State 22933", "United States of America"], address.address_parts
|
9
|
+
|
10
|
+
address.state = ""
|
11
|
+
assert_equal ["Street1", "City, 22933", "United States of America"], address.address_parts
|
12
|
+
|
13
|
+
address.country = ""
|
14
|
+
assert_equal ["Street1", "City, 22933"], address.address_parts
|
15
|
+
|
16
|
+
address.city = ""
|
17
|
+
assert_equal ["Street1", "22933"], address.address_parts
|
18
|
+
|
19
|
+
address.street1 = ""
|
20
|
+
assert_equal ["22933"], address.address_parts
|
21
|
+
|
22
|
+
address.zip = ""
|
23
|
+
assert_equal [], address.address_parts
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_options
|
27
|
+
assert_equal ["Street2"], address.address_parts(:only => :street2)
|
28
|
+
assert_equal ["Street1", "Street2"], address.address_parts(:only => [:street1, :street2])
|
29
|
+
assert_equal ["Street1", "City, State 22933", "United States of America"], address.address_parts(:except => :street2)
|
30
|
+
assert_equal ["City, State 22933", "United States of America"], address.address_parts(:except => [:street1, :street2])
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def address
|
35
|
+
return @address if @address
|
36
|
+
@address = Address.new
|
37
|
+
@address.street1 = "Street1"
|
38
|
+
@address.street2 = "Street2"
|
39
|
+
@address.city = "City"
|
40
|
+
@address.state = "State"
|
41
|
+
@address.zip = "22933"
|
42
|
+
@address.country = "United States of America"
|
43
|
+
@address
|
44
|
+
end
|
45
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: addresslogic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Johnson of Binary Logic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-16 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: echoe
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: echoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Tools for displaying addresses
|
36
|
+
email: bjohnson@binarylogic.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- lib/addresslogic/version.rb
|
43
|
+
- lib/addresslogic.rb
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- init.rb
|
47
|
+
- lib/addresslogic/version.rb
|
48
|
+
- lib/addresslogic.rb
|
49
|
+
- Manifest
|
50
|
+
- MIT-LICENSE
|
51
|
+
- Rakefile
|
52
|
+
- README.rdoc
|
53
|
+
- test/test_addresslogic.rb
|
54
|
+
- test/test_helper.rb
|
55
|
+
- addresslogic.gemspec
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/binarylogic/addresslogic
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --line-numbers
|
61
|
+
- --inline-source
|
62
|
+
- --title
|
63
|
+
- Addresslogic
|
64
|
+
- --main
|
65
|
+
- README.rdoc
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "1.2"
|
79
|
+
version:
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: addresslogic
|
83
|
+
rubygems_version: 1.3.1
|
84
|
+
signing_key:
|
85
|
+
specification_version: 2
|
86
|
+
summary: Tools for displaying addresses
|
87
|
+
test_files:
|
88
|
+
- test/test_addresslogic.rb
|
89
|
+
- test/test_helper.rb
|