Sha256: d4819f1b9214fe285f452a50c27c0d768326c9b0dcd45c78c90bb7d77339868a

Contents?: true

Size: 1.19 KB

Versions: 22

Compression:

Stored size: 1.19 KB

Contents

class Array
  def args
    flatten.map{|a| a.args}.flatten
  end
  
  def last_option
    default = self.last_arg
    last = self.flatten.last
    last.kind_of?(Hash) ? last : default    
  end  
  
  def last_arg default = {}
    last = self.flatten.last
    last.kind_of?(Hash) ? last : default
  end

  def last_arg_value default = nil
    last = self.flatten.last
    raise ArgumentError, "Default value must be a Hash, was #{default}" if !default.kind_of? Hash
    key = default.keys.first
    return default[key] if !last.kind_of? Hash
    last[key] ? last[key] : default[key]
  end  
end

class Symbol
  def args
    [to_s]
  end
end

class String
  def args
    (self =~ /\w+\s+\w+/) ? self.split : self
  end
end

def last_option *args
  default = last_arg({}, *args)
  last = args.flatten.last
  last.kind_of?(Hash) ? last : default    
end

def last_arg default, *args
  last = args.flatten.last
  last.kind_of?(Hash) ? last : default
end

def last_arg_value default, *args
  last = args.flatten.last
  raise ArgumentError, "Default value must be a Hash, was #{default}" if !default.kind_of? Hash
  key = default.keys.first
  return default[key] if !last.kind_of? Hash
  last[key] ? last[key] : default[key]
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
sugar-high-0.6.3 lib/sugar-high/arguments.rb
sugar-high-0.6.2.1 lib/sugar-high/arguments.rb
sugar-high-0.6.1 lib/sugar-high/arguments.rb
sugar-high-0.6.0 lib/sugar-high/arguments.rb
sugar-high-0.5.5 lib/sugar-high/arguments.rb
sugar-high-0.5.4 lib/sugar-high/arguments.rb
sugar-high-0.5.3 lib/sugar-high/arguments.rb
sugar-high-0.5.1 lib/sugar-high/arguments.rb
sugar-high-0.5.0 lib/sugar-high/arguments.rb
sugar-high-0.4.9.5 lib/sugar-high/arguments.rb
sugar-high-0.4.9.3 lib/sugar-high/arguments.rb
sugar-high-0.4.9.2 lib/sugar-high/arguments.rb
sugar-high-0.4.9.1 lib/sugar-high/arguments.rb
sugar-high-0.4.9 lib/sugar-high/arguments.rb
sugar-high-0.4.8 lib/sugar-high/arguments.rb
sugar-high-0.4.7 lib/sugar-high/arguments.rb
sugar-high-0.4.6.4 lib/sugar-high/arguments.rb
sugar-high-0.4.6.3 lib/sugar-high/arguments.rb
sugar-high-0.4.6.2 lib/sugar-high/arguments.rb
sugar-high-0.4.6.1 lib/sugar-high/arguments.rb