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

Returns:

  • (Boolean)


30
31
32
# File 'lib/object_attorney/helpers.rb', line 30

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

#is_integer?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#marked_for_destruction?(object) ⇒ Boolean

Returns:

  • (Boolean)


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

def marked_for_destruction?(object)
  object.respond_to?(:marked_for_destruction?) ? object.marked_for_destruction? : false
end

#plural?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#singularize(class_name) ⇒ Object



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

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



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

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