Skip to content
Browse files

persistence is now an injectable dependency

  • Loading branch information...
1 parent d52d6d7 commit 381fb7153693635355fd58dbfc2331df9afe47b4 @moonmaster9000 moonmaster9000 committed Sep 20, 2012
View
9 lib/license_finder.rb
@@ -1,9 +1,16 @@
require 'pathname'
require 'yaml'
+require 'erb'
module LicenseFinder
ROOT_PATH = Pathname.new(__FILE__).dirname
+ DEPENDENCY_ATTRIBUTES = [
+ "name", "source", "version", "license", "license_url", "approved", "notes",
+ "license_files", "readme_files", "bundler_groups", "summary",
+ "description", "homepage", "children", "parents"
+ ]
+
autoload :Bundle, 'license_finder/bundle'
autoload :BundledGem, 'license_finder/bundled_gem'
autoload :CLI, 'license_finder/cli'
@@ -18,6 +25,7 @@ module LicenseFinder
autoload :TextReport, 'license_finder/text_report'
autoload :DependencyReport, 'license_finder/dependency_report'
autoload :BundleSyncer, 'license_finder/bundle_syncer'
+ autoload :Persistence, 'license_finder/persistence/yaml'
def self.config
@config ||= Configuration.new
@@ -28,5 +36,4 @@ def self.load_rake_tasks
end
end
-require 'erb'
require 'license_finder/railtie' if defined?(Rails)
View
118 lib/license_finder/dependency.rb
@@ -1,131 +1,36 @@
module LicenseFinder
- class Dependency
- class Database
- def initialize
- @dependency_attributes = YAML.load File.read(LicenseFinder.config.dependencies_yaml) if File.exists?(LicenseFinder.config.dependencies_yaml)
- end
-
- def find(&block)
- dependency_attributes.detect &block
- end
-
- def update(dependency_hash)
- dependency_attributes.reject! { |a| a['name'] == dependency_hash['name'] }
- dependency_attributes << dependency_hash
- persist!
- end
-
- def delete_all
- File.delete(LicenseFinder.config.dependencies_yaml) if File.exists?(LicenseFinder.config.dependencies_yaml)
- @dependency_attributes = nil
- end
-
- def persist!
- File.open(LicenseFinder.config.dependencies_yaml, 'w+') do |f|
- f.write dependency_attributes.to_yaml
- end
- end
-
- def all
- dependency_attributes
- end
-
- private
- def dependency_attributes
- @dependency_attributes ||= []
- end
- end
-
- ATTRIBUTE_NAMES = [
- "name", "source", "version", "license", "license_url", "approved", "notes",
- "license_files", "readme_files", "bundler_groups", "summary",
- "description", "homepage", "children", "parents"
- ]
-
-
- attr_accessor *ATTRIBUTE_NAMES
-
- attr_reader :summary, :description
-
- def self.from_hash(attrs)
- new(attrs)
- end
-
- def self.find_by_name(name)
- attributes = database.find { |a| a['name'] == name }
- new(attributes) if attributes
- end
-
- def self.database
- @database ||= Database.new
- end
-
- def self.delete_all
- database.delete_all
- end
-
- def self.all
- database.all.map { |attributes| new(attributes) }
- end
-
- def self.unapproved
- all.select {|d| d.approved == false }
- end
-
- def initialize(attributes = {})
- update_attributes_without_saving attributes
- end
-
- def update_attributes new_values
- update_attributes_without_saving(new_values)
- save
- end
-
+ class Dependency < LicenseFinder::Persistence::Dependency
def approved
- return @approved if defined?(@approved)
+ return super unless super.nil?
- @approved = LicenseFinder.config.whitelist.include?(license)
+ self.approved = LicenseFinder.config.whitelist.include?(license)
end
def license_files
- @license_files ||= []
+ super || (self.license_files = [])
end
def readme_files
- @readme_files ||= []
+ super || (self.readme_files = [])
end
def bundler_groups
- @bundler_groups ||= []
+ super || (self.bundler_groups = [])
end
def children
- @children ||= []
+ super || (self.children = [])
end
def parents
- @parents ||= []
+ super || (self.parents = [])
end
def approve!
self.approved = true
save
end
- def save
- self.class.database.update(attributes)
- end
-
- def attributes
- attributes = {}
-
- ATTRIBUTE_NAMES.each do |attrib|
- attributes[attrib] = send attrib
- end
-
- attributes
- end
-
def license_url
LicenseFinder::LicenseUrl.find_by_name license
end
@@ -146,13 +51,6 @@ def merge(other)
merged
end
-
- private
- def update_attributes_without_saving(new_values)
- new_values.each do |key, value|
- send("#{key}=", value)
- end
- end
end
end
View
1 lib/license_finder/persistence.rb
@@ -0,0 +1 @@
+
View
6 lib/license_finder/persistence/yaml.rb
@@ -0,0 +1,6 @@
+module LicenseFinder
+ module Persistence
+ autoload :Dependency, 'license_finder/persistence/yaml/dependency'
+ end
+end
+
View
102 lib/license_finder/persistence/yaml/dependency.rb
@@ -0,0 +1,102 @@
+module LicenseFinder
+ module Persistence
+ class Dependency
+ class Database
+ def initialize
+ @dependency_attributes = YAML.load File.read(LicenseFinder.config.dependencies_yaml) if File.exists?(LicenseFinder.config.dependencies_yaml)
+ end
+
+ def find(&block)
+ dependency_attributes.detect &block
+ end
+
+ def update(dependency_hash)
+ dependency_attributes.reject! { |a| a['name'] == dependency_hash['name'] }
+ dependency_attributes << dependency_hash
+ persist!
+ end
+
+ def delete_all
+ File.delete(LicenseFinder.config.dependencies_yaml) if File.exists?(LicenseFinder.config.dependencies_yaml)
+ @dependency_attributes = nil
+ end
+
+ def persist!
+ File.open(LicenseFinder.config.dependencies_yaml, 'w+') do |f|
+ f.write dependency_attributes.to_yaml
+ end
+ end
+
+ def all
+ dependency_attributes
+ end
+
+ private
+ def dependency_attributes
+ @dependency_attributes ||= []
+ end
+ end
+
+ attr_accessor *LicenseFinder::DEPENDENCY_ATTRIBUTES
+
+ class << self
+ def find_by_name(name)
+ attributes = database.find { |a| a['name'] == name }
+ new(attributes) if attributes
+ end
+
+ def delete_all
+ database.delete_all
+ end
+
+ def all
+ database.all.map { |attributes| new(attributes) }
+ end
+
+ def unapproved
+ all.select {|d| d.approved == false }
+ end
+
+ def update(attributes)
+ database.update attributes
+ end
+
+ private
+ def database
+ @database ||= Database.new
+ end
+ end
+
+ def initialize(attributes = {})
+ update_attributes_without_saving attributes
+ end
+
+ def update_attributes new_values
+ update_attributes_without_saving(new_values)
+ save
+ end
+
+ def save
+ self.class.update(attributes)
+ end
+
+ def attributes
+ attributes = {}
+
+ LicenseFinder::DEPENDENCY_ATTRIBUTES.each do |attrib|
+ attributes[attrib] = send attrib
+ end
+
+ attributes
+ end
+
+ private
+ def update_attributes_without_saving(new_values)
+ new_values.each do |key, value|
+ send("#{key}=", value)
+ end
+ end
+ end
+ end
+end
+
View
105 spec/lib/license_finder/dependency_spec.rb
@@ -25,17 +25,17 @@ module LicenseFinder
end
describe "#approved" do
- it "should mark it as approved when the license is whitelisted" do
+ it "should return true when the license is whitelisted" do
dependency = Dependency.new('license' => 'MIT')
dependency.approved.should == true
end
- it "should not mark it as approved when the license is not whitelisted" do
+ it "should return false when the license is not whitelisted" do
dependency = Dependency.new('license' => 'GPL')
dependency.approved.should == false
end
- it "should be settable" do
+ it "should be overridable" do
dependency = Dependency.new
dependency.approved = true
dependency.approved.should == true
@@ -137,7 +137,7 @@ module LicenseFinder
describe '#approve!' do
it "should update the yaml file to show the gem is approved" do
- gem = Dependency.new({name: "foo"})
+ gem = Dependency.new(name: "foo")
gem.approve!
reloaded_gem = Dependency.find_by_name(gem.name)
reloaded_gem.approved.should be_true
@@ -153,103 +153,6 @@ module LicenseFinder
end
end
end
-
- describe "persistence" do
- before do
- Dependency.delete_all
- end
-
- describe '.new' do
- subject { Dependency.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 equal(value), "expected #{value.inspect} for #{key}, got #{subject.send("#{key}").inspect}"
- end
- end
- end
-
- context "with unknown attributes" do
- before do
- attributes['foo'] = 'bar'
- end
- it "should raise an exception" do
- expect { subject }.to raise_exception(NoMethodError)
- end
- end
- end
-
- describe '.unapproved' do
- it "should return all unapproved dependencies" do
- Dependency.new(name: "unapproved dependency", approved: false).save
- Dependency.new(name: "approved dependency", approved: true).save
-
- unapproved = Dependency.unapproved
- unapproved.count.should == 1
- unapproved.collect(&:approved).any?.should be_false
- end
- end
-
- describe '.find_by_name' do
- subject { Dependency.find_by_name gem_name }
- let(:gem_name) { "foo" }
-
- context "when a gem with the provided name exists" do
- before do
- Dependency.new(
- 'name' => gem_name,
- 'version' => '0.0.1'
- ).save
- end
-
- its(:name) { should == gem_name }
- its(:version) { should == '0.0.1' }
- end
-
- context "when no gem with the provided name exists" do
- it { should == nil }
- end
- end
-
- describe '#attributes' do
- it "should return a hash containing the values of all the accessible properties" do
- dep = Dependency.new(attributes)
- attributes = dep.attributes
- Dependency::ATTRIBUTE_NAMES.each do |name|
- attributes[name].should == dep.send(name)
- end
- end
- end
-
- describe '#save' do
- it "should persist all of the dependency's attributes" do
- dep = Dependency.new(attributes)
- dep.save
-
- saved_dep = Dependency.find_by_name(dep.name)
-
- saved_dep.attributes.should == dep.attributes
-
- dep.version = "new version"
- dep.save
-
- saved_dep = Dependency.find_by_name(dep.name)
- saved_dep.version.should == "new version"
- end
- end
-
- describe "#update_attributes" do
- it "should update the provided attributes with the provided values" do
- gem = Dependency.new(attributes)
- updated_attributes = {"version" => "new_version", "license" => "updated_license"}
- gem.update_attributes(updated_attributes)
-
- saved_gem = Dependency.find_by_name(gem.name)
- saved_gem.attributes.should == gem.attributes.merge(updated_attributes)
- end
- end
- end
end
end
View
5 spec/lib/license_finder/persistence/yaml/dependency_spec.rb
@@ -0,0 +1,5 @@
+require "spec_helper"
+
+describe LicenseFinder::Persistence::Dependency do
+ it_behaves_like "a persistable dependency"
+end
View
114 spec/support/shared_examples/persistence/dependency.rb
@@ -0,0 +1,114 @@
+shared_examples_for "a persistable dependency" do
+ let(:klass) { described_class }
+
+ let(:attributes) do
+ {
+ 'name' => "spec_name",
+ 'version' => "2.1.3",
+ 'license' => "GPLv2",
+ 'approved' => false,
+ 'notes' => 'some notes',
+ 'homepage' => 'homepage',
+ 'license_files' => ['/Users/pivotal/foo/lic1', '/Users/pivotal/bar/lic2'],
+ 'readme_files' => ['/Users/pivotal/foo/Readme1', '/Users/pivotal/bar/Readme2'],
+ 'source' => "bundle",
+ 'bundler_groups' => ["test"]
+ }
+ end
+
+ before do
+ klass.delete_all
+ 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 equal(value), "expected #{value.inspect} for #{key}, got #{subject.send("#{key}").inspect}"
+ end
+ end
+ end
+
+ context "with unknown attributes" do
+ before do
+ attributes['foo'] = 'bar'
+ end
+
+ it "should raise an exception" do
+ expect { subject }.to raise_exception(NoMethodError)
+ end
+ end
+ end
+
+ describe '.unapproved' do
+ it "should return all unapproved dependencies" do
+ klass.new(name: "unapproved dependency", approved: false).save
+ klass.new(name: "approved dependency", approved: true).save
+
+ unapproved = klass.unapproved
+ unapproved.count.should == 1
+ unapproved.collect(&:approved).any?.should be_false
+ end
+ end
+
+ describe '.find_by_name' do
+ subject { klass.find_by_name gem_name }
+ let(:gem_name) { "foo" }
+
+ context "when a gem with the provided name exists" do
+ before do
+ klass.new(
+ 'name' => gem_name,
+ 'version' => '0.0.1'
+ ).save
+ end
+
+ its(:name) { should == gem_name }
+ its(:version) { should == '0.0.1' }
+ end
+
+ context "when no gem with the provided name exists" do
+ it { should == nil }
+ end
+ end
+
+ describe '#attributes' do
+ it "should return a hash containing the values of all the accessible properties" do
+ dep = klass.new(attributes)
+ attributes = dep.attributes
+ LicenseFinder::DEPENDENCY_ATTRIBUTES.each do |name|
+ attributes[name].should == dep.send(name)
+ end
+ end
+ end
+
+ describe '#save' do
+ it "should persist all of the dependency's attributes" do
+ dep = klass.new(attributes)
+ dep.save
+
+ saved_dep = klass.find_by_name(dep.name)
+
+ saved_dep.attributes.should == dep.attributes
+
+ dep.version = "new version"
+ dep.save
+
+ saved_dep = klass.find_by_name(dep.name)
+ saved_dep.version.should == "new version"
+ end
+ end
+
+ describe "#update_attributes" do
+ it "should update the provided attributes with the provided values" do
+ gem = klass.new(attributes)
+ updated_attributes = {"version" => "new_version", "license" => "updated_license"}
+ gem.update_attributes(updated_attributes)
+
+ saved_gem = klass.find_by_name(gem.name)
+ saved_gem.attributes.should == gem.attributes.merge(updated_attributes)
+ end
+ end
+end

0 comments on commit 381fb71

Please sign in to comment.
Something went wrong with that request. Please try again.