Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 9ce1cc38df13c4b88a8fd93615318b5b26214bfe 0 parents
@binarylogic authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Ben Johnson of Binary Logic (binarylogic.com)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 Manifest
@@ -0,0 +1,9 @@
+init.rb
+lib/addresslogic/version.rb
+lib/addresslogic.rb
+MIT-LICENSE
+Rakefile
+README.rdoc
+test/addresslogic_tests.rb
+test/test_helper.rb
+Manifest
50 README.rdoc
@@ -0,0 +1,50 @@
+= Address Logic
+
+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:
+
+ # some HAML view
+ = address.street1
+ %br
+ - if !address.street2.blank?
+ = address.street2
+ %br
+ == #{address.city}, #{address.state} #{address.zip}
+ %br
+ = address.country
+
+That's just ugly. Instead, you can do this with Address Logic:
+
+ = address.address_parts.join("<br />")
+
+Or, what about a single line address?
+
+ = address.address_parts.join(", ")
+
+Maybe you only want city and state:
+
+ = address.address_parts(:only => [:city, :state]).join(", ")
+
+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.
+
+== Install and use
+
+As a gem:
+
+ $ sudo gem install addresslogic
+
+Or as a plugin
+
+ $ sudo script/plugin install url
+
+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.
+
+ class Address
+ include Addresslogic
+ end
+
+== Helpful links
+
+* <b>Documentation:</b> http://addresslogic.rubyforge.org
+
+
+Copyright (c) 2009 Ben Johnson of [Binary Logic](http://www.binarylogic.com), released under the MIT license
13 Rakefile
@@ -0,0 +1,13 @@
+require 'rubygems'
+require File.dirname(__FILE__) << "/lib/addresslogic/version"
+require 'echoe'
+
+Echoe.new 'addresslogic' do |p|
+ p.version = Addresslogic::Version::STRING
+ p.author = "Ben Johnson of Binary Logic"
+ p.email = 'bjohnson@binarylogic.com'
+ p.project = 'addresslogic'
+ p.summary = "Tools for displaying addresses"
+ p.url = "http://github.com/binarylogic/addresslogic"
+ p.dependencies = %w(echoe)
+end
2  init.rb
@@ -0,0 +1,2 @@
+require "addresslogic/version"
+require "addresslogic"
42 lib/addresslogic.rb
@@ -0,0 +1,42 @@
+# = Address Logic
+#
+# 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)
+# methods. Just include it into your class like so:
+#
+# class Address
+# include AddressLogic
+# end
+#
+# This adds a sigle method: address_parts. More on this method below...
+module Addresslogic
+ # Returns the parts of an address in an array. Example:
+ #
+ # ["Street1", "Street2", "City, State, Zip", "Country"]
+ #
+ # This makes displaying addresses on your view pretty simple:
+ #
+ # address.address_parts.join("<br />")
+ #
+ # === Options
+ #
+ # * <tt>only:</tt> fields you want included in the result
+ # * <tt>except:</tt> any fields you want excluded from the result
+ def address_parts(options = {})
+ options[:only] = [options[:only]] if options[:only] && !options[:only].is_a?(Array)
+ options[:except] = [options[:except]] if options[:except] && !options[:except].is_a?(Array)
+
+ parts = {}
+ [:street1, :street2, :city, :state, :zip, :country].each do |part|
+ next if !respond_to?(part)
+ value = send(part)
+ next if value.to_s.strip == "" || (options[:only] && !options[:only].include?(part)) || (options[:except] && options[:except].include?(part))
+ parts[part] = value
+ end
+
+ state_parts = [parts[:state], parts[:zip]].compact.join(" ")
+ state_parts = nil if state_parts.strip == ""
+ city_parts = [parts[:city], state_parts].compact.join(", ")
+ city_parts = nil if city_parts.strip == ""
+ [parts[:street1], parts[:street2], city_parts, parts[:country]].compact
+ end
+end
56 lib/addresslogic/version.rb
@@ -0,0 +1,56 @@
+module Addresslogic
+ # = Version
+ #
+ # A class for describing the current version of a library. The version
+ # consists of three parts: the +major+ number, the +minor+ number, and the
+ # +tiny+ (or +patch+) number.
+ class Version
+
+ include Comparable
+
+ # A convenience method for instantiating a new Version instance with the
+ # given +major+, +minor+, and +tiny+ components.
+ def self.[](major, minor, tiny)
+ new(major, minor, tiny)
+ end
+
+ attr_reader :major, :minor, :tiny
+
+ # Create a new Version object with the given components.
+ def initialize(major, minor, tiny)
+ @major, @minor, @tiny = major, minor, tiny
+ end
+
+ # Compare this version to the given +version+ object.
+ def <=>(version)
+ to_i <=> version.to_i
+ end
+
+ # Converts this version object to a string, where each of the three
+ # version components are joined by the '.' character. E.g., 2.0.0.
+ def to_s
+ @to_s ||= [@major, @minor, @tiny].join(".")
+ end
+
+ # Converts this version to a canonical integer that may be compared
+ # against other version objects.
+ def to_i
+ @to_i ||= @major * 1_000_000 + @minor * 1_000 + @tiny
+ end
+
+ def to_a
+ [@major, @minor, @tiny]
+ end
+
+ MAJOR = 1
+ MINOR = 0
+ TINY = 0
+
+ # The current version as a Version instance
+ CURRENT = new(MAJOR, MINOR, TINY)
+ # The current version as a String
+ STRING = CURRENT.to_s
+
+ end
+
+end
45 test/test_addresslogic.rb
@@ -0,0 +1,45 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class TestAddresslogix < Test::Unit::TestCase
+ def test_address_parts
+ assert_equal ["Street1", "Street2", "City, State 22933", "United States of America"], address.address_parts
+
+ address.street2 = ""
+ assert_equal ["Street1", "City, State 22933", "United States of America"], address.address_parts
+
+ address.state = ""
+ assert_equal ["Street1", "City, 22933", "United States of America"], address.address_parts
+
+ address.country = ""
+ assert_equal ["Street1", "City, 22933"], address.address_parts
+
+ address.city = ""
+ assert_equal ["Street1", "22933"], address.address_parts
+
+ address.street1 = ""
+ assert_equal ["22933"], address.address_parts
+
+ address.zip = ""
+ assert_equal [], address.address_parts
+ end
+
+ def test_options
+ assert_equal ["Street2"], address.address_parts(:only => :street2)
+ assert_equal ["Street1", "Street2"], address.address_parts(:only => [:street1, :street2])
+ assert_equal ["Street1", "City, State 22933", "United States of America"], address.address_parts(:except => :street2)
+ assert_equal ["City, State 22933", "United States of America"], address.address_parts(:except => [:street1, :street2])
+ end
+
+ private
+ def address
+ return @address if @address
+ @address = Address.new
+ @address.street1 = "Street1"
+ @address.street2 = "Street2"
+ @address.city = "City"
+ @address.state = "State"
+ @address.zip = "22933"
+ @address.country = "United States of America"
+ @address
+ end
+end
9 test/test_helper.rb
@@ -0,0 +1,9 @@
+require "test/unit"
+require "rubygems"
+require "ruby-debug"
+require File.dirname(__FILE__) + "/../lib/addresslogic"
+
+class Address
+ attr_accessor :street1, :street2, :city, :state, :zip, :country
+ include Addresslogic
+end
Please sign in to comment.
Something went wrong with that request. Please try again.