mooktakim-addresslogic 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,9 @@
1
+ init.rb
2
+ lib/addresslogic/version.rb
3
+ lib/addresslogic.rb
4
+ MIT-LICENSE
5
+ Rakefile
6
+ README.rdoc
7
+ test/addresslogic_tests.rb
8
+ test/test_helper.rb
9
+ Manifest
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.1"
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,2 @@
1
+ require "addresslogic/version"
2
+ require "addresslogic"
@@ -0,0 +1,86 @@
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
+ # Is there a better way to do this??
27
+ class_eval <<-RUBY
28
+ def address_parts_fields
29
+ #{args[:fields].inspect}
30
+ end
31
+ RUBY
32
+ include Addresslogic::InstanceMethods
33
+ end
34
+ end
35
+
36
+ module InstanceMethods
37
+ # Returns the parts of an address in an array. Example:
38
+ #
39
+ # ["Street1", "Street2", "City", "State Zip", "Country"]
40
+ #
41
+ # This makes displaying addresses on your view pretty simple:
42
+ #
43
+ # address.address_parts.join("<br />")
44
+ #
45
+ # === Options
46
+ #
47
+ # * <tt>only:</tt> fields you want included in the result
48
+ # * <tt>except:</tt> any fields you want excluded from the result
49
+ def address_parts(options = {})
50
+ options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
51
+ options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
52
+
53
+ parts = []
54
+ address_parts_fields.each do |part|
55
+ if part.is_a?(Array)
56
+ # We only want to allow 2d arrays
57
+ subparts = []
58
+ part.flatten.each do |subpart|
59
+ next if !respond_to?(subpart)
60
+ value = send(subpart)
61
+ next if value.to_s.blank? || (options[:only] && !options[:only].include?(subpart)) || (options[:except] && options[:except].include?(subpart))
62
+ subparts << value
63
+ end
64
+ parts << subparts unless subparts.compact.empty?
65
+ else
66
+ next if !respond_to?(part)
67
+ value = send(part)
68
+ next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(part)) || (options[:except] && options[:except].include?(part))
69
+ parts << value
70
+ end
71
+ end
72
+
73
+ result = parts.collect do |part|
74
+ if part.is_a?(Array)
75
+ part.collect{|sub| sub.to_s.strip.blank? ? nil : sub}.join(" ")
76
+ else
77
+ part.to_s.strip.blank? ? nil : part
78
+ end
79
+ end
80
+
81
+ return result.compact
82
+ end
83
+ end
84
+ end
85
+
86
+ ActiveRecord::Base.send(:include, Addresslogic)
@@ -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
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestAddresslogix < Test::Unit::TestCase
4
+ def test_address_parts
5
+ assert_equal ["12 Fancy House", "Bond Street", "London W1 8AJ", "United Kingdom"], address.address_parts
6
+
7
+ address.street2 = ""
8
+ assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], address.address_parts
9
+
10
+ address.country = ""
11
+ assert_equal ["12 Fancy House", "London W1 8AJ"], address.address_parts
12
+
13
+ address.city = ""
14
+ assert_equal ["12 Fancy House", "W1 8AJ"], address.address_parts
15
+
16
+ address.street1 = ""
17
+ assert_equal ["W1 8AJ"], address.address_parts
18
+
19
+ address.postcode = ""
20
+ assert_equal [], address.address_parts
21
+ end
22
+
23
+ def test_options
24
+ assert_equal ["Bond Street"], address.address_parts(:only => :street2)
25
+ assert_equal ["12 Fancy House", "Bond Street"], address.address_parts(:only => [:street1, :street2])
26
+ assert_equal ["12 Fancy House", "London W1 8AJ", "United Kingdom"], address.address_parts(:except => :street2)
27
+ assert_equal ["London W1 8AJ", "United Kingdom"], address.address_parts(:except => [:street1, :street2])
28
+ end
29
+
30
+ private
31
+ def address
32
+ return @address if @address
33
+ @address = Address.new
34
+ @address.street1 = "12 Fancy House"
35
+ @address.street2 = "Bond Street"
36
+ @address.city = "London"
37
+ @address.postcode = "W1 8AJ"
38
+ @address.country = "United Kingdom"
39
+ @address
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ require "test/unit"
2
+ require "rubygems"
3
+ require "ruby-debug"
4
+ require File.dirname(__FILE__) + "/../lib/addresslogic"
5
+
6
+ class Address
7
+ attr_accessor :street1, :street2, :city, :postcode, :country
8
+ include Addresslogic
9
+ apply_addresslogic :fields => [:street1, :street2, [:city, :postcode], :country]
10
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mooktakim-addresslogic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
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 -08: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.2.0
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