somewhere 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in somewhere.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,32 @@
1
+ == Info ===========================================================
2
+ This gem was developed by Jonathan Martin for use in a number of Rails projects. I tend to use addresses all over the place, so this gem handles all the nitpicky class level details for an address object.
3
+
4
+ Be sure to check out www.nybblr.com for other Rails goodies, and if you're looking for more of my gems (when I get around to it!) go to nybblr.com/gems.
5
+
6
+ == Why Somewhere? ====================================================
7
+ If you're not somewhere, you're nowhere. That's a different gem.
8
+
9
+ == Usage ==========================================================
10
+ From your ActiveRecord model, simply call the address method:
11
+
12
+ class User < ActiveRecord::Base
13
+ # Example usages
14
+ address
15
+
16
+ address :billing
17
+
18
+ address :billing, :postal_code => :zip, :include_prefix => false
19
+
20
+ end
21
+
22
+ Once you instantiate an Address object, you can convert to a hash:
23
+
24
+ address.to_hash
25
+ address.to_hash :exclude => [:country]
26
+
27
+ Or you can convert to a string:
28
+
29
+ address.to_s
30
+ address.to_s :country => false
31
+ address.to_s :delimiter => "\n"
32
+ address.to_s :multiline
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/TODO ADDED
@@ -0,0 +1 @@
1
+ Update specs
@@ -0,0 +1,78 @@
1
+ class Address
2
+ attr_accessor :label
3
+ attr_accessor :street_1
4
+ attr_accessor :street_2
5
+ attr_accessor :city
6
+ attr_accessor :state
7
+ attr_accessor :postal_code
8
+ attr_accessor :country
9
+
10
+ alias_attribute :zip, :postal_code
11
+
12
+ def initialize(*args)
13
+ case args.first
14
+ when Hash
15
+ fields = args.first
16
+ self.label = fields[:label]
17
+ self.street_1 = fields[:street_1]
18
+ self.street_2 = fields[:street_2]
19
+ self.city = fields[:city]
20
+ self.state = fields[:state]
21
+ self.postal_code = fields[:postal_code] || fields[:zip]
22
+ self.country = fields[:country]
23
+ else
24
+ # Constructor for Rails mapper
25
+ self.label = args[0]
26
+ self.street_1 = args[1]
27
+ self.street_2 = args[2]
28
+ self.city = args[3]
29
+ self.state = args[4]
30
+ self.postal_code = args[5]
31
+ self.country = args[6]
32
+ end
33
+ end
34
+
35
+ def to_s(format=nil)
36
+ options = {:label => true, :country => true, :delimiter => ", "}
37
+
38
+ case format
39
+ when Hash
40
+ options.merge! format
41
+ when Symbol
42
+ case format
43
+ when :one_line; options[:delimiter] = ", "
44
+ when :multi_line; options[:delimiter] = "\n"
45
+ end
46
+ end
47
+
48
+ fields = []
49
+ fields << :label if options[:label]
50
+ fields += [:street_1, :street_2, :city_state_post]
51
+ fields << :country if options[:country]
52
+
53
+ city_state_post = nil
54
+ unless city.blank? and state.blank? and postal_code.blank?
55
+ city_state_post = "#{city}, #{state} #{postal_code}"
56
+ end
57
+
58
+ fields.map {|a| eval a.to_s }.reject(&:blank?).join options[:delimiter]
59
+ end
60
+
61
+ def to_hash(opts={})
62
+ fields = [:label, :street_1, :street_2, :city, :state, :postal_code, :country]
63
+ options = { :exclude => [] } if opts.blank?
64
+
65
+ if options[:include].present?
66
+ fields = options[:include]
67
+ elsif options[:exclude].present?
68
+ fields -= options[:exclude]
69
+ end
70
+
71
+ hash = {}
72
+ for f in fields do
73
+ hash[f] = send f
74
+ end
75
+
76
+ hash
77
+ end
78
+ end
@@ -0,0 +1,37 @@
1
+ require 'address'
2
+
3
+ module Somewhere
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def address method=nil, opts = {}
10
+ options = { :include_prefix => true, :label => false, :country => true }
11
+ options.merge! opts
12
+
13
+ if method.blank?
14
+ options[:include_prefix] = false
15
+ method = :address
16
+ end
17
+
18
+ mapping = [:label, :street_1, :street_2, :city, :state, :postal_code, :country]
19
+
20
+ mapping.collect! { |f|
21
+ options[f] ||= options[:include_prefix] ? method.to_s+'_#{f}' : f.to_s unless options[f] == false
22
+ [options[f], f.to_s] unless options[f] == false
23
+ }.compact!
24
+
25
+ composed_of method.to_sym,
26
+ :class_name => "Address",
27
+ :allow_nil => true,
28
+ :mapping => mapping,
29
+ :converter => Proc.new { |value| value.respond_to?(:to_address) ? (value.to_address unless value.blank?) : raise(ArgumentError, "Can't convert #{value.class} to Address") }
30
+ end
31
+ end
32
+ end
33
+
34
+ # Include as a model instance method
35
+ class ActiveRecord::Base
36
+ include Somewhere
37
+ end
@@ -0,0 +1,3 @@
1
+ module Somewhere
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/somewhere/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "somewhere"
6
+ s.version = Somewhere::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = "Jonathan Martin"
9
+ s.email = "me@nybblr.com"
10
+ s.homepage = "http://rubygems.org/gems/somewhere"
11
+ s.summary = "Addresses are common. Really common. So why do we settle for 5+ unserialized address attribute fields and custom to_s methods for each model? No more. This gem is simple and to the point: serialize address data to a class, provide some syntactically useful conversion methods, done. Heavily based off my money gem Cratchit."
12
+ s.description = "Serialized address class for use with Rails models."
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "somewhere"
16
+
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
21
+ s.require_path = 'lib'
22
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: somewhere
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonathan Martin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70271651110220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70271651110220
25
+ description: Serialized address class for use with Rails models.
26
+ email: me@nybblr.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - Gemfile
32
+ - README
33
+ - Rakefile
34
+ - TODO
35
+ - lib/address.rb
36
+ - lib/somewhere.rb
37
+ - lib/somewhere/version.rb
38
+ - somewhere.gemspec
39
+ homepage: http://rubygems.org/gems/somewhere
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: 1.3.6
57
+ requirements: []
58
+ rubyforge_project: somewhere
59
+ rubygems_version: 1.8.15
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: ! 'Addresses are common. Really common. So why do we settle for 5+ unserialized
63
+ address attribute fields and custom to_s methods for each model? No more. This gem
64
+ is simple and to the point: serialize address data to a class, provide some syntactically
65
+ useful conversion methods, done. Heavily based off my money gem Cratchit.'
66
+ test_files: []