Module: SmoothOperator::Helpers
Instance Method Summary collapse
- #absolute_path?(string) ⇒ Boolean
- #blank?(object) ⇒ Boolean
- #duplicate(object) ⇒ Object
- #get_instance_variable(object, variable, default_value) ⇒ Object
- #plural?(string) ⇒ Boolean
- #present?(object) ⇒ Boolean
- #remove_initial_slash(string) ⇒ Object
- #safe_call(object, method, *args) ⇒ Object
- #stringify_keys(hash) ⇒ Object
- #super_method(object, method_name, *args) ⇒ Object
- #symbolyze_keys(hash) ⇒ Object
Instance Method Details
#absolute_path?(string) ⇒ Boolean
70 71 72 |
# File 'lib/smooth_operator/helpers.rb', line 70 def absolute_path?(string) present?(string) && string[0] == '/' end |
#blank?(object) ⇒ Boolean
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/smooth_operator/helpers.rb', line 55 def blank?(object) case object when String object.to_s == '' when Array object.empty? else object.nil? end end |
#duplicate(object) ⇒ Object
51 52 53 |
# File 'lib/smooth_operator/helpers.rb', line 51 def duplicate(object) object.dup rescue object end |
#get_instance_variable(object, variable, default_value) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/smooth_operator/helpers.rb', line 19 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 |
#plural?(string) ⇒ Boolean
46 47 48 49 |
# File 'lib/smooth_operator/helpers.rb', line 46 def plural?(string) string = string.to_s string == string.pluralize end |
#present?(object) ⇒ Boolean
66 67 68 |
# File 'lib/smooth_operator/helpers.rb', line 66 def present?(object) !blank?(object) end |
#remove_initial_slash(string) ⇒ Object
74 75 76 |
# File 'lib/smooth_operator/helpers.rb', line 74 def remove_initial_slash(string) string[1..-1] end |
#safe_call(object, method, *args) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/smooth_operator/helpers.rb', line 7 def safe_call(object, method, *args) if object.respond_to?(method) object.send(method, *args) else false end end |
#stringify_keys(hash) ⇒ Object
33 34 35 36 37 |
# File 'lib/smooth_operator/helpers.rb', line 33 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
15 16 17 |
# File 'lib/smooth_operator/helpers.rb', line 15 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
39 40 41 42 43 44 |
# File 'lib/smooth_operator/helpers.rb', line 39 def symbolyze_keys(hash) hash.keys.reduce({}) do |cloned_hash, key| cloned_hash[key.to_sym] = hash[key] cloned_hash end end |