module Tacokit::Utils
Public Instance Methods
base_path(base, *paths)
click to toggle source
# File lib/tacokit/utils.rb, line 69 def base_path(base, *paths) return base if paths.empty? resource = paths.shift resource = resource.id if resource.respond_to?(:id) path_join base, resource, *paths end
blank?(obj)
click to toggle source
# File lib/tacokit/utils.rb, line 29 def blank?(obj) obj.respond_to?(:empty?) ? obj.empty? : !obj end
camel_join(*paths)
click to toggle source
# File lib/tacokit/utils.rb, line 42 def camel_join(*paths) path_join paths.map { |p| camel_path(p) } end
camel_path(path)
click to toggle source
# File lib/tacokit/utils.rb, line 37 def camel_path(path) camelize(path.to_s, :lower) end
Also aliased as: camp
camelize(string, lower = false)
click to toggle source
rubocop:enable Style/DotPosition
# File lib/tacokit/utils.rb, line 23 def camelize(string, lower = false) string = string.to_s.gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase } string = string[0].chr.downcase + string[1..-1] if lower string end
constantize(class_name)
click to toggle source
# File lib/tacokit/utils.rb, line 59 def constantize(class_name) Tacokit.const_get(camelize(singularize(class_name.to_s))) rescue NameError nil end
deep_transform_keys(hash, &block)
click to toggle source
# File lib/tacokit/utils.rb, line 4 def deep_transform_keys(hash, &block) _deep_transform_keys_in_object(hash, &block) end
extract_options(*args)
click to toggle source
# File lib/tacokit/utils.rb, line 8 def extract_options(*args) opts = args.last.is_a?(Hash) ? args.pop : {} [args, opts] end
path_join(*paths)
click to toggle source
# File lib/tacokit/utils.rb, line 46 def path_join(*paths) paths.join("/") end
present?(obj)
click to toggle source
# File lib/tacokit/utils.rb, line 33 def present?(obj) !blank?(obj) end
resource_id(identifier)
click to toggle source
# File lib/tacokit/utils.rb, line 55 def resource_id(identifier) identifier.respond_to?(:id) ? identifier.id : identifier end
resource_path(resource_name, identifier, *paths)
click to toggle source
Returns a resource path for a given resource name and id
# File lib/tacokit/utils.rb, line 51 def resource_path(resource_name, identifier, *paths) path_join resource_name, resource_id(identifier), *paths end
singularize(string)
click to toggle source
# File lib/tacokit/utils.rb, line 65 def singularize(string) string.gsub(/s$/, "") end
underscore(string)
click to toggle source
rubocop:disable Style/DotPosition
# File lib/tacokit/utils.rb, line 14 def underscore(string) string.gsub(/::/, "/"). gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). tr("-", "_"). downcase end
Private Instance Methods
_deep_transform_keys_in_object(object) { |key| ... }
click to toggle source
# File lib/tacokit/utils.rb, line 79 def _deep_transform_keys_in_object(object, &block) case object when Hash object.each_with_object({}) do |(key, value), result| result[yield(key)] = _deep_transform_keys_in_object(value, &block) end when Array object.map { |e| _deep_transform_keys_in_object(e, &block) } else object end end