Module: SmoothOperator::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/smooth_operator/helpers.rb

Instance Method Summary collapse

Instance Method Details

#absolute_path?(string) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/smooth_operator/helpers.rb', line 76

def absolute_path?(string)
  present?(string) && string[0] == '/'
end

#blank?(object) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
# File 'lib/smooth_operator/helpers.rb', line 61

def blank?(object)
  case object
  when String
    object.to_s == ''
  when Array, Hash
    object.empty?
  else
    object.nil?
  end
end

#duplicate(object) ⇒ Object



57
58
59
# File 'lib/smooth_operator/helpers.rb', line 57

def duplicate(object)
  object.dup rescue object
end

#generated_idObject



7
8
9
# File 'lib/smooth_operator/helpers.rb', line 7

def generated_id
  Time.now.to_f.to_s.split('.')[1]
end

#get_instance_variable(object, variable, default_value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/smooth_operator/helpers.rb', line 25

def get_instance_variable(object, variable, default_value)
  instance_var = object.instance_variable_get("@#{variable}")

  return instance_var unless instance_var.nil?

  instance_var = (super_method(object, variable) || default_value)

  if instance_var.class == Class
    object.instance_variable_set("@#{variable}", instance_var)
  else
    object.instance_variable_set("@#{variable}", duplicate(instance_var))
  end
end

#has_primary_key?(object) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/smooth_operator/helpers.rb', line 15

def has_primary_key?(object)
  blank? primary_key(object)
end

#plural?(string) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/smooth_operator/helpers.rb', line 52

def plural?(string)
  string = string.to_s
  string == string.pluralize
end

#present?(object) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/smooth_operator/helpers.rb', line 72

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

#primary_key(object) ⇒ Object



11
12
13
# File 'lib/smooth_operator/helpers.rb', line 11

def primary_key(object)
  object.internal_data_get(object.class.primary_key)
end

#remove_initial_slash(string) ⇒ Object



80
81
82
# File 'lib/smooth_operator/helpers.rb', line 80

def remove_initial_slash(string)
  string[1..-1]
end

#stringify_keys(hash) ⇒ Object



39
40
41
42
43
# File 'lib/smooth_operator/helpers.rb', line 39

def stringify_keys(hash)
  stringified_hash = {}
  hash.keys.each { |key| stringified_hash[key.to_s] = hash[key] }
  stringified_hash
end

#super_method(object, method_name, *args) ⇒ Object



19
20
21
22
23
# File 'lib/smooth_operator/helpers.rb', line 19

def super_method(object, method_name, *args)
  if object.superclass.respond_to?(method_name)
    object.superclass.send(method_name, *args)
  end
end

#symbolyze_keys(hash) ⇒ Object



45
46
47
48
49
50
# File 'lib/smooth_operator/helpers.rb', line 45

def symbolyze_keys(hash)
  hash.keys.reduce({}) do |cloned_hash, key|
    cloned_hash[key.to_sym] = hash[key]
    cloned_hash
  end
end