Module: ObjectAttorney::Helpers

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

Instance Method Summary collapse

Instance Method Details

#has_errors_method?(object) ⇒ Boolean



26
27
28
# File 'lib/object_attorney/helpers.rb', line 26

def has_errors_method?(object)
  object.present? && object.respond_to?(:errors) && !object.errors.nil?
end

#is_integer?(string) ⇒ Boolean



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

def is_integer?(string)
  string.match(/^(\d)+$/)
end

#plural?(string) ⇒ Boolean



16
17
18
19
# File 'lib/object_attorney/helpers.rb', line 16

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

#singularize(class_name) ⇒ Object



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

def singularize(class_name)
  class_name = class_name.to_s
  plural?(class_name) ? class_name.singularize : class_name
end

#try_or_return(object, method, default_value) ⇒ Object



21
22
23
24
# File 'lib/object_attorney/helpers.rb', line 21

def try_or_return(object, method, default_value)
  returning_value = object.try(method)
  returning_value.nil? ? default_value : returning_value
end