lux-fw 0.1.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.version +1 -0
- data/bin/cli/am +250 -0
- data/bin/cli/assets +37 -0
- data/bin/cli/console +50 -0
- data/bin/cli/dev +1 -0
- data/bin/cli/eval +15 -0
- data/bin/cli/exceptions +62 -0
- data/bin/cli/generate +82 -0
- data/bin/cli/get +5 -0
- data/bin/cli/nginx +28 -0
- data/bin/cli/production +1 -0
- data/bin/cli/routes +12 -0
- data/bin/cli/server +1 -0
- data/bin/cli/stat +1 -0
- data/bin/forever +65 -0
- data/bin/job_que +39 -0
- data/bin/lux +87 -0
- data/bin/txt/nginx.conf +29 -0
- data/bin/txt/siege-and-puma.txt +3 -0
- data/lib/common/base32.rb +47 -0
- data/lib/common/before_and_after.rb +71 -0
- data/lib/common/class_attributes.rb +66 -0
- data/lib/common/class_method_params.rb +94 -0
- data/lib/common/crypt.rb +66 -0
- data/lib/common/folder_model.rb +50 -0
- data/lib/common/generic_model.rb +62 -0
- data/lib/common/policy.rb +54 -0
- data/lib/common/string_base.rb +49 -0
- data/lib/common/url.rb +171 -0
- data/lib/lux/api/api.rb +150 -0
- data/lib/lux/api/lib/application_api.rb +19 -0
- data/lib/lux/api/lib/doc_builder.rb +18 -0
- data/lib/lux/api/lib/dsl.rb +73 -0
- data/lib/lux/api/lib/model_api.rb +145 -0
- data/lib/lux/api/lib/rescue.rb +18 -0
- data/lib/lux/cache/cache.rb +71 -0
- data/lib/lux/cache/lib/memcached.rb +3 -0
- data/lib/lux/cache/lib/null.rb +23 -0
- data/lib/lux/cache/lib/ram.rb +38 -0
- data/lib/lux/cell/cell.rb +260 -0
- data/lib/lux/config/config.rb +88 -0
- data/lib/lux/controller/controller.rb +185 -0
- data/lib/lux/controller/lib/nav.rb +77 -0
- data/lib/lux/controller/lib/plugs.rb +10 -0
- data/lib/lux/delayed_job/delayed_job.rb +44 -0
- data/lib/lux/delayed_job/lib/memory.rb +14 -0
- data/lib/lux/delayed_job/lib/nsq.rb +3 -0
- data/lib/lux/delayed_job/lib/postgre.rb +6 -0
- data/lib/lux/delayed_job/lib/redis.rb +19 -0
- data/lib/lux/error/error.rb +75 -0
- data/lib/lux/helper/helper.rb +109 -0
- data/lib/lux/html/html.rb +3 -0
- data/lib/lux/html/lib/form.rb +81 -0
- data/lib/lux/html/lib/input.rb +71 -0
- data/lib/lux/html/lib/input_types.rb +277 -0
- data/lib/lux/lux.rb +164 -0
- data/lib/lux/mailer/mailer.rb +73 -0
- data/lib/lux/page/lib/encrypt_params.rb +44 -0
- data/lib/lux/page/lib/flash.rb +49 -0
- data/lib/lux/page/lib/static_file.rb +97 -0
- data/lib/lux/page/page.rb +271 -0
- data/lib/lux/rescue_from/rescue_from.rb +61 -0
- data/lib/lux/template/template.rb +95 -0
- data/lib/lux-fw.rb +48 -0
- data/lib/overload/array.rb +52 -0
- data/lib/overload/blank.rb +62 -0
- data/lib/overload/date.rb +58 -0
- data/lib/overload/file.rb +14 -0
- data/lib/overload/hash.rb +86 -0
- data/lib/overload/hash_wia.rb +282 -0
- data/lib/overload/inflections.rb +199 -0
- data/lib/overload/integer.rb +19 -0
- data/lib/overload/module.rb +10 -0
- data/lib/overload/nil.rb +8 -0
- data/lib/overload/object.rb +77 -0
- data/lib/overload/string.rb +89 -0
- data/lib/overload/string_inflections.rb +7 -0
- data/lib/overload/struct.rb +5 -0
- data/lib/plugins/assets/assets_plug.rb +26 -0
- data/lib/plugins/assets/helper_module_adapter.rb +49 -0
- data/lib/plugins/assets/init.rb +4 -0
- data/lib/plugins/db_helpers/array_and_hstore.rb +64 -0
- data/lib/plugins/db_helpers/arrays_and_tags.rb +23 -0
- data/lib/plugins/db_helpers/before_save.rb +44 -0
- data/lib/plugins/db_helpers/cached_find_by.rb +45 -0
- data/lib/plugins/db_helpers/class_and_instance.rb +120 -0
- data/lib/plugins/db_helpers/dataset_plugin.rb +101 -0
- data/lib/plugins/db_helpers/filter_wrappers.rb +21 -0
- data/lib/plugins/db_helpers/link_plugin.rb +95 -0
- data/lib/plugins/db_helpers/localize_plugin.rb +57 -0
- data/lib/plugins/db_helpers/primary_keys.rb +36 -0
- data/lib/plugins/db_helpers/typero_attributes.rb +69 -0
- data/lib/plugins/db_logger/init.rb +18 -0
- data/lib/plugins/db_logger/lux_response_adapter.rb +9 -0
- data/lib/plugins/paginate/helper.rb +32 -0
- data/lib/plugins/paginate/sequel_adapter.rb +18 -0
- data/lib/vendor/mini_assets/mini_asset/base.rb +167 -0
- data/lib/vendor/mini_assets/mini_asset/css.rb +38 -0
- data/lib/vendor/mini_assets/mini_asset/js.rb +38 -0
- data/lib/vendor/mini_assets/mini_asset.rb +31 -0
- data/lib/vendor/oauth/lib/facebook.rb +35 -0
- data/lib/vendor/oauth/lib/github.rb +37 -0
- data/lib/vendor/oauth/lib/google.rb +41 -0
- data/lib/vendor/oauth/lib/linkedin.rb +41 -0
- data/lib/vendor/oauth/lib/stackexchange.rb +37 -0
- data/lib/vendor/oauth/lib/twitter.rb +41 -0
- data/lib/vendor/oauth/oauth.rb +46 -0
- metadata +334 -0
@@ -0,0 +1,282 @@
|
|
1
|
+
# exrtacted from Rails
|
2
|
+
|
3
|
+
class HashWithIndifferentAccess < Hash
|
4
|
+
# Returns +true+ so that <tt>Array#extract_options!</tt> finds members of
|
5
|
+
# this class.
|
6
|
+
def extractable_options?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
def with_indifferent_access
|
11
|
+
dup
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(constructor = {})
|
15
|
+
if constructor.respond_to?(:to_hash)
|
16
|
+
super()
|
17
|
+
update(constructor)
|
18
|
+
|
19
|
+
hash = constructor.to_hash
|
20
|
+
self.default = hash.default if hash.default
|
21
|
+
self.default_proc = hash.default_proc if hash.default_proc
|
22
|
+
else
|
23
|
+
super(constructor)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def default(*args)
|
28
|
+
arg_key = args.first
|
29
|
+
|
30
|
+
if include?(key = convert_key(arg_key))
|
31
|
+
self[key]
|
32
|
+
else
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.[](*args)
|
38
|
+
new.merge!(Hash[*args])
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
|
42
|
+
alias_method :regular_update, :update unless method_defined?(:regular_update)
|
43
|
+
|
44
|
+
# Assigns a new value to the hash:
|
45
|
+
#
|
46
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
47
|
+
# hash[:key] = 'value'
|
48
|
+
#
|
49
|
+
# This value can be later fetched using either +:key+ or <tt>'key'</tt>.
|
50
|
+
def []=(key, value)
|
51
|
+
regular_writer(convert_key(key), convert_value(value, for: :assignment))
|
52
|
+
end
|
53
|
+
|
54
|
+
alias_method :store, :[]=
|
55
|
+
|
56
|
+
# Updates the receiver in-place, merging in the hash passed as argument:
|
57
|
+
#
|
58
|
+
# hash_1 = ActiveSupport::HashWithIndifferentAccess.new
|
59
|
+
# hash_1[:key] = 'value'
|
60
|
+
#
|
61
|
+
# hash_2 = ActiveSupport::HashWithIndifferentAccess.new
|
62
|
+
# hash_2[:key] = 'New Value!'
|
63
|
+
#
|
64
|
+
# hash_1.update(hash_2) # => {"key"=>"New Value!"}
|
65
|
+
#
|
66
|
+
# The argument can be either an
|
67
|
+
# <tt>ActiveSupport::HashWithIndifferentAccess</tt> or a regular +Hash+.
|
68
|
+
# In either case the merge respects the semantics of indifferent access.
|
69
|
+
#
|
70
|
+
# If the argument is a regular hash with keys +:key+ and +"key"+ only one
|
71
|
+
# of the values end up in the receiver, but which one is unspecified.
|
72
|
+
#
|
73
|
+
# When given a block, the value for duplicated keys will be determined
|
74
|
+
# by the result of invoking the block with the duplicated key, the value
|
75
|
+
# in the receiver, and the value in +other_hash+. The rules for duplicated
|
76
|
+
# keys follow the semantics of indifferent access:
|
77
|
+
#
|
78
|
+
# hash_1[:key] = 10
|
79
|
+
# hash_2['key'] = 12
|
80
|
+
# hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22}
|
81
|
+
def update(other_hash)
|
82
|
+
if other_hash.is_a? HashWithIndifferentAccess
|
83
|
+
super(other_hash)
|
84
|
+
else
|
85
|
+
other_hash.to_hash.each_pair do |key, value|
|
86
|
+
if block_given? && key?(key)
|
87
|
+
value = yield(convert_key(key), self[key], value)
|
88
|
+
end
|
89
|
+
regular_writer(convert_key(key), convert_value(value))
|
90
|
+
end
|
91
|
+
self
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
alias_method :merge!, :update
|
96
|
+
|
97
|
+
# Checks the hash for a key matching the argument passed in:
|
98
|
+
#
|
99
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
100
|
+
# hash['key'] = 'value'
|
101
|
+
# hash.key?(:key) # => true
|
102
|
+
# hash.key?('key') # => true
|
103
|
+
def key?(key)
|
104
|
+
super(convert_key(key))
|
105
|
+
end
|
106
|
+
|
107
|
+
alias_method :include?, :key?
|
108
|
+
alias_method :has_key?, :key?
|
109
|
+
alias_method :member?, :key?
|
110
|
+
|
111
|
+
# Same as <tt>Hash#[]</tt> where the key passed as argument can be
|
112
|
+
# either a string or a symbol:
|
113
|
+
#
|
114
|
+
# counters = ActiveSupport::HashWithIndifferentAccess.new
|
115
|
+
# counters[:foo] = 1
|
116
|
+
#
|
117
|
+
# counters['foo'] # => 1
|
118
|
+
# counters[:foo] # => 1
|
119
|
+
# counters[:zoo] # => nil
|
120
|
+
def [](key)
|
121
|
+
super(convert_key(key))
|
122
|
+
end
|
123
|
+
|
124
|
+
# Same as <tt>Hash#fetch</tt> where the key passed as argument can be
|
125
|
+
# either a string or a symbol:
|
126
|
+
#
|
127
|
+
# counters = ActiveSupport::HashWithIndifferentAccess.new
|
128
|
+
# counters[:foo] = 1
|
129
|
+
#
|
130
|
+
# counters.fetch('foo') # => 1
|
131
|
+
# counters.fetch(:bar, 0) # => 0
|
132
|
+
# counters.fetch(:bar) { |key| 0 } # => 0
|
133
|
+
# counters.fetch(:zoo) # => KeyError: key not found: "zoo"
|
134
|
+
def fetch(key, *extras)
|
135
|
+
super(convert_key(key), *extras)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Returns an array of the values at the specified indices:
|
139
|
+
#
|
140
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
141
|
+
# hash[:a] = 'x'
|
142
|
+
# hash[:b] = 'y'
|
143
|
+
# hash.values_at('a', 'b') # => ["x", "y"]
|
144
|
+
def values_at(*indices)
|
145
|
+
indices.collect { |key| self[convert_key(key)] }
|
146
|
+
end
|
147
|
+
|
148
|
+
# Returns an array of the values at the specified indices, but also
|
149
|
+
# raises an exception when one of the keys can't be found.
|
150
|
+
#
|
151
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
152
|
+
# hash[:a] = 'x'
|
153
|
+
# hash[:b] = 'y'
|
154
|
+
# hash.fetch_values('a', 'b') # => ["x", "y"]
|
155
|
+
# hash.fetch_values('a', 'c') { |key| 'z' } # => ["x", "z"]
|
156
|
+
# hash.fetch_values('a', 'c') # => KeyError: key not found: "c"
|
157
|
+
def fetch_values(*indices, &block)
|
158
|
+
indices.collect { |key| fetch(key, &block) }
|
159
|
+
end if Hash.method_defined?(:fetch_values)
|
160
|
+
|
161
|
+
# Returns a shallow copy of the hash.
|
162
|
+
#
|
163
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new({ a: { b: 'b' } })
|
164
|
+
# dup = hash.dup
|
165
|
+
# dup[:a][:c] = 'c'
|
166
|
+
#
|
167
|
+
# hash[:a][:c] # => nil
|
168
|
+
# dup[:a][:c] # => "c"
|
169
|
+
def dup
|
170
|
+
self.class.new(self).tap do |new_hash|
|
171
|
+
set_defaults(new_hash)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# This method has the same semantics of +update+, except it does not
|
176
|
+
# modify the receiver but rather returns a new hash with indifferent
|
177
|
+
# access with the result of the merge.
|
178
|
+
def merge(hash, &block)
|
179
|
+
dup.update(hash, &block)
|
180
|
+
end
|
181
|
+
|
182
|
+
# Like +merge+ but the other way around: Merges the receiver into the
|
183
|
+
# argument and returns a new hash with indifferent access as result:
|
184
|
+
#
|
185
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
186
|
+
# hash['a'] = nil
|
187
|
+
# hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1}
|
188
|
+
def reverse_merge(other_hash)
|
189
|
+
super(self.class.new(other_hash))
|
190
|
+
end
|
191
|
+
alias_method :with_defaults, :reverse_merge
|
192
|
+
|
193
|
+
# Same semantics as +reverse_merge+ but modifies the receiver in-place.
|
194
|
+
def reverse_merge!(other_hash)
|
195
|
+
replace(reverse_merge(other_hash))
|
196
|
+
end
|
197
|
+
alias_method :with_defaults!, :reverse_merge!
|
198
|
+
|
199
|
+
# Replaces the contents of this hash with other_hash.
|
200
|
+
#
|
201
|
+
# h = { "a" => 100, "b" => 200 }
|
202
|
+
# h.replace({ "c" => 300, "d" => 400 }) # => {"c"=>300, "d"=>400}
|
203
|
+
def replace(other_hash)
|
204
|
+
super(self.class.new(other_hash))
|
205
|
+
end
|
206
|
+
|
207
|
+
# Removes the specified key from the hash.
|
208
|
+
def delete(key)
|
209
|
+
super(convert_key(key))
|
210
|
+
end
|
211
|
+
|
212
|
+
def stringify_keys!; self end
|
213
|
+
def deep_stringify_keys!; self end
|
214
|
+
def stringify_keys; dup end
|
215
|
+
def deep_stringify_keys; dup end
|
216
|
+
# undef :symbolize_keys!
|
217
|
+
# undef :deep_symbolize_keys!
|
218
|
+
def symbolize_keys; to_hash.symbolize_keys! end
|
219
|
+
def deep_symbolize_keys; to_hash.deep_symbolize_keys! end
|
220
|
+
def to_options!; self end
|
221
|
+
|
222
|
+
def select(*args, &block)
|
223
|
+
return to_enum(:select) unless block_given?
|
224
|
+
dup.tap { |hash| hash.select!(*args, &block) }
|
225
|
+
end
|
226
|
+
|
227
|
+
def reject(*args, &block)
|
228
|
+
return to_enum(:reject) unless block_given?
|
229
|
+
dup.tap { |hash| hash.reject!(*args, &block) }
|
230
|
+
end
|
231
|
+
|
232
|
+
def transform_values(*args, &block)
|
233
|
+
return to_enum(:transform_values) unless block_given?
|
234
|
+
dup.tap { |hash| hash.transform_values!(*args, &block) }
|
235
|
+
end
|
236
|
+
|
237
|
+
def compact
|
238
|
+
dup.tap(&:compact!)
|
239
|
+
end
|
240
|
+
|
241
|
+
# Convert to a regular hash with string keys.
|
242
|
+
def to_hash
|
243
|
+
_new_hash = Hash.new
|
244
|
+
set_defaults(_new_hash)
|
245
|
+
|
246
|
+
each do |key, value|
|
247
|
+
_new_hash[key] = convert_value(value, for: :to_hash)
|
248
|
+
end
|
249
|
+
_new_hash
|
250
|
+
end
|
251
|
+
|
252
|
+
private
|
253
|
+
def convert_key(key) # :doc:
|
254
|
+
key.kind_of?(Symbol) ? key.to_s : key
|
255
|
+
end
|
256
|
+
|
257
|
+
def convert_value(value, options = {}) # :doc:
|
258
|
+
if value.is_a? Hash
|
259
|
+
if options[:for] == :to_hash
|
260
|
+
value.to_hash
|
261
|
+
else
|
262
|
+
value.class == Hash ? HashWithIndifferentAccess.new(value) : value
|
263
|
+
end
|
264
|
+
elsif value.is_a?(Array)
|
265
|
+
if options[:for] != :assignment || value.frozen?
|
266
|
+
value = value.dup
|
267
|
+
end
|
268
|
+
value.map! { |e| convert_value(e, options) }
|
269
|
+
else
|
270
|
+
value
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
def set_defaults(target) # :doc:
|
275
|
+
if default_proc
|
276
|
+
target.default_proc = default_proc.dup
|
277
|
+
else
|
278
|
+
target.default = default
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
@@ -0,0 +1,199 @@
|
|
1
|
+
# # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
|
2
|
+
|
3
|
+
# class String
|
4
|
+
# def pluralize(locale = :en)
|
5
|
+
# word = self
|
6
|
+
# apply_inflections(word, inflections(locale).plurals)
|
7
|
+
# end
|
8
|
+
|
9
|
+
# def singularize(locale = :en)
|
10
|
+
# word = self
|
11
|
+
# apply_inflections(word, inflections(locale).singulars)
|
12
|
+
# end
|
13
|
+
|
14
|
+
# def camelize(uppercase_first_letter = true)
|
15
|
+
# term = self
|
16
|
+
# string = term.to_s
|
17
|
+
# if uppercase_first_letter
|
18
|
+
# string = string.sub(/^[a-z\d]*/) { |match| inflections.acronyms[match] || match.capitalize }
|
19
|
+
# else
|
20
|
+
# string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
|
21
|
+
# end
|
22
|
+
# string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }
|
23
|
+
# string.gsub!("/".freeze, "::".freeze)
|
24
|
+
# string
|
25
|
+
# end
|
26
|
+
|
27
|
+
# def underscore
|
28
|
+
# camel_cased_word = self
|
29
|
+
# return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
|
30
|
+
# word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
|
31
|
+
# word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'.freeze }#{$2.downcase}" }
|
32
|
+
# word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
|
33
|
+
# word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
|
34
|
+
# word.tr!("-".freeze, "_".freeze)
|
35
|
+
# word.downcase!
|
36
|
+
# word
|
37
|
+
# end
|
38
|
+
|
39
|
+
# def humanize(options = {})
|
40
|
+
# result = self.to_s.dup
|
41
|
+
|
42
|
+
# inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
43
|
+
|
44
|
+
# result.sub!(/\A_+/, "".freeze)
|
45
|
+
# result.sub!(/_id\z/, "".freeze)
|
46
|
+
# result.tr!("_".freeze, " ".freeze)
|
47
|
+
|
48
|
+
# result.gsub!(/([a-z\d]*)/i) do |match|
|
49
|
+
# "#{inflections.acronyms[match] || match.downcase}"
|
50
|
+
# end
|
51
|
+
|
52
|
+
# if options.fetch(:capitalize, true)
|
53
|
+
# result.sub!(/\A\w/) { |match| match.upcase }
|
54
|
+
# end
|
55
|
+
|
56
|
+
# result
|
57
|
+
# end
|
58
|
+
|
59
|
+
# def upcase_first
|
60
|
+
# string = self
|
61
|
+
# string.length > 0 ? string[0].upcase.concat(string[1..-1]) : ""
|
62
|
+
# end
|
63
|
+
|
64
|
+
# def titleize
|
65
|
+
# word = self
|
66
|
+
# humanize(underscore(word)).gsub(/\b(?<!['’`])[a-z]/) { |match| match.capitalize }
|
67
|
+
# end
|
68
|
+
|
69
|
+
# def tableize
|
70
|
+
# class_name = self
|
71
|
+
# pluralize(underscore(class_name))
|
72
|
+
# end
|
73
|
+
|
74
|
+
# def classify
|
75
|
+
# table_name = self.dup
|
76
|
+
# table_name.to_s.sub(/.*\./, "").freeze.singularize.camelize
|
77
|
+
# end
|
78
|
+
|
79
|
+
# def dasherize
|
80
|
+
# underscored_word = self
|
81
|
+
# underscored_word.tr("_".freeze, "-".freeze)
|
82
|
+
# end
|
83
|
+
|
84
|
+
# def demodulize()
|
85
|
+
# path = self.to_s
|
86
|
+
# if i = path.rindex("::")
|
87
|
+
# path[(i + 2)..-1]
|
88
|
+
# else
|
89
|
+
# path
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
|
93
|
+
# def deconstantize
|
94
|
+
# path = self.to_s
|
95
|
+
# path.to_s[0, path.rindex("::") || 0] # implementation based on the one in facets' Module#spacename
|
96
|
+
# end
|
97
|
+
|
98
|
+
# def foreign_key(separate_class_name_and_id_with_underscore = true)
|
99
|
+
# class_name = self
|
100
|
+
# underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
101
|
+
# end
|
102
|
+
|
103
|
+
# def constantize
|
104
|
+
# camel_cased_word = self
|
105
|
+
# names = camel_cased_word.split("::".freeze)
|
106
|
+
|
107
|
+
# # Trigger a built-in NameError exception including the ill-formed constant in the message.
|
108
|
+
# Object.const_get(camel_cased_word) if names.blank?
|
109
|
+
|
110
|
+
# # Remove the first blank element in case of '::ClassName' notation.
|
111
|
+
# names.shift if names.size > 1 && names.first.blank?
|
112
|
+
|
113
|
+
# names.inject(Object) do |constant, name|
|
114
|
+
# if constant == Object
|
115
|
+
# constant.const_get(name)
|
116
|
+
# else
|
117
|
+
# candidate = constant.const_get(name)
|
118
|
+
# next candidate if constant.const_defined?(name, false)
|
119
|
+
# next candidate unless Object.const_defined?(name)
|
120
|
+
|
121
|
+
# # Go down the ancestors to check if it is owned directly. The check
|
122
|
+
# # stops when we reach Object or the end of ancestors tree.
|
123
|
+
# constant = constant.ancestors.inject do |const, ancestor|
|
124
|
+
# break const if ancestor == Object
|
125
|
+
# break ancestor if ancestor.const_defined?(name, false)
|
126
|
+
# const
|
127
|
+
# end
|
128
|
+
|
129
|
+
# # owner is in Object, so raise
|
130
|
+
# constant.const_get(name, false)
|
131
|
+
# end
|
132
|
+
# end
|
133
|
+
# end
|
134
|
+
|
135
|
+
# def safe_constantize
|
136
|
+
# camel_cased_word = self
|
137
|
+
# constantize(camel_cased_word)
|
138
|
+
# rescue NameError => e
|
139
|
+
# raise if e.name && !(camel_cased_word.to_s.split("::").include?(e.name.to_s) ||
|
140
|
+
# e.name.to_s == camel_cased_word.to_s)
|
141
|
+
# rescue ArgumentError => e
|
142
|
+
# raise unless /not missing constant #{const_regexp(camel_cased_word)}!$/.match?(e.message)
|
143
|
+
# end
|
144
|
+
|
145
|
+
# def ordinal
|
146
|
+
# number = self
|
147
|
+
# abs_number = number.to_i.abs
|
148
|
+
|
149
|
+
# if (11..13).include?(abs_number % 100)
|
150
|
+
# "th"
|
151
|
+
# else
|
152
|
+
# case abs_number % 10
|
153
|
+
# when 1; "st"
|
154
|
+
# when 2; "nd"
|
155
|
+
# when 3; "rd"
|
156
|
+
# else "th"
|
157
|
+
# end
|
158
|
+
# end
|
159
|
+
# end
|
160
|
+
|
161
|
+
# def ordinalize
|
162
|
+
# number = self
|
163
|
+
# "#{number}#{ordinal(number)}"
|
164
|
+
# end
|
165
|
+
|
166
|
+
# private
|
167
|
+
|
168
|
+
# # Mounts a regular expression, returned as a string to ease interpolation,
|
169
|
+
# # that will match part by part the given constant.
|
170
|
+
# #
|
171
|
+
# # const_regexp("Foo::Bar::Baz") # => "Foo(::Bar(::Baz)?)?"
|
172
|
+
# # const_regexp("::") # => "::"
|
173
|
+
# def const_regexp(camel_cased_word) #:nodoc:
|
174
|
+
# parts = camel_cased_word.split("::".freeze)
|
175
|
+
|
176
|
+
# return Regexp.escape(camel_cased_word) if parts.blank?
|
177
|
+
|
178
|
+
# last = parts.pop
|
179
|
+
|
180
|
+
# parts.reverse.inject(last) do |acc, part|
|
181
|
+
# part.blank? ? acc : "#{part}(::#{acc})?"
|
182
|
+
# end
|
183
|
+
# end
|
184
|
+
|
185
|
+
# # Applies inflection rules for +singularize+ and +pluralize+.
|
186
|
+
# #
|
187
|
+
# # apply_inflections('post', inflections.plurals) # => "posts"
|
188
|
+
# # apply_inflections('posts', inflections.singulars) # => "post"
|
189
|
+
# def apply_inflections(word, rules)
|
190
|
+
# result = word.to_s.dup
|
191
|
+
|
192
|
+
# if word.blank? || inflections.uncountables.uncountable?(result)
|
193
|
+
# result
|
194
|
+
# else
|
195
|
+
# rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
196
|
+
# result
|
197
|
+
# end
|
198
|
+
# end
|
199
|
+
# end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Integer
|
2
|
+
def pluralize(desc)
|
3
|
+
if self == 0
|
4
|
+
"no #{desc.to_s.pluralize}"
|
5
|
+
elsif self == 1
|
6
|
+
"#{self} #{desc}"
|
7
|
+
else
|
8
|
+
"#{self} #{desc.to_s.pluralize}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def dotted
|
13
|
+
data = self.to_s
|
14
|
+
while data.sub!(/(\d)(\d{3})$/, "\\1.\\2")
|
15
|
+
1
|
16
|
+
end
|
17
|
+
data
|
18
|
+
end
|
19
|
+
end
|
data/lib/overload/nil.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
class LocalRaiseError < StandardError
|
2
|
+
end
|
3
|
+
|
4
|
+
class Object
|
5
|
+
def r(what=nil)
|
6
|
+
opath = what.class.ancestors
|
7
|
+
out = opath.join("\n> ")
|
8
|
+
|
9
|
+
data = what.is_a?(Hash) ? JSON.pretty_generate(what) : what.ai(plain:true)
|
10
|
+
out = [data, out, ''].join("\n\n-\n\n")
|
11
|
+
|
12
|
+
raise LocalRaiseError.new out
|
13
|
+
end
|
14
|
+
|
15
|
+
def instance_variables_hash
|
16
|
+
Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ]
|
17
|
+
end
|
18
|
+
|
19
|
+
def or(_or)
|
20
|
+
self.blank? || self == 0 ? _or : self
|
21
|
+
end
|
22
|
+
|
23
|
+
def try(*args)
|
24
|
+
return nil if self.class == NilClass
|
25
|
+
self.send(*args)
|
26
|
+
end
|
27
|
+
|
28
|
+
def die(desc=nil, exp_object=nil)
|
29
|
+
desc ||= 'died without desc'
|
30
|
+
desc = '%s: %s' % [exp_object.class, desc] if exp_object
|
31
|
+
raise desc
|
32
|
+
end
|
33
|
+
|
34
|
+
# this will capture plain Hash and HashWithIndifferentAccess
|
35
|
+
def is_hash?
|
36
|
+
self.class.to_s.index('Hash') ? true : false
|
37
|
+
end
|
38
|
+
|
39
|
+
def is_array?
|
40
|
+
self.class.to_s.index('Array') ? true : false
|
41
|
+
end
|
42
|
+
|
43
|
+
def is_string?
|
44
|
+
self.class.to_s == 'String' ? true : false
|
45
|
+
end
|
46
|
+
|
47
|
+
def is_false?
|
48
|
+
self.class.name == 'FalseClass' ? true : false
|
49
|
+
end
|
50
|
+
|
51
|
+
def is_true?
|
52
|
+
self ? true :false
|
53
|
+
end
|
54
|
+
|
55
|
+
def is_numeric?
|
56
|
+
Float(self) != nil rescue false
|
57
|
+
end
|
58
|
+
|
59
|
+
def is_symbol?
|
60
|
+
self.class.to_s == 'Symbol' ? true : false
|
61
|
+
end
|
62
|
+
|
63
|
+
def is_boolean?
|
64
|
+
self.class == TrueClass || self.class == FalseClass
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
# if we dont have awesome print in prodction, define mock
|
70
|
+
method(:ap) rescue Proc.new do
|
71
|
+
class Object
|
72
|
+
def ap(*args)
|
73
|
+
puts args
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end.call
|
77
|
+
|
@@ -0,0 +1,89 @@
|
|
1
|
+
class String
|
2
|
+
# simple markdown
|
3
|
+
def as_html
|
4
|
+
ret = self
|
5
|
+
ret = ret.gsub(/([\w\.])\n(\w)/,"\\1<br/>\\2")
|
6
|
+
ret
|
7
|
+
end
|
8
|
+
|
9
|
+
# convert escaped strings, remove scritpts
|
10
|
+
def to_html(opts={})
|
11
|
+
value = self.gsub(/</, '<').gsub(/>/, '>').gsub(/&/,'&')
|
12
|
+
value = value.gsub(/<script/,'<script') unless opts[:script]
|
13
|
+
value = value.gsub(/<link/,'<link') unless opts[:link]
|
14
|
+
value
|
15
|
+
end
|
16
|
+
|
17
|
+
def trim(len)
|
18
|
+
return self if self.length<len
|
19
|
+
data = self.dup[0,len]+'…'
|
20
|
+
data
|
21
|
+
end
|
22
|
+
|
23
|
+
def sanitize
|
24
|
+
Sanitize.clean(self, :elements=>%w[span ul ol li b bold i italic u underline hr br p], :attributes=>{'span'=>['style']} )
|
25
|
+
end
|
26
|
+
|
27
|
+
def tag(node_name, opts={})
|
28
|
+
opts.tag(node_name, self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def wrap(node_name, opts={})
|
32
|
+
return self unless node_name
|
33
|
+
opts.tag(node_name, self)
|
34
|
+
end
|
35
|
+
|
36
|
+
def fix_ut8
|
37
|
+
self.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '?')
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_erb
|
41
|
+
self.gsub(/<%=([^%]+)%>/) { eval $1; }
|
42
|
+
end
|
43
|
+
|
44
|
+
def parameterize
|
45
|
+
self.downcase.gsub(/[^\w]+/,'-')
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_url
|
49
|
+
# str_from = 'šđč枊ĐČĆŽäÄéeöÖüüÜß'
|
50
|
+
# str_to = 'sdcczSDCCZaAeeoOuuUs'
|
51
|
+
# self.downcase.gsub(/\s+/,'-').tr(str_from, str_to).gsub(/[^\w\-]/,'')
|
52
|
+
self.gsub('&',' and ').gsub('.',' dot ').parameterize.gsub('-dot-','.').downcase[0, 50]
|
53
|
+
end
|
54
|
+
|
55
|
+
def css_to_hash
|
56
|
+
self.split('&').inject({}) do |h,line|
|
57
|
+
el = line.split('=', 2)
|
58
|
+
h[el[0]] = el[1]
|
59
|
+
h
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_a
|
64
|
+
self.split(/\s*,\s*/)
|
65
|
+
end
|
66
|
+
|
67
|
+
def starts_with?(prefix)
|
68
|
+
prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix
|
69
|
+
end
|
70
|
+
|
71
|
+
def last(num=1)
|
72
|
+
len = self.length
|
73
|
+
self[len-num, len]
|
74
|
+
end
|
75
|
+
|
76
|
+
def span_green
|
77
|
+
%[<span style="color: #080;">#{self}</span>]
|
78
|
+
end
|
79
|
+
|
80
|
+
def span_red
|
81
|
+
%[<span style="color: #800;">#{self}</span>]
|
82
|
+
end
|
83
|
+
|
84
|
+
def constantize
|
85
|
+
klass = self
|
86
|
+
obj = Object.const_get(klass)
|
87
|
+
obj.to_s == klass.sub(/^::/,'') ? obj : raise(NameError, 'Cant find class %s' % klass)
|
88
|
+
end
|
89
|
+
end
|