Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/jinx/helpers/enumerate.rb,
lib/jinx/helpers/validation.rb,
lib/jinx/helpers/collections.rb,
lib/jinx/helpers/pretty_print.rb,
lib/jinx/helpers/transitive_closure.rb
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Returns whether this object is nil, false, empty, or a whitespace string.
-
#collection? ⇒ Boolean
Whether this object is a Jinx::Collection.
-
#enumerate {|item| ... } ⇒ Object
This base implementation of
enumerate
calls the given block on self unless this object in nil. -
#nil_or_empty? ⇒ Boolean
Returns whether this object is nil, empty, or a whitespace string.
-
#pp_s(opts = nil) ⇒ String
Formats this object with the standard PrettyPrint.
-
#print_class_and_id ⇒ String
(also: #qp)
This object’s class demodulized name and object id.
-
#to_enum ⇒ Enumerable
Returns an enumerator on this Object.
-
#transitive_closure(method = nil) {|node| ... } ⇒ Object
Returns the transitive closure over a method or block.
Instance Method Details
#blank? ⇒ Boolean
Returns whether this object is nil, false, empty, or a whitespace string. This method is borrowed from Rails ActiveSupport.
18 19 20 |
# File 'lib/jinx/helpers/validation.rb', line 18 def blank? respond_to?(:empty?) ? empty? : !self end |
#collection? ⇒ Boolean
Returns whether this object is a Jinx::Collection.
18 19 20 |
# File 'lib/jinx/helpers/collections.rb', line 18 def collection? Jinx::Collection === self end |
#enumerate {|item| ... } ⇒ Object
This base implementation of enumerate
calls the given block on self unless this object in nil. If this object is nil, this method is a no-op.
9 10 11 |
# File 'lib/jinx/helpers/enumerate.rb', line 9 def enumerate yield(self) unless nil? end |
#nil_or_empty? ⇒ Boolean
Returns whether this object is nil, empty, or a whitespace string. This method differs from #blank? in that false
is an allowed value.
30 31 32 |
# File 'lib/jinx/helpers/validation.rb', line 30 def nil_or_empty? blank? and self != false end |
#pp_s(opts = nil) ⇒ String
Formats this object with the standard PrettyPrint.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jinx/helpers/pretty_print.rb', line 62 def pp_s(opts=nil) s = StringIO.new if Options.get(:single_line, opts) then PP.singleline_pp(self, s) else PP.pp(self, s) end s.rewind s.read.chomp end |
#print_class_and_id ⇒ String Also known as: qp
Returns this object’s class demodulized name and object id.
50 51 52 |
# File 'lib/jinx/helpers/pretty_print.rb', line 50 def print_class_and_id "#{self.class.qp}@#{object_id}" end |
#to_enum ⇒ Enumerable
Returns an enumerator on this Object. This default implementation returns an Enumerable::Enumerator on enumerate.
17 18 19 |
# File 'lib/jinx/helpers/enumerate.rb', line 17 def to_enum Enumerable::Enumerator.new(self, :enumerate) end |
#transitive_closure(method = nil) {|node| ... } ⇒ Object
Returns the transitive closure over a method or block. This method returns an array partially ordered by the children method or block, i.e. each node occurs before all other nodes referenced directly or indirectly by the children.
If a method symbol or name is provided, then that method is called. Otherwise, the block is called. In either case, the call is expected to return an object or Enumerable of objects which also respond to the method or block.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jinx/helpers/transitive_closure.rb', line 29 def transitive_closure(method=nil) raise ArgumentError.new("Missing both a method argument and a block") if method.nil? and not block_given? # If there is a method argument, then the transitive closure is based on that method. # Otherwise, visit the closure in reverse depth-first order. if method then transitive_closure() { |node| node.send(method) } else Jinx::Visitor.new(:depth_first) { |node| yield node }.to_enum(self).to_a.reverse end end |