Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/eac_ruby_utils/patches/object/debug.rb,
lib/eac_ruby_utils/patches/object/if_nil.rb,
lib/eac_ruby_utils/patches/object/to_uri.rb,
lib/eac_ruby_utils/patches/object/asserts.rb,
lib/eac_ruby_utils/patches/object/compact.rb,
lib/eac_ruby_utils/patches/object/to_bool.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,
lib/eac_ruby_utils/patches/object/call_if_proc.rb,
lib/eac_ruby_utils/patches/object/i18n_translate.rb,
lib/eac_ruby_utils/patches/object/recursive_build.rb
Instance Method Summary collapse
-
#assert_argument(klass, argument_name = 'unknown_argument_name') ⇒ Object
Raises a ArgumentError if
self
is not aklass
. -
#assert_count(count, argument_name = 'unknown_argument_name') ⇒ Object
Raises a ArgumentError if
self.count
is not equal tocount
. -
#call_if_proc ⇒ Object
If
self
is a Proc, return the value of.call
. - #compact(*attributes) ⇒ EacRubyUtils::Compact
- #compact_debug(*attributes) ⇒ void
- #compact_to_a(*attributes) ⇒ Array
- #compact_to_h(*attributes) ⇒ Hash
- #i18n_translate(entry_suffix, values = {}) ⇒ Object
-
#if_blank ⇒ Object
yield
ifself
is blank. -
#if_nil ⇒ Object
yield
ifself
is nil,self
otherwise. - #if_not_nil(default_value = nil) ⇒ Object
- #if_present(default_value = nil) ⇒ Object
-
#if_respond(method_name, default_value = nil) ⇒ Object
otherwise.
- #on_i18n_locale(locale) ⇒ Object
- #pretty_debug(options = {}) ⇒ Object
- #print_debug(options = {}) ⇒ Object
- #print_debug_label(label) ⇒ Object
- #print_debug_options(options) ⇒ Object
- #print_debug_title(title) ⇒ Object
- #raise_debug ⇒ Object
-
#recursive_build(&neighbors_block) ⇒ Enumerable
Equivalent to EacRubyUtils::RecursiveBuilder.new(self, &neighbors_block).result.
-
#to_bool ⇒ Boolean
Shortcut to +EacRubyUtils::Boolean.parse(self).
- #to_debug ⇒ Object
-
#to_pathname ⇒ Pathname
Convert
self
to String and then to Pathname. -
#to_uri ⇒ Addressable::URI
Convert
self
to String and then to Addressable::URI.
Instance Method Details
#assert_argument(klass, argument_name = 'unknown_argument_name') ⇒ Object
Raises a ArgumentError if self
is not a klass
.
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 |
#assert_count(count, argument_name = 'unknown_argument_name') ⇒ Object
Raises a ArgumentError if self.count
is not equal to count
.
18 19 20 21 22 23 24 |
# File 'lib/eac_ruby_utils/patches/object/asserts.rb', line 18 def assert_count(count, argument_name = 'unknown_argument_name') return self if self.count == count raise ::ArgumentError, "Argument \"#{argument_name}\" has wrong elements count" \ "(Actual: #{self.count}, Required: #{count})" end |
#call_if_proc ⇒ Object
If self
is a Proc, return the value of .call
. If not, return self
.
8 9 10 |
# File 'lib/eac_ruby_utils/patches/object/call_if_proc.rb', line 8 def call_if_proc is_a?(::Proc) ? call : self end |
#compact(*attributes) ⇒ EacRubyUtils::Compact
7 8 9 |
# File 'lib/eac_ruby_utils/patches/object/compact.rb', line 7 def compact(*attributes) ::EacRubyUtils::Compact.new(self, attributes) end |
#compact_debug(*attributes) ⇒ void
This method returns an undefined value.
10 11 12 13 14 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 10 def compact_debug(*attributes) ::EacRubyUtils::Compact.new(self, attributes).to_h.each do |name, value| value.print_debug(label: name) end end |
#compact_to_a(*attributes) ⇒ Array
12 13 14 |
# File 'lib/eac_ruby_utils/patches/object/compact.rb', line 12 def compact_to_a(*attributes) compact(*attributes).to_a end |
#compact_to_h(*attributes) ⇒ Hash
17 18 19 |
# File 'lib/eac_ruby_utils/patches/object/compact.rb', line 17 def compact_to_h(*attributes) compact(*attributes).to_h end |
#i18n_translate(entry_suffix, values = {}) ⇒ Object
6 7 8 |
# File 'lib/eac_ruby_utils/patches/object/i18n_translate.rb', line 6 def i18n_translate(entry_suffix, values = {}) self.class.i18n_translate(entry_suffix, values) end |
#if_blank ⇒ Object
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_nil ⇒ Object
Returns yield
if self
is nil, self
otherwise.
12 13 14 15 16 |
# File 'lib/eac_ruby_utils/patches/object/if_nil.rb', line 12 def if_nil return yield if nil? && block_given? self end |
#if_not_nil(default_value = nil) ⇒ Object
5 6 7 8 9 |
# File 'lib/eac_ruby_utils/patches/object/if_nil.rb', line 5 def if_not_nil(default_value = nil) return default_value if nil? block_given? ? yield(self) : 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 |
#on_i18n_locale(locale) ⇒ Object
10 11 12 |
# File 'lib/eac_ruby_utils/patches/object/i18n_translate.rb', line 10 def on_i18n_locale(locale) self.class.on_i18n_locale(locale) end |
#pretty_debug(options = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 16 def pretty_debug( = {}) () $stderr.write(pretty_inspect) self end |
#print_debug(options = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 24 def print_debug( = {}) () $stderr.write("#{to_debug}\n") self end |
#print_debug_label(label) ⇒ Object
31 32 33 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 31 def print_debug_label(label) $stderr.write("#{label}: ") end |
#print_debug_options(options) ⇒ Object
35 36 37 38 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 35 def () [:title].if_present { |v| print_debug_title(v) } [:label].if_present { |v| print_debug_label(v) } end |
#print_debug_title(title) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 40 def print_debug_title(title) title = title.to_s char = '=' $stderr.write("#{char * (4 + title.length)}\n") $stderr.write("#{char} #{title} #{char}\n") $stderr.write("#{char * (4 + title.length)}\n") end |
#raise_debug ⇒ Object
52 53 54 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 52 def raise_debug raise to_debug end |
#recursive_build(&neighbors_block) ⇒ Enumerable
Equivalent to EacRubyUtils::RecursiveBuilder.new(self, &neighbors_block).result.
9 10 11 |
# File 'lib/eac_ruby_utils/patches/object/recursive_build.rb', line 9 def recursive_build(&neighbors_block) ::EacRubyUtils::RecursiveBuilder.new(self, &neighbors_block).result end |
#to_bool ⇒ Boolean
Shortcut to +EacRubyUtils::Boolean.parse(self).
9 10 11 |
# File 'lib/eac_ruby_utils/patches/object/to_bool.rb', line 9 def to_bool ::EacRubyUtils::Boolean.parse(self) end |
#to_debug ⇒ Object
48 49 50 |
# File 'lib/eac_ruby_utils/patches/object/debug.rb', line 48 def to_debug "|#{::Object.instance_method(:to_s).bind(self).call}|#{self}|" end |
#to_pathname ⇒ Pathname
Convert self
to String and then to Pathname. Return nil if self
is blank?
.
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 |
#to_uri ⇒ Addressable::URI
Convert self
to String and then to Addressable::URI. Return nil if self
is blank?
.
10 11 12 13 14 |
# File 'lib/eac_ruby_utils/patches/object/to_uri.rb', line 10 def to_uri return self if is_a?(::Addressable::URI) to_s.blank? ? nil : ::Addressable::URI.parse(to_s) end |