Skip to content

Commit

Permalink
make configuration persistence injectable
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Kane Parker committed Sep 25, 2012
1 parent 2c3c170 commit be919cc
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 32 deletions.
32 changes: 1 addition & 31 deletions lib/license_finder/configuration.rb
@@ -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 changes: 1 addition & 1 deletion lib/license_finder/dependency.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/license_finder/persistence/yaml.rb
@@ -1,6 +1,7 @@
module LicenseFinder
module Persistence
autoload :Dependency, 'license_finder/persistence/yaml/dependency'
autoload :Configuration, 'license_finder/persistence/yaml/configuration'
end
end

34 changes: 34 additions & 0 deletions lib/license_finder/persistence/yaml/configuration.rb
@@ -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
4 changes: 4 additions & 0 deletions lib/license_finder/persistence/yaml/dependency.rb
Expand Up @@ -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
Expand Down
@@ -0,0 +1,5 @@
require "spec_helper"

describe LicenseFinder::Persistence::Configuration do
it_behaves_like "a persistable configuration"
end
22 changes: 22 additions & 0 deletions spec/support/shared_examples/persistence/configuration.rb
@@ -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
6 changes: 6 additions & 0 deletions spec/support/shared_examples/persistence/dependency.rb
Expand Up @@ -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)
Expand Down

0 comments on commit be919cc

Please sign in to comment.