Browse Source
With the new design, we take the load off of deck.rb and simply delegate methods over to the new conf.rb. This means that things like `antialias` is now available as a method to the normal Squib scripts for easy checking (without being mutable). Squib::Conf also handles parsing and defaults, and any potential input validation we need to do in the future. Typographer is also set up now as a deck-wide configuration. This may change in the future if we want typography customization per-command, although that seems like a strange use case. Lots of tests for this one, and lots of cross-cutting concerns in this commit. This commit also includes some tweaks to rspec tests, including tagging of slow tests for a `rake spec_fastonly` Conflicts: spec/samples/samples_regression_spec.rb spec/spec_helper.rb squib.sublime-projectdev
21 changed files with 330 additions and 202 deletions
@ -0,0 +1,114 @@
|
||||
require 'squib' |
||||
require 'forwardable' |
||||
require 'squib/args/typographer' |
||||
|
||||
module Squib |
||||
# @api private |
||||
class Conf |
||||
|
||||
DEFAULTS = { |
||||
'antialias' => 'best', |
||||
'backend' => 'memory', |
||||
'count_format' => SYSTEM_DEFAULTS[:count_format], |
||||
'custom_colors' => {}, |
||||
'dir' => SYSTEM_DEFAULTS[:dir], |
||||
'hint' => :none, |
||||
'img_dir' => '.', |
||||
'progress_bars' => false, |
||||
'ldquote' => "\u201C", # UTF8 chars |
||||
'rdquote' => "\u201D", |
||||
'lsquote' => "\u2018", |
||||
'rsquote' => "\u2019", |
||||
'em_dash' => "\u2014", |
||||
'en_dash' => "\u2013", |
||||
'ellipsis' => "\u2026", |
||||
'smart_quotes' => true, |
||||
'text_hint' => 'off', |
||||
} |
||||
|
||||
#Translate the hints to the methods. |
||||
ANTIALIAS_OPTS = { |
||||
nil => 'subpixel', |
||||
'best' => 'subpixel', |
||||
'good' => 'gray', |
||||
'fast' => 'gray', |
||||
'gray' => 'gray', |
||||
'subpixel' => 'subpixel' |
||||
} |
||||
|
||||
def initialize(config_hash = DEFAULTS) |
||||
@config_hash = config_hash |
||||
@typographer = Args::Typographer.new(config_hash) |
||||
normalize_antialias |
||||
end |
||||
|
||||
# FIXME REMOVE THIS as part of refactoring |
||||
# Delegate [] to our hash |
||||
# @api private |
||||
# def [](key) |
||||
# @config_hash[key] |
||||
# end |
||||
|
||||
# Load the configuration file, if exists, overriding hardcoded defaults |
||||
# @api private |
||||
def self.load(file) |
||||
yaml = {} |
||||
if File.exists? file |
||||
Squib::logger.info { " using config: #{file}" } |
||||
yaml = YAML.load_file(file) || {} |
||||
end |
||||
Conf.new(DEFAULTS.merge(yaml)) |
||||
end |
||||
|
||||
def to_s |
||||
"Conf: #{@config_hash.to_s}" |
||||
end |
||||
|
||||
def img_dir |
||||
@config_hash['img_dir'] |
||||
end |
||||
|
||||
def text_hint |
||||
@config_hash['text_hint'] |
||||
end |
||||
|
||||
def progress_bars |
||||
@config_hash['progress_bars'] |
||||
end |
||||
|
||||
def typographer |
||||
@typographer |
||||
end |
||||
|
||||
def dir |
||||
@config_hash['dir'] |
||||
end |
||||
|
||||
def prefix |
||||
@config_hash['prefix'] |
||||
end |
||||
|
||||
def count_format |
||||
@config_hash['count_format'] |
||||
end |
||||
|
||||
def antialias |
||||
@config_hash['antialias'] |
||||
end |
||||
|
||||
def backend |
||||
@config_hash['backend'] |
||||
end |
||||
|
||||
def custom_colors |
||||
@config_hash['custom_colors'] |
||||
end |
||||
|
||||
private |
||||
|
||||
def normalize_antialias |
||||
@config_hash['antialias'] = ANTIALIAS_OPTS[@config_hash['antialias'].downcase.strip] |
||||
end |
||||
|
||||
end |
||||
end |
||||
@ -0,0 +1,29 @@
|
||||
require 'squib' |
||||
require 'spec_helper' |
||||
|
||||
describe Squib::Conf do |
||||
|
||||
it 'parses the project template file just fine' do |
||||
conf = Squib::Conf.load(project_template('config.yml')) |
||||
expect(conf.backend).to eq(Squib::Conf::DEFAULTS['backend']) |
||||
end |
||||
|
||||
it 'parses an empty file' do |
||||
conf = Squib::Conf.load(conf('empty.yml')) |
||||
expect(conf.backend).to eq(Squib::Conf::DEFAULTS['backend']) |
||||
end |
||||
|
||||
it 'parses the sample custom config' do |
||||
conf = Squib::Conf.load(sample_file('custom-config.yml')) |
||||
expect(conf.progress_bars).to be true |
||||
expect(conf.text_hint).to eq '#FF0000' |
||||
expect(conf.custom_colors).to eq({ 'foo' => '#ccc' }) |
||||
expect(conf.img_dir).to eq 'customconfig-imgdir' |
||||
end |
||||
|
||||
it 'normalizes antialias automatically' do |
||||
expect(Squib::Conf::DEFAULTS['antialias']).to eq 'best' |
||||
expect(Squib::Conf.new.antialias).to eq 'subpixel' |
||||
end |
||||
|
||||
end |
||||
Loading…
Reference in new issue