Sha256: f74d3937cb22dd3559b9dc2f202bf9b9e85eb25817b5289554c2f7fc6933436b
Contents?: true
Size: 1.87 KB
Versions: 4
Compression:
Stored size: 1.87 KB
Contents
class Object # 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`. # attr_accessor :rash_formatter # 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 # 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 end # class Object
Version data entries
4 entries across 4 versions & 1 rubygems