Module: SmoothOperator::Helpers

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

Instance Method Summary collapse

Instance Method Details

#blank?(object) ⇒ Boolean

Returns:



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

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

#duplicate(object) ⇒ Object



38
39
40
# File 'lib/smooth_operator/helpers.rb', line 38

def duplicate(object)
  object.dup rescue object
end

#get_instance_variable(object, variable, default_value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/smooth_operator/helpers.rb', line 11

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

#present?(object) ⇒ Boolean

Returns:



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

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

#stringify_keys(hash) ⇒ Object



25
26
27
28
29
# File 'lib/smooth_operator/helpers.rb', line 25

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



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

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

#symbolyze_keys(hash) ⇒ Object



31
32
33
34
35
36
# File 'lib/smooth_operator/helpers.rb', line 31

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