Module: Tacokit::Utils
Instance Method Summary (collapse)
- - (Object) base_path(base, *paths)
- - (Boolean) blank?(obj)
- - (Object) camel_join(*paths)
- - (Object) camel_path(path) (also: #camp)
-
- (Object) camelize(string, lower = false)
rubocop:enable Style/DotPosition.
- - (Object) constantize(class_name)
- - (Object) deep_transform_keys(hash, &block)
- - (Object) extract_options(*args)
- - (Object) path_join(*paths)
- - (Boolean) present?(obj)
- - (Object) resource_id(identifier)
-
- (Object) resource_path(resource_name, identifier, *paths)
Returns a resource path for a given resource name and id.
- - (Object) singularize(string)
-
- (Object) underscore(string)
rubocop:disable Style/DotPosition.
Instance Method Details
- (Object) base_path(base, *paths)
69 70 71 72 73 74 75 |
# 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 |
- (Boolean) blank?(obj)
29 30 31 |
# File 'lib/tacokit/utils.rb', line 29 def blank?(obj) obj.respond_to?(:empty?) ? obj.empty? : !obj end |
- (Object) camel_join(*paths)
42 43 44 |
# File 'lib/tacokit/utils.rb', line 42 def camel_join(*paths) path_join paths.map { |p| camel_path(p) } end |
- (Object) camel_path(path) Also known as: camp
37 38 39 |
# File 'lib/tacokit/utils.rb', line 37 def camel_path(path) camelize(path.to_s, :lower) end |
- (Object) camelize(string, lower = false)
rubocop:enable Style/DotPosition
23 24 25 26 27 |
# 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 |
- (Object) constantize(class_name)
59 60 61 62 63 |
# File 'lib/tacokit/utils.rb', line 59 def constantize(class_name) Tacokit.const_get(camelize(singularize(class_name.to_s))) rescue NameError nil end |
- (Object) deep_transform_keys(hash, &block)
4 5 6 |
# File 'lib/tacokit/utils.rb', line 4 def deep_transform_keys(hash, &block) _deep_transform_keys_in_object(hash, &block) end |
- (Object) extract_options(*args)
8 9 10 11 |
# File 'lib/tacokit/utils.rb', line 8 def (*args) opts = args.last.is_a?(Hash) ? args.pop : {} [args, opts] end |
- (Object) path_join(*paths)
46 47 48 |
# File 'lib/tacokit/utils.rb', line 46 def path_join(*paths) paths.join("/") end |
- (Boolean) present?(obj)
33 34 35 |
# File 'lib/tacokit/utils.rb', line 33 def present?(obj) !blank?(obj) end |
- (Object) resource_id(identifier)
55 56 57 |
# File 'lib/tacokit/utils.rb', line 55 def resource_id(identifier) identifier.respond_to?(:id) ? identifier.id : identifier end |
- (Object) resource_path(resource_name, identifier, *paths)
Returns a resource path for a given resource name and id
51 52 53 |
# File 'lib/tacokit/utils.rb', line 51 def resource_path(resource_name, identifier, *paths) path_join resource_name, resource_id(identifier), *paths end |
- (Object) singularize(string)
65 66 67 |
# File 'lib/tacokit/utils.rb', line 65 def singularize(string) string.gsub(/s$/, "") end |
- (Object) underscore(string)
rubocop:disable Style/DotPosition
14 15 16 17 18 19 20 |
# 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 |