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

Instance Method Details

#blank?Boolean

Returns whether this object is nil, false, empty, or a whitespace string. This method is borrowed from Rails ActiveSupport.

Examples:

''.blank? => true
nil.blank? => true
false.blank? => true
[].blank? => true
[[]].blank? => false

Returns:

  • (Boolean)

    whether this object is nil, false, empty, or a whitespace string

See Also:

  • Object.{{#nil_or_empty?}


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.

Returns:



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.

Yields:

  • (item)

    the block to apply to this object

Yield Parameters:

  • item

    self



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.

Examples:

''.nil_or_empty? => true
nil.nil_or_empty? => true
false.nil_or_empty? => false

Returns:

  • (Boolean)

    whether this object is nil, empty, or a whitespace string



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.

Parameters:

  • opts (Hash, Symbol, nil) (defaults to: nil)

    the print options

Options Hash (opts):

  • :single_line (Boolean)

    print the output on a single line

Returns:

  • (String)

    the formatted print result



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

Returns this object’s class demodulized name and object id.

Returns:

  • (String)

    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_enumEnumerable

Returns an enumerator on this Object. This default implementation returns an Enumerable::Enumerator on enumerate.

Returns:

  • (Enumerable)

    this object as an enumerable item



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.

Examples:

class Node
  attr_reader :parent, :children
  def initialize(name, parent=nil)
    super()
    @name = name
    @parent = parent
    @children = []
    parent.children << self if parent
  end
end
a = Node.new('a'); b = Node.new('b', a), c = Node.new('c', a); d = Node.new('d', c)
a.transitive_closure { |node| node.children }.to_a.join(", ") #=> a, b, c, d
a.transitive_closure(:children).to_a.join(", ") #=> a, b, c, d

Parameters:

  • method (Symbol, nil) (defaults to: nil)

    the child reference, or nil if a block is given

Yields:

  • (node)

    the parent node’s children

Yield Parameters:

  • node

    the parent node

Raises:

  • (ArgumentError)


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