From 0bcac1a76d330e55690ee778b2847c75c676561f Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Sun, 20 Jul 2014 01:33:49 -0400 Subject: [PATCH] Adding a basic custom config file * Also adding in a logger * Added a sample for custom config * Saving a png now creates _output --- lib/squib.rb | 12 +++++++++++- lib/squib/api/save.rb | 2 +- lib/squib/deck.rb | 20 ++++++++++++++++++-- samples/custom-config.rb | 6 ++++++ samples/custom-config.yml | 1 + 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 samples/custom-config.rb create mode 100644 samples/custom-config.yml diff --git a/lib/squib.rb b/lib/squib.rb index 86c9bd9..84b3788 100644 --- a/lib/squib.rb +++ b/lib/squib.rb @@ -1,4 +1,14 @@ +require 'logger' require 'squib/version' require 'squib/commands/new' require 'squib/deck' -require 'squib/card' \ No newline at end of file +require 'squib/card' + +module Squib + + def logger + @logger ||= Logger.new(STDOUT) + end + module_function :logger + +end \ No newline at end of file diff --git a/lib/squib/api/save.rb b/lib/squib/api/save.rb index 4ac3c40..49d880a 100644 --- a/lib/squib/api/save.rb +++ b/lib/squib/api/save.rb @@ -8,7 +8,7 @@ module Squib end def save_png(range: :all, dir: "_output", prefix: 'card_') - range = rangeify(range) + range = rangeify(range); dir = dirify(dir, allow_create: true) range.each { |i| @cards[i].save_png(i, dir, prefix) } end diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index fb63f34..5e9db7f 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -1,3 +1,4 @@ +require 'yaml' require 'squib/card' require 'squib/input_helpers' require 'squib/constants' @@ -11,26 +12,41 @@ module Squib attr_reader :cards attr_reader :text_hint - def initialize(width: 825, height: 1125, cards: 1, &block) + def initialize(width: 825, height: 1125, cards: 1, config: 'config.yml', &block) @width=width; @height=height @dpi = 300 @font = 'Sans 36' @cards = [] cards.times{ @cards << Squib::Card.new(self, width, height) } + load_config(config) if block_given? instance_eval(&block) end end + # API: Accesses the array of cards in the deck + # @api public def [](key) @cards[key] end - #For Enumerable + # Public: Accesses each card of the array in the deck + # @api def each(&block) @cards.each { |card| block.call(card) } end + # Internal: Load the configuration file, if exists, + # overriding hardcoded defaults + # @api private + def load_config(file) + if File.exists? file + if config = YAML.load_file(file) + @dpi = config['dpi'].to_i + end + end + end + ################## ### PUBLIC API ### ################## diff --git a/samples/custom-config.rb b/samples/custom-config.rb new file mode 100644 index 0000000..5efddfe --- /dev/null +++ b/samples/custom-config.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby +require 'squib' + +Squib::Deck.new(config: 'custom-config.yml') do + +end \ No newline at end of file diff --git a/samples/custom-config.yml b/samples/custom-config.yml new file mode 100644 index 0000000..b3a8568 --- /dev/null +++ b/samples/custom-config.yml @@ -0,0 +1 @@ +dpi: 300 \ No newline at end of file