module SearchObject::Helper
:api: private
Public Instance Methods
camelize(text)
click to toggle source
# File lib/search_object/helper.rb, line 22 def camelize(text) text.to_s.gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase } end
deep_copy(object)
click to toggle source
# File lib/search_object/helper.rb, line 59 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)
click to toggle source
# File lib/search_object/helper.rb, line 43 def define_module(&block) Module.new do define_singleton_method :included do |base| base.class_eval(&block) end end end
ensure_included(item, collection)
click to toggle source
# File lib/search_object/helper.rb, line 35 def ensure_included(item, collection) if collection.include? item item else collection.first end end
normalize_search_handler(handler, name)
click to toggle source
# File lib/search_object/helper.rb, line 51 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
slice_keys(hash, keys)
click to toggle source
# File lib/search_object/helper.rb, line 15 def slice_keys(hash, keys) keys.inject({}) do |memo, key| memo[key] = hash[key] if hash.key? key memo end end
stringify_keys(hash)
click to toggle source
# File lib/search_object/helper.rb, line 8 def stringify_keys(hash) # Note(rstankov): From Rails 5+ ActionController::Parameters aren't Hash # In a lot of cases `stringify_keys` is used on action params hash = hash.to_unsafe_h if hash.respond_to? :to_unsafe_h Hash[(hash || {}).map { |k, v| [k.to_s, v] }] end
underscore(text)
click to toggle source
# File lib/search_object/helper.rb, line 26 def underscore(text) text.to_s .tr('::', '_') .gsub(/([A-Z]+)([A-Z][a-z])/) { "#{Regexp.last_match[1]}_#{Regexp.last_match[2]}" } .gsub(/([a-z\d])([A-Z])/) { "#{Regexp.last_match[1]}_#{Regexp.last_match[2]}" } .tr('-', '_') .downcase end