Skip to content

Commit

Permalink
Add groups to bukfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Nullreff committed Jan 23, 2014
1 parent 66da8a3 commit a80452d
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/bukin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

module Bukin
class BukinError < StandardError; end
class BukfileError < BukinError; end
class BukfileError < BukinError
def self.not_symbol(group)
BukfileError.new("The group name '#{group}' must be a Symbol")
end

def self.nested_groups
BukfileError.new("Nesing 'group' statements is not supported")
end
end
class InstallError < BukinError; end

class VersionError < BukinError
Expand Down
28 changes: 28 additions & 0 deletions lib/bukin/bukfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Bukfile
def initialize(path = nil, &block)
@resources = []
@path = path || File.join(Dir.pwd, FILE_NAME)
@groups = []

if block
instance_eval(&block)
else
Expand All @@ -21,6 +23,17 @@ def plugin(name, *args)
add_resource name, :plugin, args
end

def group(*groups)
raise BukfileError.nested_groups unless @groups.empty?
groups.each do |group|
raise BukfileError.not_symbol(group) unless group.is_a?(Symbol)
end

@groups = groups
yield
@groups = []
end

def to_s
@reources.map do |resource|
result = "#{resource[:name]}"
Expand All @@ -46,6 +59,8 @@ def add_resource(name, type, args)
:version => version
}.merge(options)

resource[:group] = build_groups(resource[:group])

@resources << resource
end

Expand All @@ -54,5 +69,18 @@ def resource_exists?(name, type)
resource[:name] == name && resource[:type] == type
end
end

def build_groups(group)
case group
when nil
@groups.uniq
when Symbol
[group] | @groups
when Array
group | @groups
else
raise BukfileError.not_symbol(group)
end
end
end
end
125 changes: 124 additions & 1 deletion spec/bukfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

describe Bukin::Bukfile do
[:server, :plugin].each do |type|

it "adds a #{type} by method" do
bukfile = Bukin::Bukfile.new{}
bukfile.resources.size.should == 0
Expand Down Expand Up @@ -88,4 +87,128 @@
bukfile.resources.size.should == 0
bukfile.resources.find{|resource| resource[:name] == 'missing_plugin'}.should be_nil
end

it 'attaches a single group to a resource' do
bukfile = Bukin::Bukfile.new do
plugin 'test', group: :test
end

bukfile.resources.first[:group].should == [:test]
end

it 'attaches multiple groups to a resource' do
bukfile = Bukin::Bukfile.new do
plugin 'test', group: [:test, :development]
end

bukfile.resources.first[:group].should == [:test, :development]
end

it 'attaches a single group to multiple resources' do
bukfile = Bukin::Bukfile.new do
group :test do
plugin 'test1'
plugin 'test2'
end
end

bukfile.resources[0][:group].should == [:test]
bukfile.resources[1][:group].should == [:test]
end

it 'attaches multiple groups to multiple resources' do
bukfile = Bukin::Bukfile.new do
group :test, :development do
plugin 'test1'
plugin 'test2'
end
end

bukfile.resources[0][:group].should == [:test, :development]
bukfile.resources[1][:group].should == [:test, :development]
end

it 'attaches groups to multiple resources' do
bukfile = Bukin::Bukfile.new do
group :test do
plugin 'test1', group: :development
plugin 'test2'
end
end

bukfile.resources[0][:group].should =~ [:test, :development]
bukfile.resources[1][:group].should == [:test]
end

it 'combines duplicate groups' do
bukfile = Bukin::Bukfile.new do
group :test do
plugin 'test1', group: [:development, :test]
plugin 'test2', group: :test
end
end

bukfile.resources[0][:group].should =~ [:test, :development]
bukfile.resources[1][:group].should == [:test]
end

it 'combines duplicate method groups' do
bukfile = Bukin::Bukfile.new do
group :test, :test do
plugin 'test'
end
end

bukfile.resources.first[:group].should == [:test]
end

it 'combines duplicate resource groups' do
bukfile = Bukin::Bukfile.new do
plugin 'test', group: [:test, :test]
end

bukfile.resources.first[:group].should == [:test]
end

it 'only adds groups inside a block' do
bukfile = Bukin::Bukfile.new do
group :development do
plugin 'test1'
end
plugin 'test2'
end

bukfile.resources[0][:group].should == [:development]
bukfile.resources[1][:group].should == []
end

it 'throws an error when groups are nested' do
expect do
Bukin::Bukfile.new do
group :test do
group :development do
plugin 'test'
end
end
end
end.to raise_error(Bukin::BukfileError)
end

it 'throws an error when group is not a symbol' do
expect do
Bukin::Bukfile.new do
group 'test' do
plugin 'test'
end
end
end.to raise_error(Bukin::BukfileError)
end

it 'throws an error when resource group is not a symbol' do
expect do
Bukin::Bukfile.new do
plugin 'test', group: 'test'
end
end.to raise_error(Bukin::BukfileError)
end
end

0 comments on commit a80452d

Please sign in to comment.