Class: Object

Inherits:
BasicObject
Defined in:
lib/nrser/rash/core_ext/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rash_formatterObject

Monkey-patch to add an instance variable named rash_formatter to Object to control how that object is formatted for printing if it's the final value returned to NRSER::Rash::CLI.call.

The value should either be a symbol identifying a method on NRSER::Rash::Formatters to call or a callable that takes the object as it's only argument and returns a formatted String.



10
11
12
# File 'lib/nrser/rash/core_ext/object.rb', line 10

def rash_formatter
  @rash_formatter
end

Instance Method Details

#rash_formatter_tap(formatter = (default = true; nil), &block) ⇒ Object

shortcut method to set the rash_formatter and return the object. this method is hacked up a bit to support the following syntax:

{:x => 1, :y => 2}.rash_formatter_tap :json

{:x => 1, :y => 2}.rash_formatter_tap do |obj|
  "here's the object: #{ obj.inspect }"
end

which returns the object itself, allowing it to easily be tacked on to the last statement in a method. it's equivalent to:

{:x => 1, :y => 2}.tap {|obj| obj.rash_formatter = :json}

{:x => 1, :y => 2}.tap do |obj|
  obj.rash_formatter = Proc.new do |obj|
    "here's the object: #{ obj.inpsect }"
  end
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nrser/rash/core_ext/object.rb', line 32

def rash_formatter_tap(formatter = (default = true; nil), &block)
  if default
    if block.nil?
      # neither the `formatter` arg or a block were supplied, so
      # the method is acting as an attribute reader, return the
      # value
      raise ArgumentError.new "must provide an argument or block."
    else
      # only a block was supplied, set it as the formatter and
      # return the object
      @rash_formatter = block
      self
    end
  else
    if block
      raise ArgumentError.new "can't provide an argument and block."
    else
      # only `formatter` arg was supplied, set it and return the object
      @rash_formatter = formatter
      self
    end
  end
end