padrino-support 0.13.3.1 → 0.13.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3d089c213dd819144c3f8667c4b783bcba84ffe
4
- data.tar.gz: ba764bc92ef86ab5b1e4835006af84806eb30b76
3
+ metadata.gz: ac419de645c2f3f52edf81364c8b170975b5e374
4
+ data.tar.gz: dbdec2f4d9e8f5adbbf1025ab416deb14040091f
5
5
  SHA512:
6
- metadata.gz: 8f634f3097194ef1f71a5f03a614be4be6db5cd564fe9a9f315a8d43218c972568ae8de8a649b6d13e4be945e8a343a1ed9b50fe59f8be59abfc5f266358d4b9
7
- data.tar.gz: e107ef8c84a337d6501f1df06ba4f96ab75b1169194238d99dcb9f7b6c2e520382927e570172c27cc84e6ec57f30f75c77f5ba1cb446d5d50987768455afd9dd
6
+ metadata.gz: 748ed677b332883f78061f4485dc28c5026055ad3b7af3c7abc747f2581d7b36bc3fbe77e0e5b5b00d86f6e347fa9335bface9ed5e8dd45fc61becf334312283
7
+ data.tar.gz: 586a56f27ab351fbaad2430900de3b87a9b107530cb82e169b5683420bdf27d191ed7c32722f18ad45238ad223774ec9e2665169201f988a2f82b43801795e43
@@ -18,7 +18,6 @@ rescue LoadError
18
18
  end
19
19
 
20
20
 
21
- require 'padrino-support/core_ext/string/inflections'
22
21
  require 'padrino-support/core_ext/string/colorize'
23
22
  require 'padrino-support/core_ext/object_space'
24
23
  require 'padrino-support/file_set'
