roda-tags 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +24 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +1083 -0
- data/Rakefile +21 -0
- data/lib/core_ext/blank.rb +158 -0
- data/lib/core_ext/hash.rb +47 -0
- data/lib/core_ext/object.rb +31 -0
- data/lib/core_ext/string.rb +330 -0
- data/lib/roda/plugins/tag_helpers.rb +919 -0
- data/lib/roda/plugins/tags.rb +452 -0
- data/lib/roda/tags.rb +3 -0
- data/lib/roda/tags/version.rb +8 -0
- data/roda-tags.gemspec +49 -0
- metadata +237 -0
data/Rakefile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
2
|
+
require "rake/testtask"
|
|
3
|
+
|
|
4
|
+
Rake::TestTask.new(:spec) do |t|
|
|
5
|
+
t.libs << "spec"
|
|
6
|
+
t.libs << "lib"
|
|
7
|
+
t.test_files = FileList['spec/**/*_spec.rb']
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
task :default => :spec
|
|
11
|
+
|
|
12
|
+
desc "Run specs with coverage"
|
|
13
|
+
task :coverage do
|
|
14
|
+
ENV['COVERAGE'] = '1'
|
|
15
|
+
Rake::Task['spec'].invoke
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
desc "Run Rubocop report"
|
|
19
|
+
task :rubocop do
|
|
20
|
+
`rubocop -f html -o ./Rubocop-report.html lib/`
|
|
21
|
+
end
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
|
|
2
|
+
# reopening Object class
|
|
3
|
+
class Object
|
|
4
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
|
5
|
+
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
|
6
|
+
#
|
|
7
|
+
# This simplifies
|
|
8
|
+
#
|
|
9
|
+
# !address || address.empty?
|
|
10
|
+
#
|
|
11
|
+
# to
|
|
12
|
+
#
|
|
13
|
+
# address.blank?
|
|
14
|
+
#
|
|
15
|
+
# @return [true, false]
|
|
16
|
+
def blank?
|
|
17
|
+
respond_to?(:empty?) ? !!empty? : !self
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# An object is present if it's not blank.
|
|
21
|
+
#
|
|
22
|
+
# @return [true, false]
|
|
23
|
+
def present?
|
|
24
|
+
!blank?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Returns the receiver if it's present otherwise returns +nil+.
|
|
28
|
+
# <tt>object.presence</tt> is equivalent to
|
|
29
|
+
#
|
|
30
|
+
# object.present? ? object : nil
|
|
31
|
+
#
|
|
32
|
+
# For example, something like
|
|
33
|
+
#
|
|
34
|
+
# state = params[:state] if params[:state].present?
|
|
35
|
+
# country = params[:country] if params[:country].present?
|
|
36
|
+
# region = state || country || 'US'
|
|
37
|
+
#
|
|
38
|
+
# becomes
|
|
39
|
+
#
|
|
40
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
|
41
|
+
#
|
|
42
|
+
# @return [Object]
|
|
43
|
+
def presence
|
|
44
|
+
self if present?
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# reopening NilClass class
|
|
49
|
+
class NilClass
|
|
50
|
+
# +nil+ is blank:
|
|
51
|
+
#
|
|
52
|
+
# nil.blank? # => true
|
|
53
|
+
#
|
|
54
|
+
# @return [true]
|
|
55
|
+
def blank?
|
|
56
|
+
true
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# reopening FalseClass class
|
|
62
|
+
class FalseClass
|
|
63
|
+
# +false+ is blank:
|
|
64
|
+
#
|
|
65
|
+
# false.blank? # => true
|
|
66
|
+
#
|
|
67
|
+
# @return [true]
|
|
68
|
+
def blank?
|
|
69
|
+
true
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# reopening TrueClass class
|
|
75
|
+
class TrueClass
|
|
76
|
+
# +true+ is not blank:
|
|
77
|
+
#
|
|
78
|
+
# true.blank? # => false
|
|
79
|
+
#
|
|
80
|
+
# @return [false]
|
|
81
|
+
def blank?
|
|
82
|
+
false
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# reopening Array class
|
|
88
|
+
class Array
|
|
89
|
+
# An array is blank if it's empty:
|
|
90
|
+
#
|
|
91
|
+
# [].blank? # => true
|
|
92
|
+
# [1,2,3].blank? # => false
|
|
93
|
+
#
|
|
94
|
+
# @return [true, false]
|
|
95
|
+
alias_method :blank?, :empty?
|
|
96
|
+
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# reopening Hash class
|
|
100
|
+
class Hash
|
|
101
|
+
# A hash is blank if it's empty:
|
|
102
|
+
#
|
|
103
|
+
# {}.blank? # => true
|
|
104
|
+
# { key: 'value' }.blank? # => false
|
|
105
|
+
#
|
|
106
|
+
# @return [true, false]
|
|
107
|
+
alias_method :blank?, :empty?
|
|
108
|
+
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# reopening String class
|
|
112
|
+
class String
|
|
113
|
+
BLANK_RE = /\A[[:space:]]*\z/
|
|
114
|
+
|
|
115
|
+
# A string is blank if it's empty or contains whitespaces only:
|
|
116
|
+
#
|
|
117
|
+
# ''.blank? # => true
|
|
118
|
+
# ' '.blank? # => true
|
|
119
|
+
# "\t\n\r".blank? # => true
|
|
120
|
+
# ' blah '.blank? # => false
|
|
121
|
+
#
|
|
122
|
+
# Unicode whitespace is supported:
|
|
123
|
+
#
|
|
124
|
+
# "\u00a0".blank? # => true
|
|
125
|
+
#
|
|
126
|
+
# @return [true, false]
|
|
127
|
+
def blank?
|
|
128
|
+
BLANK_RE === self
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# reopening Numeric class
|
|
134
|
+
class Numeric #:nodoc:
|
|
135
|
+
# No number is blank:
|
|
136
|
+
#
|
|
137
|
+
# 1.blank? # => false
|
|
138
|
+
# 0.blank? # => false
|
|
139
|
+
#
|
|
140
|
+
# @return [false]
|
|
141
|
+
def blank?
|
|
142
|
+
false
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# reopening Time class
|
|
148
|
+
class Time #:nodoc:
|
|
149
|
+
# No Time is blank:
|
|
150
|
+
#
|
|
151
|
+
# Time.now.blank? # => false
|
|
152
|
+
#
|
|
153
|
+
# @return [false]
|
|
154
|
+
def blank?
|
|
155
|
+
false
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# :stopdoc:
|
|
2
|
+
class Hash
|
|
3
|
+
|
|
4
|
+
# remove any other version of #to_html_attributes
|
|
5
|
+
undef_method :to_html_attributes if method_defined?(:to_html_attributes)
|
|
6
|
+
|
|
7
|
+
def to_html_attributes(empties = nil)
|
|
8
|
+
hash = self.dup
|
|
9
|
+
hash.reject! { |_k, v| v.blank? } unless empties.nil?
|
|
10
|
+
out = ''
|
|
11
|
+
hash.keys.sort.each do |key| # NB!! sorting output order of attributes alphabetically
|
|
12
|
+
val = hash[key].is_a?(Array) ? hash[key].join('_') : hash[key].to_s
|
|
13
|
+
out << "#{key}=\"#{val}\" "
|
|
14
|
+
end
|
|
15
|
+
out.strip
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
unless method_defined?(:reverse_merge)
|
|
20
|
+
# Merges the caller into +other_hash+. For example,
|
|
21
|
+
#
|
|
22
|
+
# options = options.reverse_merge(size: 25, velocity: 10)
|
|
23
|
+
#
|
|
24
|
+
# is equivalent to
|
|
25
|
+
#
|
|
26
|
+
# options = { size: 25, velocity: 10 }.merge(options)
|
|
27
|
+
#
|
|
28
|
+
# This is particularly useful for initializing an options hash
|
|
29
|
+
# with default values.
|
|
30
|
+
def reverse_merge(other_hash)
|
|
31
|
+
other_hash.merge(self)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
unless method_defined?(:reverse_merge!)
|
|
36
|
+
|
|
37
|
+
# Destructive +reverse_merge+.
|
|
38
|
+
def reverse_merge!(other_hash)
|
|
39
|
+
# right wins if there is no left
|
|
40
|
+
merge!(other_hash) { |_key, left, _right| left }
|
|
41
|
+
end
|
|
42
|
+
alias_method :reverse_update, :reverse_merge!
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
# :startdoc:
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
# reopening Object class
|
|
3
|
+
class Object
|
|
4
|
+
# Returns true if this object is included in the argument. Argument must be
|
|
5
|
+
# any object which responds to +#include?+. Usage:
|
|
6
|
+
#
|
|
7
|
+
# characters = ["Konata", "Kagami", "Tsukasa"]
|
|
8
|
+
# "Konata".in?(characters) # => true
|
|
9
|
+
#
|
|
10
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond
|
|
11
|
+
# to +#include?+.
|
|
12
|
+
def in?(another_object)
|
|
13
|
+
another_object.include?(self)
|
|
14
|
+
rescue NoMethodError
|
|
15
|
+
# raise ArgumentError.new('The parameter passed to #in? must respond to #include?')
|
|
16
|
+
raise(ArgumentError, 'The parameter passed to #in? must respond to #include?')
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns the receiver if it's included in the argument otherwise returns +nil+.
|
|
20
|
+
# Argument must be any object which responds to +#include?+. Usage:
|
|
21
|
+
#
|
|
22
|
+
# params[:bucket_type].presence_in %w( project calendar )
|
|
23
|
+
#
|
|
24
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
|
|
25
|
+
#
|
|
26
|
+
# @return [Object]
|
|
27
|
+
def presence_in(another_object)
|
|
28
|
+
self.in?(another_object) ? self : nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
# The inflector extension adds inflection instance methods to String, which allows the easy
|
|
2
|
+
# transformation of words from singular to plural, class names to table names, modularized class
|
|
3
|
+
# names to ones without, and class names to foreign keys. It exists for
|
|
4
|
+
# backwards compatibility to legacy Sequel code.
|
|
5
|
+
#
|
|
6
|
+
# To load the extension:
|
|
7
|
+
class String
|
|
8
|
+
# This module acts as a singleton returned/yielded by String.inflections,
|
|
9
|
+
# which is used to override or specify additional inflection rules. Examples:
|
|
10
|
+
#
|
|
11
|
+
# String.inflections do |inflect|
|
|
12
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
|
13
|
+
# inflect.singular /^(ox)en/i, '\1'
|
|
14
|
+
#
|
|
15
|
+
# inflect.irregular 'octopus', 'octopi'
|
|
16
|
+
#
|
|
17
|
+
# inflect.uncountable "equipment"
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# New rules are added at the top. So in the example above, the irregular rule for octopus will
|
|
21
|
+
# now be the first of the pluralization and singularization rules that is runs. This guarantees
|
|
22
|
+
# that your rules run before any of the rules that may already have been loaded.
|
|
23
|
+
module Inflections
|
|
24
|
+
@plurals = []
|
|
25
|
+
@singulars = []
|
|
26
|
+
@uncountables = []
|
|
27
|
+
|
|
28
|
+
# Proc that is instance evaled to create the default inflections for both the
|
|
29
|
+
# model inflector and the inflector extension.
|
|
30
|
+
DEFAULT_INFLECTIONS_PROC = proc do
|
|
31
|
+
plural(/$/, 's')
|
|
32
|
+
plural(/s$/i, 's')
|
|
33
|
+
plural(/(alias|(?:stat|octop|vir|b)us)$/i, '\1es')
|
|
34
|
+
plural(/(buffal|tomat)o$/i, '\1oes')
|
|
35
|
+
plural(/([ti])um$/i, '\1a')
|
|
36
|
+
plural(/sis$/i, 'ses')
|
|
37
|
+
plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
|
|
38
|
+
plural(/(hive)$/i, '\1s')
|
|
39
|
+
plural(/([^aeiouy]|qu)y$/i, '\1ies')
|
|
40
|
+
plural(/(x|ch|ss|sh)$/i, '\1es')
|
|
41
|
+
plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
|
|
42
|
+
plural(/([m|l])ouse$/i, '\1ice')
|
|
43
|
+
|
|
44
|
+
singular(/s$/i, '')
|
|
45
|
+
singular(/([ti])a$/i, '\1um')
|
|
46
|
+
singular(/(analy|ba|cri|diagno|parenthe|progno|synop|the)ses$/i, '\1sis')
|
|
47
|
+
singular(/([^f])ves$/i, '\1fe')
|
|
48
|
+
singular(/([h|t]ive)s$/i, '\1')
|
|
49
|
+
singular(/([lr])ves$/i, '\1f')
|
|
50
|
+
singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
|
51
|
+
singular(/(m)ovies$/i, '\1ovie')
|
|
52
|
+
singular(/(x|ch|ss|sh)es$/i, '\1')
|
|
53
|
+
singular(/([m|l])ice$/i, '\1ouse')
|
|
54
|
+
singular(/buses$/i, 'bus')
|
|
55
|
+
singular(/oes$/i, 'o')
|
|
56
|
+
singular(/shoes$/i, 'shoe')
|
|
57
|
+
singular(/(alias|(?:stat|octop|vir|b)us)es$/i, '\1')
|
|
58
|
+
singular(/(vert|ind)ices$/i, '\1ex')
|
|
59
|
+
singular(/matrices$/i, 'matrix')
|
|
60
|
+
|
|
61
|
+
irregular('person', 'people')
|
|
62
|
+
irregular('man', 'men')
|
|
63
|
+
irregular('child', 'children')
|
|
64
|
+
irregular('sex', 'sexes')
|
|
65
|
+
irregular('move', 'moves')
|
|
66
|
+
irregular('quiz', 'quizzes')
|
|
67
|
+
irregular('testis', 'testes')
|
|
68
|
+
|
|
69
|
+
uncountable(%w(equipment information rice money species series fish sheep news))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class << self
|
|
74
|
+
# Array of 2 element arrays, first containing a regex, and the second containing a
|
|
75
|
+
# substitution pattern, used for plurization.
|
|
76
|
+
attr_reader :plurals
|
|
77
|
+
|
|
78
|
+
# Array of 2 element arrays, first containing a regex, and the second containing a
|
|
79
|
+
# substitution pattern, used for singularization.
|
|
80
|
+
attr_reader :singulars
|
|
81
|
+
|
|
82
|
+
# Array of strings for words were the singular form is the same as the plural form
|
|
83
|
+
attr_reader :uncountables
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Clears the loaded inflections within a given scope (default is :all). Give the scope as a
|
|
87
|
+
# symbol of the inflection type, the options are: :plurals, :singulars, :uncountables
|
|
88
|
+
#
|
|
89
|
+
# Examples:
|
|
90
|
+
# clear :all
|
|
91
|
+
# clear :plurals
|
|
92
|
+
def self.clear(scope = :all)
|
|
93
|
+
case scope
|
|
94
|
+
when :all
|
|
95
|
+
@plurals = []
|
|
96
|
+
@singulars = []
|
|
97
|
+
@uncountables = []
|
|
98
|
+
else
|
|
99
|
+
instance_variable_set("@#{scope}", [])
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Specifies a new irregular that applies to both pluralization and singularization at the same
|
|
104
|
+
# time. This can only be used for strings, not regular expressions. You simply pass the
|
|
105
|
+
# irregular in singular and plural form.
|
|
106
|
+
#
|
|
107
|
+
# Examples:
|
|
108
|
+
# irregular 'octopus', 'octopi'
|
|
109
|
+
# irregular 'person', 'people'
|
|
110
|
+
def self.irregular(singular, plural)
|
|
111
|
+
plural(Regexp.new("(#{singular[0, 1]})#{singular[1..-1]}$", 'i'), '\1' + plural[1..-1])
|
|
112
|
+
singular(Regexp.new("(#{plural[0, 1]})#{plural[1..-1]}$", 'i'), '\1' + singular[1..-1])
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a
|
|
116
|
+
# regular expression. The replacement should always be a string that may include references to
|
|
117
|
+
# the matched data from the rule.
|
|
118
|
+
#
|
|
119
|
+
# Example:
|
|
120
|
+
# plural(/(x|ch|ss|sh)$/i, '\1es')
|
|
121
|
+
def self.plural(rule, replacement)
|
|
122
|
+
@plurals.insert(0, [rule, replacement])
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Specifies a new singularization rule and its replacement. The rule can either be a string or
|
|
126
|
+
# a regular expression. The replacement should always be a string that may include references
|
|
127
|
+
# to the matched data from the rule.
|
|
128
|
+
#
|
|
129
|
+
# Example:
|
|
130
|
+
# singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
|
131
|
+
def self.singular(rule, replacement)
|
|
132
|
+
@singulars.insert(0, [rule, replacement])
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
|
136
|
+
#
|
|
137
|
+
# Examples:
|
|
138
|
+
# uncountable "money"
|
|
139
|
+
# uncountable "money", "information"
|
|
140
|
+
# uncountable %w( money information rice )
|
|
141
|
+
def self.uncountable(*words)
|
|
142
|
+
(@uncountables << words).flatten!
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Sequel.require('default_inflections', 'model')
|
|
146
|
+
# instance_eval(&Sequel::DEFAULT_INFLECTIONS_PROC)
|
|
147
|
+
# Sequel.require('default_inflections', 'model')
|
|
148
|
+
instance_eval(&DEFAULT_INFLECTIONS_PROC)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Yield the Inflections module if a block is given, and return
|
|
152
|
+
# the Inflections module.
|
|
153
|
+
def self.inflections
|
|
154
|
+
yield Inflections if block_given?
|
|
155
|
+
Inflections
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# By default, camelize converts the string to UpperCamelCase. If the argument to camelize
|
|
159
|
+
# is set to :lower then camelize produces lowerCamelCase.
|
|
160
|
+
#
|
|
161
|
+
# camelize will also convert '/' to '::' which is useful for converting paths to namespaces
|
|
162
|
+
#
|
|
163
|
+
# Examples
|
|
164
|
+
# "active_record".camelize #=> "ActiveRecord"
|
|
165
|
+
# "active_record".camelize(:lower) #=> "activeRecord"
|
|
166
|
+
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
|
167
|
+
# "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
|
|
168
|
+
def camelize(first_letter_in_uppercase = :upper)
|
|
169
|
+
s = gsub(%r{/(.?)}) { |x| "::#{x[-1..-1].upcase unless x == '/'}" }
|
|
170
|
+
.gsub(/(^|_)(.)/) { |x| x[-1..-1].upcase }
|
|
171
|
+
s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
|
|
172
|
+
s
|
|
173
|
+
end
|
|
174
|
+
alias_method :camelcase, :camelize
|
|
175
|
+
|
|
176
|
+
# Singularizes and camelizes the string. Also strips out all characters preceding
|
|
177
|
+
# and including a period (".").
|
|
178
|
+
#
|
|
179
|
+
# Examples
|
|
180
|
+
# "egg_and_hams".classify #=> "EggAndHam"
|
|
181
|
+
# "post".classify #=> "Post"
|
|
182
|
+
# "schema.post".classify #=> "Post"
|
|
183
|
+
def classify
|
|
184
|
+
sub(/.*\./, '').singularize.camelize
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Constantize tries to find a declared constant with the name specified
|
|
188
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
|
189
|
+
# or is not initialized.
|
|
190
|
+
#
|
|
191
|
+
# Examples
|
|
192
|
+
# "Module".constantize #=> Module
|
|
193
|
+
# "Class".constantize #=> Class
|
|
194
|
+
def constantize
|
|
195
|
+
unless m = /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.match(self)
|
|
196
|
+
raise(NameError, "#{inspect} is not a valid constant name!")
|
|
197
|
+
end
|
|
198
|
+
Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Replaces underscores with dashes in the string.
|
|
202
|
+
#
|
|
203
|
+
# Example
|
|
204
|
+
# "puni_puni".dasherize #=> "puni-puni"
|
|
205
|
+
def dasherize
|
|
206
|
+
tr('_', '-')
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Removes the module part from the expression in the string
|
|
210
|
+
#
|
|
211
|
+
# Examples
|
|
212
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
|
213
|
+
# "Inflections".demodulize #=> "Inflections"
|
|
214
|
+
def demodulize
|
|
215
|
+
gsub(/^.*::/, '')
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Creates a foreign key name from a class name.
|
|
219
|
+
# +use_underscore+ sets whether the method should put '_' between the name and 'id'.
|
|
220
|
+
#
|
|
221
|
+
# Examples
|
|
222
|
+
# "Message".foreign_key #=> "message_id"
|
|
223
|
+
# "Message".foreign_key(false) #=> "messageid"
|
|
224
|
+
# "Admin::Post".foreign_key #=> "post_id"
|
|
225
|
+
def foreign_key(use_underscore = true)
|
|
226
|
+
"#{demodulize.underscore}#{'_' if use_underscore}id"
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
|
230
|
+
# Like titleize, this is meant for creating pretty output.
|
|
231
|
+
#
|
|
232
|
+
# Examples
|
|
233
|
+
# "employee_salary" #=> "Employee salary"
|
|
234
|
+
# "author_id" #=> "Author"
|
|
235
|
+
def humanize
|
|
236
|
+
gsub(/_id$/, '').tr('_', ' ').capitalize
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Returns the plural form of the word in the string.
|
|
240
|
+
#
|
|
241
|
+
# Examples
|
|
242
|
+
# "post".pluralize #=> "posts"
|
|
243
|
+
# "octopus".pluralize #=> "octopi"
|
|
244
|
+
# "sheep".pluralize #=> "sheep"
|
|
245
|
+
# "words".pluralize #=> "words"
|
|
246
|
+
# "the blue mailman".pluralize #=> "the blue mailmen"
|
|
247
|
+
# "CamelOctopus".pluralize #=> "CamelOctopi"
|
|
248
|
+
def pluralize
|
|
249
|
+
result = dup
|
|
250
|
+
unless Inflections.uncountables.include?(downcase)
|
|
251
|
+
Inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
|
252
|
+
end
|
|
253
|
+
result
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The reverse of pluralize, returns the singular form of a word in a string.
|
|
257
|
+
#
|
|
258
|
+
# Examples
|
|
259
|
+
# "posts".singularize #=> "post"
|
|
260
|
+
# "octopi".singularize #=> "octopus"
|
|
261
|
+
# "sheep".singluarize #=> "sheep"
|
|
262
|
+
# "word".singluarize #=> "word"
|
|
263
|
+
# "the blue mailmen".singularize #=> "the blue mailman"
|
|
264
|
+
# "CamelOctopi".singularize #=> "CamelOctopus"
|
|
265
|
+
def singularize
|
|
266
|
+
result = dup
|
|
267
|
+
unless Inflections.uncountables.include?(downcase)
|
|
268
|
+
Inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
|
269
|
+
end
|
|
270
|
+
result
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Underscores and pluralizes the string.
|
|
274
|
+
#
|
|
275
|
+
# Examples
|
|
276
|
+
# "RawScaledScorer".tableize #=> "raw_scaled_scorers"
|
|
277
|
+
# "egg_and_ham".tableize #=> "egg_and_hams"
|
|
278
|
+
# "fancyCategory".tableize #=> "fancy_categories"
|
|
279
|
+
def tableize
|
|
280
|
+
underscore.pluralize
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
|
284
|
+
# a nicer looking title. Titleize is meant for creating pretty output.
|
|
285
|
+
#
|
|
286
|
+
# titleize is also aliased as as titlecase
|
|
287
|
+
#
|
|
288
|
+
# Examples
|
|
289
|
+
# "man from the boondocks".titleize #=> "Man From The Boondocks"
|
|
290
|
+
# "x-men: the last stand".titleize #=> "X Men: The Last Stand"
|
|
291
|
+
def titleize
|
|
292
|
+
underscore.humanize.gsub(/\b([a-z])/) { |x| x[-1..-1].upcase }
|
|
293
|
+
end
|
|
294
|
+
alias_method :titlecase, :titleize
|
|
295
|
+
|
|
296
|
+
# The reverse of camelize. Makes an underscored form from the expression in the string.
|
|
297
|
+
# Also changes '::' to '/' to convert namespaces to paths.
|
|
298
|
+
#
|
|
299
|
+
# Examples
|
|
300
|
+
# "ActiveRecord".underscore #=> "active_record"
|
|
301
|
+
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
|
302
|
+
def underscore
|
|
303
|
+
gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
|
304
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
# Ripped from the Sequel gem by Jeremy Evans
|
|
310
|
+
#
|
|
311
|
+
#
|
|
312
|
+
# Copyright (c) 2007-2008 Sharon Rosner
|
|
313
|
+
# Copyright (c) 2008-2015 Jeremy Evans
|
|
314
|
+
#
|
|
315
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
316
|
+
# of this software and associated documentation files (the "Software"), to
|
|
317
|
+
# deal in the Software without restriction, including without limitation the
|
|
318
|
+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
319
|
+
# sell copies of the Software, and to permit persons to whom the Software is
|
|
320
|
+
# furnished to do so, subject to the following conditions:
|
|
321
|
+
#
|
|
322
|
+
# The above copyright notice and this permission notice shall be included in
|
|
323
|
+
# all copies or substantial portions of the Software.
|
|
324
|
+
#
|
|
325
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
326
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
327
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
328
|
+
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
329
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
330
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|