Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add configuration block to Letters

  • Loading branch information...
commit e6e4718c637dc8054df9e7ad11732a25e2807dde 1 parent 6e2d91b
@davejacobs authored
View
12 README.md
@@ -307,4 +307,14 @@ The `o` method takes options, too, so you can add a prefix message to the output
</tr>
</table>
-See full documentation at http://lettersrb.com/api
+See the [full documentation](http://lettersrb.com/api) for examples and more detailed explanations.
+
+### Configuration ###
+
+Lastly, you can tune and tweak each Letters method to default to your own tastes. Want to name put files somewhere else? No problem. Don't like YAML? Default `f` to use Pretty Print instead! The world of defaults is your oyster.
+
+```ruby
+Letters.config do
+ f :format => "pp", :name => "my-special-file"
+end
+```
View
44 lib/letters/config.rb
@@ -0,0 +1,44 @@
+require "letters/assertion_error"
+
+module Letters
+ def self.defaults
+ defaults = {
+ a: { error_class: Letters::AssertionError },
+ d1: { dup: false },
+ d2: { format: "ap" },
+ f: { format: "yaml", name: "log" },
+ k: { max: 0 },
+ l: { level: "info", format: "yaml" },
+ o: { format: "ap", stream: $stdout },
+ t: { time_format: "millis" }
+ }
+
+ defaults.tap do |hash|
+ hash.default_proc = lambda {|h, k| h[k] = Hash.new }
+ end
+ end
+
+ def self.user_defaults
+ @user_defaults ||= Hash.new {|h, k| h[k] = Hash.new }
@muescha
muescha added a note

maybe with a Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) } you dont need the line 40

@davejacobs Owner

Could you explain a little further?

@muescha
muescha added a note

if you do this change then you not need the 'module config' which prefills the user_defaults for a-z letters with empty hash

@davejacobs Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+
+ def self.defaults_with(letter, opts={})
+ defaults[letter].merge(user_defaults[letter]).merge(opts)
+ end
+
+ def self.reset_config!
+ user_defaults.clear
+ end
+
+ def self.config(&block)
+ Letters::Config.class_eval(&block)
+ end
+
+ module Config
+ ("a".."z").each do |letter|
+ define_singleton_method(letter) do |opts={}|
+ Letters.user_defaults[letter.to_sym] = opts
+ end
+ end
+ end
+end
View
19 lib/letters/core_ext.rb
@@ -1,10 +1,10 @@
+require "letters/config"
require "letters/helpers"
require "letters/diff"
-require "letters/assertion_error"
+require "letters/time_formats"
require "letters/empty_error"
require "letters/kill_error"
require "letters/nil_error"
-require "letters/time_formats"
module Letters
module CoreExt
@@ -12,7 +12,7 @@ module CoreExt
# Assert
def a(opts={}, &block)
- opts = { error_class: AssertionError }.merge opts
+ opts = Letters.defaults_with(:a, opts)
tap do |o|
Helpers.message opts
if block_given? && !o.instance_eval(&block)
@@ -52,8 +52,7 @@ def d1
# Diff 2
def d2(opts={})
- require "awesome_print"
- opts = { format: "ap" }.merge opts
+ opts = Letters.defaults_with(:d2, opts)
tap do |o|
diff = Helpers.diff(Letters.object_for_diff, o)
Helpers.message opts
@@ -73,7 +72,7 @@ def e(opts={})
# File
def f(opts={})
- opts = { format: "yaml", name: "log" }.merge opts
+ opts = Letters.defaults_with(:f, opts)
tap do |o|
suffixes = [""] + (1..50).to_a
deduper = suffixes.detect {|x| !File.directory? "#{opts[:name]}#{x}" }
@@ -96,7 +95,7 @@ def j(&block)
# Kill
def k(opts={})
- opts = { max: 0 }.merge(opts)
+ opts = Letters.defaults_with(:k, opts)
opts.merge! :error_class => KillError
tap do |o|
@letters_kill_count ||= 0
@@ -109,7 +108,7 @@ def k(opts={})
# Log
def l(opts={})
- opts = { level: "info", format: "yaml" }.merge opts
+ opts = Letters.defaults_with(:l, opts)
tap do |o|
begin
logger.send(opts[:level], opts[:message]) if opts[:message]
@@ -141,7 +140,7 @@ def n(opts={})
# Print to STDOUT
def o(opts={}, &block)
- opts = { format: "ap", stream: $stdout }.merge opts
+ opts = Letters.defaults_with(:o, opts)
tap do |o|
Helpers.message opts
obj = block_given? ? o.instance_eval(&block) : o
@@ -167,7 +166,7 @@ def s(level=nil)
# Timestamp
def t(opts={})
- opts = { time_format: "millis" }.merge opts
+ opts = Letters.defaults_with(:t, opts)
tap do
Helpers.message opts
Helpers.out Time.now.to_s(opts[:time_format].to_sym), opts
View
33 spec/letters/config_spec.rb
@@ -0,0 +1,33 @@
+require "spec_helper"
+
+module Letters
+ describe "configuration" do
+ let(:hash) { { a: "b" } }
+
+ before do
+ Letters.config do
+ f :format => "pp"
+ end
+ File.exist?("log").should be_false
+ end
+
+ after do
+ FileUtils.rm "log"
+ end
+
+ describe ".config" do
+ it "allows default argument configuration" do
+ hash.f
+ File.read("log").should == hash.pretty_inspect
+ end
+ end
+
+ describe ".reset_config!" do
+ it "clears out the config hash" do
+ Letters.reset_config!
+ hash.f
+ File.read("log").should == hash.to_yaml
+ end
+ end
+ end
+end
View
1  spec/spec_helper.rb
@@ -1,3 +1,4 @@
require "letters"
require "awesome_print"
require "timecop"
+require "fileutils"
Please sign in to comment.
Something went wrong with that request. Please try again.