Module: Tacokit::Utils

Included in:
Client, Transform
Defined in:
lib/tacokit/utils.rb

Instance Method Summary collapse

Instance Method Details

#base_path(base, *paths) ⇒ Object


60
61
62
63
64
65
66
# File 'lib/tacokit/utils.rb', line 60

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) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/tacokit/utils.rb', line 29

def blank?(obj)
  obj.respond_to?(:empty?) ? obj.empty? : !obj
end

#camel_join(*paths) ⇒ Object


42
43
44
# File 'lib/tacokit/utils.rb', line 42

def camel_join(*paths)
  path_join paths.map { |p| camel_path(p) }
end

#camel_path(path) ⇒ Object Also known as: camp


37
38
39
# File 'lib/tacokit/utils.rb', line 37

def camel_path(path)
  camelize(path.to_s, :lower)
end

#camelize(string, lower = false) ⇒ Object

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

#constantize(class_name) ⇒ Object


50
51
52
53
54
# File 'lib/tacokit/utils.rb', line 50

def constantize(class_name)
  Tacokit.const_get(camelize(singularize(class_name.to_s)))
rescue NameError
  nil
end

#deep_transform_keys(hash, &block) ⇒ Object


4
5
6
# File 'lib/tacokit/utils.rb', line 4

def deep_transform_keys(hash, &block)
  _deep_transform_keys_in_object(hash, &block)
end

#extract_options(*args) ⇒ Object


8
9
10
11
# 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) ⇒ Object


46
47
48
# File 'lib/tacokit/utils.rb', line 46

def path_join(*paths)
  paths.join("/")
end

#present?(obj) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
# File 'lib/tacokit/utils.rb', line 33

def present?(obj)
  !blank?(obj)
end

#singularize(string) ⇒ Object


56
57
58
# File 'lib/tacokit/utils.rb', line 56

def singularize(string)
  string.gsub(/s$/, "")
end

#underscore(string) ⇒ Object

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