Permalink
Browse files

make configuration persistence injectable

  • Loading branch information...
1 parent 2c3c170 commit be919cc452d3cf499cca80b6a4f7d677cb75d2fa Matthew Kane Parker committed Sep 25, 2012
@@ -1,34 +1,4 @@
module LicenseFinder
- class Configuration
- attr_reader :whitelist, :ignore_groups, :dependencies_dir
-
- def initialize
- config = {}
-
- if File.exists?(config_file_path)
- yaml = File.read(config_file_path)
- config = YAML.load(yaml)
- end
-
- @whitelist = config['whitelist'] || []
- @ignore_groups = (config["ignore_groups"] || []).map(&:to_sym)
- @dependencies_dir = config['dependencies_file_dir'] || '.'
- end
-
- def config_file_path
- File.join('.', 'config', 'license_finder.yml')
- end
-
- def dependencies_yaml
- File.join(dependencies_dir, "dependencies.yml")
- end
-
- def dependencies_text
- File.join(dependencies_dir, "dependencies.txt")
- end
-
- def dependencies_html
- File.join(dependencies_dir, "dependencies.html")
- end
+ class Configuration < LicenseFinder::Persistence::Configuration
end
end
@@ -2,7 +2,7 @@ module LicenseFinder
class Dependency < LicenseFinder::Persistence::Dependency
def approved
return super if super
- self.approved = LicenseFinder.config.whitelist.include?(license)
+ self.approved = config.whitelist.include?(license)
end
def license_files
@@ -1,6 +1,7 @@
module LicenseFinder
module Persistence
autoload :Dependency, 'license_finder/persistence/yaml/dependency'
+ autoload :Configuration, 'license_finder/persistence/yaml/configuration'
end
end
@@ -0,0 +1,34 @@
+module LicenseFinder
+ module Persistence
+ class Configuration
+ attr_reader :whitelist, :ignore_groups, :dependencies_dir
+
+ def initialize(config={})
+ if File.exists?(config_file_path)
+ yaml = File.read(config_file_path)
+ config = YAML.load(yaml).merge config
+ end
+
+ @whitelist = config['whitelist'] || []
+ @ignore_groups = (config["ignore_groups"] || []).map(&:to_sym)
+ @dependencies_dir = config['dependencies_file_dir'] || '.'
+ end
+
+ def config_file_path
+ File.join('.', 'config', 'license_finder.yml')
+ end
+
+ def dependencies_yaml
+ File.join(dependencies_dir, "dependencies.yml")
+ end
+
+ def dependencies_text
+ File.join(dependencies_dir, "dependencies.txt")
+ end
+
+ def dependencies_html
+ File.join(dependencies_dir, "dependencies.html")
+ end
+ end
+ end
+end
@@ -84,6 +84,10 @@ def initialize(attributes = {})
update_attributes_without_saving attributes
end
+ def config
+ LicenseFinder.config
+ end
+
def update_attributes new_values
update_attributes_without_saving(new_values)
save
@@ -0,0 +1,5 @@
+require "spec_helper"
+
+describe LicenseFinder::Persistence::Configuration do
+ it_behaves_like "a persistable configuration"
+end
@@ -0,0 +1,22 @@
+shared_examples_for "a persistable configuration" do
+ let(:klass) { described_class }
+
+ let(:attributes) do
+ {
+ "whitelist" => ["FooLicense", "BarLicense"],
+ "ignore_groups" => [:test, :development]
+ }
+ end
+
+ describe '.new' do
+ subject { klass.new(attributes) }
+
+ context "with known attributes" do
+ it "should set the all of the attributes on the instance" do
+ attributes.each do |key, value|
+ subject.send("#{key}").should == value
+ end
+ end
+ end
+ end
+end
@@ -78,6 +78,12 @@
end
end
+ describe "#config" do
+ it 'should respond to it' do
+ klass.new.should respond_to(:config)
+ end
+ end
+
describe '#attributes' do
it "should return a hash containing the values of all the accessible properties" do
dep = klass.new(attributes)

0 comments on commit be919cc

Please sign in to comment.