Module: SearchObject::Helper
- Defined in:
- lib/search_object/helper.rb
Class Method Summary collapse
- .camelize(text) ⇒ Object
-
.deep_copy(object) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .define_module(&block) ⇒ Object
- .ensure_included(item, collection) ⇒ Object
- .normalize_search_handler(handler, name) ⇒ Object
- .select_keys(hash, keys) ⇒ Object
- .stringify_keys(hash) ⇒ Object
Class Method Details
.camelize(text) ⇒ Object
15 16 17 |
# File 'lib/search_object/helper.rb', line 15 def camelize(text) text.to_s.gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase } end |
.deep_copy(object) ⇒ Object
rubocop:disable Metrics/MethodLength
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/search_object/helper.rb', line 43 def deep_copy(object) # rubocop:disable Metrics/MethodLength case object when Array object.map { |element| deep_copy(element) } when Hash object.inject({}) do |result, (key, value)| result[key] = deep_copy(value) result end when NilClass, FalseClass, TrueClass, Symbol, Method, Numeric object else object.dup end end |
.define_module(&block) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/search_object/helper.rb', line 27 def define_module(&block) Module.new do define_singleton_method :included do |base| base.class_eval(&block) end end end |
.ensure_included(item, collection) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/search_object/helper.rb', line 19 def ensure_included(item, collection) if collection.include? item item else collection.first end end |
.normalize_search_handler(handler, name) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/search_object/helper.rb', line 35 def normalize_search_handler(handler, name) case handler when Symbol then ->(scope, value) { method(handler).call scope, value } when Proc then handler else ->(scope, value) { scope.where name => value unless value.blank? } end end |
.select_keys(hash, keys) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/search_object/helper.rb', line 8 def select_keys(hash, keys) keys.inject({}) do |memo, key| memo[key] = hash[key] if hash.key? key memo end end |
.stringify_keys(hash) ⇒ Object
4 5 6 |
# File 'lib/search_object/helper.rb', line 4 def stringify_keys(hash) Hash[(hash || {}).map { |k, v| [k.to_s, v] }] end |