Class: Object

Inherits:
BasicObject
Defined in:
lib/eac_ruby_utils/patches/object/asserts.rb,
lib/eac_ruby_utils/patches/object/template.rb,
lib/eac_ruby_utils/patches/object/if_present.rb,
lib/eac_ruby_utils/patches/object/if_respond.rb,
lib/eac_ruby_utils/patches/object/to_pathname.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.templateObject


8
9
10
# File 'lib/eac_ruby_utils/patches/object/template.rb', line 8

def template
  @template ||= ::EacRubyUtils::Templates::Searcher.default.template(name.underscore)
end

Instance Method Details

#assert_argument(klass, argument_name = 'unknown_argument_name') ⇒ Object

Raises a ArgumentError if self is not a klass.

Returns:

  • self

Raises:

  • (::ArgumentError)

7
8
9
10
11
12
13
# File 'lib/eac_ruby_utils/patches/object/asserts.rb', line 7

def assert_argument(klass, argument_name = 'unknown_argument_name')
  return self if is_a?(klass)

  raise ::ArgumentError,
        "Argument \"#{argument_name}\" is not a #{klass}" \
        "(Actual class: #{self.class}, actual value: #{self})"
end

#if_blankObject

Returns yield if self is blank.

Returns:

  • yield if self is blank.


14
15
16
17
18
# File 'lib/eac_ruby_utils/patches/object/if_present.rb', line 14

def if_blank
  return yield if blank? && block_given?

  self
end

#if_present(default_value = nil) ⇒ Object


7
8
9
10
11
# File 'lib/eac_ruby_utils/patches/object/if_present.rb', line 7

def if_present(default_value = nil)
  return default_value if blank?

  block_given? ? yield(self) : self
end

#if_respond(method_name, default_value = nil) ⇒ Object

otherwise.


8
9
10
11
12
13
14
# File 'lib/eac_ruby_utils/patches/object/if_respond.rb', line 8

def if_respond(method_name, default_value = nil)
  return default_value unless respond_to?(method_name)

  value = send(method_name)

  block_given? ? yield(value) : value
end

#templateObject


13
14
15
# File 'lib/eac_ruby_utils/patches/object/template.rb', line 13

def template
  self.class.template
end

#to_pathnamePathname

Convert self to String and then to Pathname. Return nil if self is blank?.

Returns:


10
11
12
13
14
# File 'lib/eac_ruby_utils/patches/object/to_pathname.rb', line 10

def to_pathname
  return self if is_a?(::Pathname)

  to_s.blank? ? nil : ::Pathname.new(to_s)
end