@@ -0,0 +1,49 @@
1
+ ##
2
+ # This module is based on Sequel 4.27.0
3
+ # sequel-4.27.0/lib/sequel/model/default_inflections.rb
4
+ #
5
+ module Padrino
6
+ # Proc that is instance evaled to create the default inflections for both the
7
+ # model inflector and the inflector extension.
8
+ DEFAULT_INFLECTIONS_PROC = proc do
9
+ plural(/$/, 's')
10
+ plural(/s$/i, 's')
11
+ plural(/(alias|(?:stat|octop|vir|b)us)$/i, '\1es')
12
+ plural(/(buffal|tomat)o$/i, '\1oes')
13
+ plural(/([ti])um$/i, '\1a')
14
+ plural(/sis$/i, 'ses')
15
+ plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
16
+ plural(/(hive)$/i, '\1s')
17
+ plural(/([^aeiouy]|qu)y$/i, '\1ies')
18
+ plural(/(x|ch|ss|sh)$/i, '\1es')
19
+ plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
20
+ plural(/([m|l])ouse$/i, '\1ice')
21
+
22
+ singular(/s$/i, '')
23
+ singular(/([ti])a$/i, '\1um')
24
+ singular(/(analy|ba|cri|diagno|parenthe|progno|synop|the)ses$/i, '\1sis')
25
+ singular(/([^f])ves$/i, '\1fe')
26
+ singular(/([h|t]ive)s$/i, '\1')
27
+ singular(/([lr])ves$/i, '\1f')
28
+ singular(/([^aeiouy]|qu)ies$/i, '\1y')
29
+ singular(/(m)ovies$/i, '\1ovie')
30
+ singular(/(x|ch|ss|sh)es$/i, '\1')
31
+ singular(/([m|l])ice$/i, '\1ouse')
32
+ singular(/buses$/i, 'bus')
33
+ singular(/oes$/i, 'o')
34
+ singular(/shoes$/i, 'shoe')
35
+ singular(/(alias|(?:stat|octop|vir|b)us)es$/i, '\1')
36
+ singular(/(vert|ind)ices$/i, '\1ex')
37
+ singular(/matrices$/i, 'matrix')
38
+
39
+ irregular('person', 'people')
40
+ irregular('man', 'men')
41
+ irregular('child', 'children')
42
+ irregular('sex', 'sexes')
43
+ irregular('move', 'moves')
44
+ irregular('quiz', 'quizzes')
45
+ irregular('testis', 'testes')
46
+
47
+ uncountable(%w(equipment information rice money species series fish sheep news))
48
+ end
49
+ end
@@ -0,0 +1,179 @@
1
+ ##
2
+ # This module is based on Sequel 4.27.0
3
+ # sequel-4.27.0/lib/sequel/model/inflections.rb
4
+ #
5
+ require 'padrino-support/default_inflections'
6
+
7
+ module Padrino
8
+ # This module acts as a singleton returned/yielded by Sequel.inflections,
9
+ # which is used to override or specify additional inflection rules
10
+ # for Sequel. Examples:
11
+ #
12
+ # Sequel.inflections do |inflect|
13
+ # inflect.plural /^(ox)$/i, '\1\2en'
14
+ # inflect.singular /^(ox)en/i, '\1'
15
+ #
16
+ # inflect.irregular 'octopus', 'octopi'
17
+ #
18
+ # inflect.uncountable "equipment"
19
+ # end
20
+ #
21
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
22
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
23
+ # already have been loaded.
24
+ module Inflections
25
+ CAMELIZE_CONVERT_REGEXP = /(^|_)(.)/.freeze
26
+ CAMELIZE_MODULE_REGEXP = /\/(.?)/.freeze
27
+ DASH = '-'.freeze
28
+ DEMODULIZE_CONVERT_REGEXP = /^.*::/.freeze
29
+ EMPTY_STRING= ''.freeze
30
+ SLASH = '/'.freeze
31
+ VALID_CONSTANT_NAME_REGEXP = /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.freeze
32
+ UNDERSCORE = '_'.freeze
33
+ UNDERSCORE_CONVERT_REGEXP1 = /([A-Z]+)([A-Z][a-z])/.freeze
34
+ UNDERSCORE_CONVERT_REGEXP2 = /([a-z\d])([A-Z])/.freeze
35
+ UNDERSCORE_CONVERT_REPLACE = '\1_\2'.freeze
36
+ UNDERSCORE_MODULE_REGEXP = /::/.freeze
37
+
38
+ @plurals, @singulars, @uncountables = [], [], []
39
+
40
+ class << self
41
+ # Array of two element arrays, first containing a regex, and the second containing a substitution pattern, used for plurization.
42
+ attr_reader :plurals
43
+
44
+ # Array of two element arrays, first containing a regex, and the second containing a substitution pattern, used for singularization.
45
+ attr_reader :singulars
46
+
47
+ # Array of strings for words were the singular form is the same as the plural form
48
+ attr_reader :uncountables
49
+ end
50
+
51
+ # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
52
+ # the options are: :plurals, :singulars, :uncountables
53
+ #
54
+ # Examples:
55
+ # clear :all
56
+ # clear :plurals
57
+ def self.clear(scope = :all)
58
+ case scope
59
+ when :all
60
+ @plurals, @singulars, @uncountables = [], [], []
61
+ else
62
+ instance_variable_set("@#{scope}", [])
63
+ end
64
+ end
65
+
66
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
67
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
68
+ #
69
+ # Examples:
70
+ # irregular 'octopus', 'octopi'
71
+ # irregular 'person', 'people'
72
+ def self.irregular(singular, plural)
73
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
74
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
75
+ end
76
+
77
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
78
+ # The replacement should always be a string that may include references to the matched data from the rule.
79
+ #
80
+ # Example:
81
+ # plural(/(x|ch|ss|sh)$/i, '\1es')
82
+ def self.plural(rule, replacement)
83
+ @plurals.insert(0, [rule, replacement])
84
+ end
85
+
86
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
87
+ # The replacement should always be a string that may include references to the matched data from the rule.
88
+ #
89
+ # Example:
90
+ # singular(/([^aeiouy]|qu)ies$/i, '\1y')
91
+ def self.singular(rule, replacement)
92
+ @singulars.insert(0, [rule, replacement])
93
+ end
94
+
95
+ # Add uncountable words that shouldn't be attempted inflected.
96
+ #
97
+ # Examples:
98
+ # uncountable "money"
99
+ # uncountable "money", "information"
100
+ # uncountable %w( money information rice )
101
+ def self.uncountable(*words)
102
+ (@uncountables << words).flatten!
103
+ end
104
+
105
+ instance_eval(&DEFAULT_INFLECTIONS_PROC)
106
+
107
+ extend self
108
+
109
+ # Convert the given string to CamelCase. Will also convert '/' to '::' which is useful for converting paths to namespaces.
110
+ def camelize(s)
111
+ s = s.to_s
112
+ return s.camelize if s.respond_to?(:camelize)
113
+ s = s.gsub(CAMELIZE_MODULE_REGEXP){|x| "::#{x[-1..-1].upcase unless x == SLASH}"}.gsub(CAMELIZE_CONVERT_REGEXP){|x| x[-1..-1].upcase}
114
+ s
115
+ end
116
+
117
+ # Tries to find a declared constant with the name specified
118
+ # in the string. It raises a NameError when the name is not in CamelCase
119
+ # or is not initialized.
120
+ def constantize(s)
121
+ s = s.to_s
122
+ return s.constantize if s.respond_to?(:constantize)
123
+ raise(NameError, "#{s.inspect} is not a valid constant name!") unless m = VALID_CONSTANT_NAME_REGEXP.match(s)
124
+ Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
125
+ end
126
+
127
+ # Removes the module part from the expression in the string
128
+ def demodulize(s)
129
+ s = s.to_s
130
+ return s.demodulize if s.respond_to?(:demodulize)
131
+ s.gsub(DEMODULIZE_CONVERT_REGEXP, EMPTY_STRING)
132
+ end
133
+
134
+ # Returns the plural form of the word in the string.
135
+ def pluralize(s)
136
+ s = s.to_s
137
+ return s.pluralize if s.respond_to?(:pluralize)
138
+ result = s.dup
139
+ Inflections.plurals.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(s.downcase)
140
+ result
141
+ end
142
+
143
+ # The reverse of pluralize, returns the singular form of a word in a string.
144
+ def singularize(s)
145
+ s = s.to_s
146
+ return s.singularize if s.respond_to?(:singularize)
147
+ result = s.dup
148
+ Inflections.singulars.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(s.downcase)
149
+ result
150
+ end
151
+
152
+ # The reverse of camelize. Makes an underscored form from the expression in the string.
153
+ # Also changes '::' to '/' to convert namespaces to paths.
154
+ def underscore(s)
155
+ s = s.to_s
156
+ return s.underscore if s.respond_to?(:underscore)
157
+ s.gsub(UNDERSCORE_MODULE_REGEXP, SLASH).gsub(UNDERSCORE_CONVERT_REGEXP1, UNDERSCORE_CONVERT_REPLACE).
158
+ gsub(UNDERSCORE_CONVERT_REGEXP2, UNDERSCORE_CONVERT_REPLACE).tr(DASH, UNDERSCORE).downcase
159
+ end
160
+
161
+ ##
162
+ # Capitalizes the first word, turns underscores into spaces, and strips a trailing '_id' if present.
163
+ #
164
+ def humanize(s)
165
+ s = s.to_s
166
+ return s.humanize if s.respond_to?(:humanize)
167
+ s.gsub(/_id$/, '').tr('_', ' ').capitalize
168
+ end
169
+
170
+ ##
171
+ # Create a class name from a plural table name like Rails does for table names to models.
172
+ #
173
+ def classify(s)
174
+ s = s.to_s
175
+ return s.classify if s.respond_to?(:classify)
176
+ camelize(singularize(s.sub(/.*\./, '')))
177
+ end
178
+ end
179
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.3.1
4
+ version: 0.13.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2016-08-30 00:00:00.000000000 Z
15
+ date: 2016-09-06 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activesupport
@@ -42,8 +42,9 @@ files:
42
42
  - lib/padrino-support.rb
