seamusabshere-common_name 0.1.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Seamus Abshere
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,129 @@
1
+ =common_name
2
+
3
+ DRY up humanize/downcase/underscore/pluralize/to_sym/etc for names that we use all the time.
4
+
5
+ ==Examples
6
+
7
+ >> User.common_name
8
+ => "user"
9
+ >> User.common_plural_symbol
10
+ => :users
11
+ >> User.common_plural_instance
12
+ => "@users"
13
+
14
+ ==Quick start
15
+
16
+ Put this in <tt>config/initializers/common_name.rb</tt>... (it will let you say <tt>BusCompany.common_name</tt> and get the string "bus_company")
17
+
18
+ ActiveRecord::Base.class_eval do
19
+ class << self; def _common_name; name.underscore; end; end
20
+ include CommonName
21
+ end
22
+
23
+ Put this in <tt>app/controllers/application_controller.rb</tt>... (in the CareersController, it will let you say <tt>common_model</tt> and get the class Career)
24
+
25
+ class ApplicationController < ActionController::Base
26
+ class << self; def _common_name; controller_name.singularize; end; end
27
+ include CommonName
28
+ end
29
+
30
+ Put this in <tt>app/helpers/application_helper.rb</tt>... (in careers/show, it will let you say <tt><%= common_plural_title %></tt> and see "Careers")
31
+
32
+ module ApplicationHelper
33
+ CommonName::METHODS.each do |m|
34
+ eval %{ def common_#{m}; controller.common_#{m}; end }
35
+ end
36
+ end
37
+
38
+ Here are all the methods it gives you:
39
+
40
+ common_name => "bus_company"
41
+ common_symbol => :bus_company
42
+ common_instance => "@bus_company"
43
+ common_title => "Bus company"
44
+ common_human => "bus company"
45
+ common_camel => "BusCompany"
46
+
47
+ common_plural => "bus_companies"
48
+ common_plural_symbol => :bus_companies
49
+ common_plural_instance => "@bus_companies"
50
+ common_plural_title => "Bus companies"
51
+ common_plural_human => "bus companies"
52
+ common_plural_camel => "BusCompanies"
53
+
54
+ common_model => BusCompany [the class]
55
+
56
+ ==What it's not
57
+
58
+ It's <em>not</em> a replacement for <tt>humanize</tt>, <tt>pluralize</tt>, etc.
59
+
60
+ >> "bus_company".common_title
61
+ NoMethodError: undefined method 'common_title' for "bus company":String
62
+
63
+ The point is to cover common names of classes and their instances.
64
+
65
+ ==Rationale
66
+
67
+ I don't like chains of humanize/downcase/underscore/pluralize/to_sym, for example:
68
+
69
+ >> BusCompany.name.underscore.humanize.downcase.pluralize
70
+ => "bus companies"
71
+
72
+ So I replaced them with easy-to-remember methods like:
73
+
74
+ >> BusCompany.common_plural_human
75
+ => "bus companies"
76
+
77
+ I also didn't like worrying about .name versus .class.name:
78
+
79
+ >> @bus_company.class.name # @bus_company = BusCompany.new
80
+ => "BusCompany"
81
+ >> @bus_company.name
82
+ => ""
83
+
84
+ So, the <tt>common_name</tt> of a class (<tt>BusCompany</tt>) is always equivalent to the <tt>common_name</tt> of its instances (<tt>@bus_company</tt>):
85
+
86
+ >> BusCompany.common_plural_human == @bus_company.common_plural_human
87
+ => true
88
+
89
+ ==Advanced usage
90
+
91
+ The <tt>_common_name</tt> method should provide an <em>underscored</em> form that will be used to derive other common forms like <tt>common_human</tt> and <tt>common_plural_symbol</tt>.
92
+
93
+ Note that calling <tt>common_<i>X</i></tt> on a class and on its instances will return the same value.
94
+
95
+ I also use it on non-ActiveRecord classes:
96
+
97
+ class FooBar
98
+ class << self; def _common_name; name.underscore; end; end
99
+ include CommonName
100
+ end
101
+
102
+ >> FooBar.common_name
103
+ => "foo_bar"
104
+ >> f.common_name # f = FooBar.new
105
+ => "foo_bar"
106
+
107
+ If you define <tt>_common_plural</tt>, it will be the basis for the plural forms:
108
+
109
+ class Government
110
+ class << self
111
+ def _common_name
112
+ 'government'
113
+ end
114
+ def _common_plural
115
+ 'government'
116
+ end
117
+ end
118
+ include CommonName
119
+ end
120
+
121
+ That way you can enforce uncountability
122
+
123
+ Government.common_name == Government.common_plural
124
+
125
+ without setting a general rule in the Inflector that the word "government" is uncountable.
126
+
127
+ ==Copyright
128
+
129
+ Copyright (c) 2009 Seamus Abshere. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "common_name"
8
+ gem.summary = %Q{DRY up humanize/downcase/underscore/pluralize/to_sym/etc for names that we use all the time.}
9
+ gem.description = %Q{Provides methods like User.common_name (#=> "user") and User.common_plural_symbol (#=> :users) so that you don't have to chain humanize/downcase/etc. etc.'}
10
+ gem.email = "seamus@abshere.net"
11
+ gem.homepage = "http://github.com/seamusabshere/common_name"
12
+ gem.authors = ["Seamus Abshere"]
13
+ gem.rubyforge_project = "common_name"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+
17
+ Jeweler::RubyforgeTasks.new do |rubyforge|
18
+ rubyforge.doc_task = "rdoc"
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/*_test.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+
45
+
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ if File.exist?('VERSION')
52
+ version = File.read('VERSION')
53
+ else
54
+ version = ""
55
+ end
56
+
57
+ rdoc.rdoc_dir = 'rdoc'
58
+ rdoc.title = "common_name #{version}"
59
+ rdoc.rdoc_files.include('README*')
60
+ rdoc.rdoc_files.include('lib/**/*.rb')
61
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{common_name}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Seamus Abshere"]
12
+ s.date = %q{2009-08-18}
13
+ s.description = %q{Provides methods like User.common_name (#=> "user") and User.common_plural_symbol (#=> :users) so that you don't have to chain humanize/downcase/etc. etc.'}
14
+ s.email = %q{seamus@abshere.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "common_name.gemspec",
27
+ "lib/common_name.rb",
28
+ "test/common_name_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/seamusabshere/common_name}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubyforge_project = %q{common_name}
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{DRY up humanize/downcase/underscore/pluralize/to_sym/etc for names that we use all the time.}
37
+ s.test_files = [
38
+ "test/common_name_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
@@ -0,0 +1,86 @@
1
+ # This library comes out of my frustration with seeing lines like
2
+ #
3
+ # BusCompany.name.underscore.humanize.downcase.pluralize #=> "bus companies"
4
+ #
5
+ # all over my application.
6
+ #
7
+ # See <tt>README</tt> for more information.
8
+ #
9
+ # Copyright (c) 2009 Seamus Abshere. See LICENSE for details.
10
+
11
+ require 'rubygems'
12
+ require 'activesupport'
13
+
14
+ module CommonName
15
+ def self.included(klass)
16
+ klass.extend ClassMethods
17
+ end
18
+
19
+ METHODS = %w{
20
+ name symbol instance title human camel
21
+ plural plural_symbol plural_instance plural_title plural_human plural_camel
22
+ model
23
+ }
24
+
25
+ # delegate instance methods to class methods
26
+ METHODS.each do |m|
27
+ eval %{ def common_#{m}; self.class.common_#{m}; end }
28
+ end
29
+
30
+ module ClassMethods
31
+ # "bus_company"
32
+ #
33
+ # Identical to <tt>_common_name</tt>, which you have to provide.
34
+ def common_name; _COMMON_NAME ||= _common_name; end
35
+
36
+ # :bus_company
37
+ def common_symbol; _COMMON_SYMBOL ||= common_name.to_sym; end
38
+
39
+ # "@bus_company"
40
+ #
41
+ # For use with <tt>instance_variable_get</tt>, etc.
42
+ def common_instance; _COMMON_INSTANCE ||= '@' << common_name; end
43
+
44
+ # "Bus company"
45
+ #
46
+ # Microsoft titlecase.
47
+ def common_title; _COMMON_TITLE ||= common_name.humanize; end
48
+
49
+ # "bus company"
50
+ #
51
+ # Unlike <tt>humanize</tt>, this is lowercase.
52
+ def common_human; _COMMON_HUMAN ||= common_title.downcase; end
53
+
54
+ # "BusCompany"
55
+ def common_camel; _COMMON_CAMEL ||= common_name.camelcase; end
56
+
57
+ # BusCompany
58
+ #
59
+ # Already constantized.
60
+ def common_model; _COMMON_MODEL ||= common_camel.constantize; end
61
+
62
+ # "bus_companies"
63
+ #
64
+ # Derived from _common_name, unless you provide _common_plural.
65
+ def common_plural; _COMMON_PLURAL ||= (respond_to?(:_common_plural) ? _common_plural : common_name.pluralize); end
66
+
67
+ # :bus_companies
68
+ def common_plural_symbol; _COMMON_PLURAL_SYMBOL ||= common_plural.to_sym; end
69
+
70
+ # "@bus_companies"
71
+ def common_plural_instance; _COMMON_PLURAL_INSTANCE ||= '@' << common_plural; end
72
+
73
+ # "Bus companies"
74
+ #
75
+ # See +common_title+ for notes on capitalization.
76
+ def common_plural_title; _COMMON_PLURAL_TITLE ||= common_plural.humanize; end
77
+
78
+ # "bus companies"
79
+ #
80
+ # See +common_human+ for notes on capitalization.
81
+ def common_plural_human; _COMMON_PLURAL_HUMAN ||= common_plural_title.downcase; end
82
+
83
+ # "BusCompanies"
84
+ def common_plural_camel; _COMMON_PLURAL_CAMEL ||= common_plural.camelcase; end
85
+ end
86
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ # a pretty normal class
4
+ class BusCompany
5
+ def self._common_name
6
+ name.underscore
7
+ end
8
+ include CommonName
9
+ end
10
+
11
+ # a class where you always want to chop off the _component part and singularize
12
+ class Component
13
+ def self._common_name
14
+ name.underscore.gsub('_component', '').singularize
15
+ end
16
+ include CommonName
17
+ end
18
+ class AutomobilesComponent < Component; end
19
+ class GovernmentComponent < Component
20
+ def self._common_plural
21
+ 'government'
22
+ end
23
+ end
24
+
25
+ class CommonNameTest < Test::Unit::TestCase
26
+ should "have required API method" do
27
+ assert BusCompany.respond_to?(:_common_name)
28
+ assert Component.respond_to?(:_common_name)
29
+ end
30
+
31
+ should "have optional API method" do
32
+ assert !Component.respond_to?(:_common_plural)
33
+ assert GovernmentComponent.respond_to?(:_common_plural)
34
+ end
35
+
36
+ should "have underscored common name" do
37
+ assert !BusCompany._common_name.include?(' ')
38
+ assert !Component._common_name.include?(' ')
39
+ end
40
+
41
+ {
42
+ :common_name => "bus_company",
43
+ :common_symbol => :bus_company,
44
+ :common_instance => "@bus_company",
45
+ :common_title => "Bus company",
46
+ :common_human => "bus company",
47
+ :common_camel => "BusCompany",
48
+ :common_plural => "bus_companies",
49
+ :common_plural_symbol => :bus_companies,
50
+ :common_plural_instance => "@bus_companies",
51
+ :common_plural_title => "Bus companies",
52
+ :common_plural_human => "bus companies",
53
+ :common_plural_camel => "BusCompanies",
54
+ :common_model => BusCompany
55
+ }.each do |method, result|
56
+ should "have #{method} at the class level" do
57
+ assert_equal result, BusCompany.send(method)
58
+ end
59
+
60
+ should "have #{method} at the instance level" do
61
+ assert_equal result, BusCompany.send(method)
62
+ end
63
+ end
64
+
65
+ should "have common name at the class level" do
66
+ assert_equal "component", Component.common_name
67
+ assert_equal "automobile", AutomobilesComponent.common_name
68
+ assert_equal "government", GovernmentComponent.common_name
69
+ end
70
+
71
+ should "have common name at the instance level" do
72
+ assert_equal "component", Component.new.common_name
73
+ assert_equal "automobile", AutomobilesComponent.new.common_name
74
+ assert_equal "government", GovernmentComponent.new.common_name
75
+ end
76
+
77
+ should "have common plural at the class level" do
78
+ assert_equal "components", Component.common_plural
79
+ assert_equal "automobiles", AutomobilesComponent.common_plural
80
+ assert_equal "government", GovernmentComponent.common_plural # uncountable
81
+ end
82
+
83
+ should "have common plural at the instance level" do
84
+ assert_equal "components", Component.new.common_plural
85
+ assert_equal "automobiles", AutomobilesComponent.new.common_plural
86
+ assert_equal "government", GovernmentComponent.new.common_plural # uncountable
87
+ end
88
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'common_name'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seamusabshere-common_name
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Seamus Abshere
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Provides methods like User.common_name (#=> "user") and User.common_plural_symbol (#=> :users) so that you don't have to chain humanize/downcase/etc. etc.'
17
+ email: seamus@abshere.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - common_name.gemspec
33
+ - lib/common_name.rb
34
+ - test/common_name_test.rb
35
+ - test/test_helper.rb
36
+ has_rdoc: false
37
+ homepage: http://github.com/seamusabshere/common_name
38
+ licenses:
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: common_name
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: DRY up humanize/downcase/underscore/pluralize/to_sym/etc for names that we use all the time.
63
+ test_files:
64
+ - test/common_name_test.rb
65
+ - test/test_helper.rb