43
43
  - lib/padrino-support/core_ext/object_space.rb
44
44
  - lib/padrino-support/core_ext/string/colorize.rb
45
- - lib/padrino-support/core_ext/string/inflections.rb
45
+ - lib/padrino-support/default_inflections.rb
46
46
  - lib/padrino-support/file_set.rb
47
+ - lib/padrino-support/inflections.rb
47
48
  - lib/padrino-support/locale/cs.yml
48
49
  - lib/padrino-support/locale/da.yml
49
50
  - lib/padrino-support/locale/de.yml
@@ -1,118 +0,0 @@
1
- require 'active_support/inflections' # load default inflections
2
- require 'active_support/inflector/methods' # constantize
3
- require 'active_support/inflector/inflections' # pluralize
4
-
5
- ##
6
- # This is an adapted version of active_support/core_ext/string/inflections.rb
7
- # to prevent loading several dependencies including I18n gem.
8
- #
9
- # Issue: https://github.com/rails/rails/issues/1526
10
- #
11
- class String
12
- ##
13
- # Returns the plural form of the word in the string.
14
- #
15
- # "post".pluralize # => "posts"
16
- # "octopus".pluralize # => "octopi"
17
- # "sheep".pluralize # => "sheep"
18
- # "words".pluralize # => "words"
19
- # "the blue mailman".pluralize # => "the blue mailmen"
20
- # "CamelOctopus".pluralize # => "CamelOctopi"
21
- #
22
- def pluralize
23
- ActiveSupport::Inflector.pluralize(self)
24
- end
25
-
26
- ##
27
- # Returns the singular form of the word in the string.
28
- #
29
- # "posts".singularize # => "post"
30
- # "octopi".singularize # => "octopus"
31
- # "sheep".singularize # => "sheep"
32
- # "words".singularize # => "word"
33
- # "the blue mailmen".singularize # => "the blue mailman"
34
- # "CamelOctopi".singularize # => "CamelOctopus"
35
- #
36
- def singularize
37
- ActiveSupport::Inflector.singularize(self)
38
- end
39
-
40
- ##
41
- # +constantize+ tries to find a declared constant with the name specified
42
- # in the string. It raises a NameError when the name is not in CamelCase
43
- # or is not initialized.
44
- #
45
- # "Module".constantize # => Module
46
- # "Class".constantize # => Class
47
- #
48
- def constantize
49
- ActiveSupport::Inflector.constantize(self)
50
- end
51
-
52
- ##
53
- # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
54
- #
55
- # +underscore+ will also change '::' to '/' to convert namespaces to paths.
56
- #
57
- # "ActiveRecord".underscore # => "active_record"
58
- # "ActiveRecord::Errors".underscore # => active_record/errors
59
- #
60
- def underscore
61
- ActiveSupport::Inflector.underscore(self)
62
- end
63
-
64
- ##
65
- # By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
66
- # is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
67
- #
68
- # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
69
- #
70
- # "active_record".camelize # => "ActiveRecord"
71
- # "active_record".camelize(:lower) # => "activeRecord"
72
- # "active_record/errors".camelize # => "ActiveRecord::Errors"
73
- # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
74
- #
75
- def camelize(first_letter = :upper)
76
- case first_letter
77
- when :upper then ActiveSupport::Inflector.camelize(self, true)
78
- when :lower then ActiveSupport::Inflector.camelize(self, false)
79
- end
80
- end
81
- alias_method :camelcase, :camelize
82
-
83
- ##
84
- # Create a class name from a plural table name like Rails does for table names to models.
85
- # Note that this returns a string and not a class. (To convert to an actual class
86
- # follow +classify+ with +constantize+.)
87
- #
88
- # "egg_and_hams".classify # => "EggAndHam"
89
- # "posts".classify # => "Post"
90
- #
91
- # Singular names are not handled correctly.
92
- #
93
- # "business".classify # => "Busines"
94
- #
95
- def classify
96
- ActiveSupport::Inflector.classify(self)
97
- end
98
-
99
- ##
100
- # Capitalizes the first word, turns underscores into spaces, and strips a trailing '_id' if present.
101
- #
102
- if ActiveSupport::Inflector.method(:humanize).arity == 1
103
- def humanize
104
- ActiveSupport::Inflector.humanize(self)
105
- end
106
- else
107
- def humanize(options = {})
108
- ActiveSupport::Inflector.humanize(self, options)
109
- end
110
- end
111
-
112
- ##
113
- # Replaces underscores with dashes in the string.
114
- #
115
- def dasherize
116
- ActiveSupport::Inflector.dasherize(self)
117
- end
118
- end