Skip to content

Commit

Permalink
extracted system config
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Mandrup committed Nov 28, 2011
1 parent c23fac9 commit d6d2eb8
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 117 deletions.
6 changes: 1 addition & 5 deletions Gemfile
Expand Up @@ -6,7 +6,7 @@ group :default do
gem 'sweetloader', '~> 0.1.0'
gem 'hashie'

gem 'cantango-core', :git => 'git://github.com/kristianmandrup/cantango-core.git'
gem 'cantango-config', :git => 'git://github.com/kristianmandrup/cantango-config.git'
end

group :development do
Expand All @@ -29,10 +29,6 @@ group :test do

# Debug and performance tests
gem 'cutter'

# Adapters
gem 'sourcify'
gem 'dkastner-moneta', '>= 1.0'
end

group :test, :development do
Expand Down
2 changes: 1 addition & 1 deletion lib/cantango/ability/helper/role.rb
Expand Up @@ -2,7 +2,7 @@ module CanTango
class Ability
module Helper
module Role
include CanTango::Helpers::RoleMethods
include CanTango::Helpers::Role

# return list roles the user has
def roles
Expand Down
2 changes: 1 addition & 1 deletion lib/cantango/ability/helper/role_group.rb
Expand Up @@ -2,7 +2,7 @@ module CanTango
class Ability
module Helper
module RoleGroup
include CanTango::Helpers::RoleMethods
include CanTango::Helpers::RoleGroup

# return list of symbols for role groups the user belongs to
def role_groups
Expand Down
39 changes: 5 additions & 34 deletions lib/cantango/configuration/role_groups.rb
@@ -1,41 +1,12 @@
module CanTango
class Configuration
class RoleGroups < RoleRegistry
include Singleton

def role_group_system= name
raise ArgumentError, "Must be a label" if !name.kind_of_label?
@role_group_system = name.to_sym
end

def role_groups_list_map= role_systems_hash
raise ArgumentError, "Must be a hash fx :troles => :role_list, was: #{role_systems_hash}" if !role_systems_hash.kind_of?(Hash)
@role_groups_list_map = role_systems_hash
end

def role_group_system
@role_group_system ||= :troles
end

def add_role_group_system role_system_hash
raise ArgumentError, "Must be a hash fx :troles => :role_list, was: #{role_system_hash}" if !role_system_hash.kind_of?(Hash)
role_groups_list_map.merge! role_system
end

def default_has_method
role_group_methods[:has] || :in_role_group?
end

def default_list_method
role_group_methods[:list] || :role_groups_list
end

def role_group_methods
role_groups_list_map[role_group_system] || {}
class RoleGroups < System
def system
@system ||= :troles
end

def role_groups_list_map
@role_groups_list_map ||= {
def default_system_apis
{
:troles => {:list => :role_group_list}
}
end
Expand Down
15 changes: 0 additions & 15 deletions lib/cantango/configuration/role_registry.rb
@@ -1,21 +1,6 @@
module CanTango
class Configuration
class RoleRegistry < Registry
attr_reader :has_method, :list_method

[:has_method, :list_method].each do |meth|
class_eval %{
def #{meth}
@#{meth} ||= default_#{meth}
end
def #{meth}= name
raise "Must be a label" if !name.kind_of_label?
@#{meth} = name
end
}
end

def only *names
@onlies = names.select_labels
end
Expand Down
45 changes: 8 additions & 37 deletions lib/cantango/configuration/roles.rb
@@ -1,42 +1,13 @@
module CanTango
class Configuration
class Roles < RoleRegistry
include Singleton

def role_system= name
raise ArgumentError, "Must be a label" if !name.kind_of_label?
@role_system = name.to_sym
end

def roles_list_map= role_systems_hash
raise ArgumentError, "Must be a hash fx :troles => :role_list, was: #{role_systems_hash}" if !role_systems_hash.kind_of?(Hash)
@roles_list_map = role_systems_hash
end

def role_system
@role_system ||= :simple_roles
end

def add_role_system role_system_hash
raise ArgumentError, "Must be a hash fx :troles => :role_list, was: #{role_system_hash}" if !role_system_hash.kind_of?(Hash)
roles_list_map.merge! role_system
end

def default_has_method
role_system_methods[:list] || :has_role?
end

def default_list_method
role_system_methods[:list] || :roles_list
end

def role_system_methods
roles_list_map[role_system] || {}
end

def roles_list_map
@roles_list_map ||= {
:troles => {:list => :role_list},
class Roles < System
def system
@system ||= :simple_roles
end

def default_system_apis
{
:troles => {:list => :role_list, :has => :has_role?},
:simple_roles => {:list => :roles_list}
}
end
Expand Down
43 changes: 43 additions & 0 deletions lib/cantango/configuration/system.rb
@@ -0,0 +1,43 @@
module CanTango
class Configuration
class System < RoleRegistry
include Singleton

def system= name
raise ArgumentError, "Must be a label" if !name.kind_of_label?
@system = name.to_sym
end

def system_apis= system_apis
raise ArgumentError, "Must be a hash fx :troles => :role_list, was: #{system_apis}" if !system_apis.kind_of?(Hash)
@system_apis = system_apis
end

def system
@system ||= default_system
end

def default_system
nil
end

def add_systems system_apis
raise ArgumentError, "Must be a hash fx :troles => :role_list, was: #{system_apis}" if !system_apis.kind_of?(Hash)
self.system_apis.merge! system_apis
end
alias_method :add_system, :add_systems

def system_api
system_apis[system] || {}
end

def system_apis
@system_apis ||= default_system_apis
end

def default_system_apis
{}
end
end
end
end
10 changes: 3 additions & 7 deletions lib/cantango/helpers/role.rb
@@ -1,14 +1,10 @@
module CanTango
module Helpers
module Role
def has_role_meth
config.roles.has_method
def role_method name
config.roles.method_names[name]
end

def roles_list_meth
config.roles.list_method
end


def config
CanTango.config
end
Expand Down
8 changes: 2 additions & 6 deletions lib/cantango/helpers/role_group.rb
@@ -1,12 +1,8 @@
module CanTango
module Helpers
module RoleGroup
def has_role_group_meth
config.role_groups.has_method
end

def role_groups_list_meth
config.role_groups.list_method
def role_group_method name
config.roles_groups.method_names[name]
end

def config
Expand Down
8 changes: 3 additions & 5 deletions spec/cantango/configuration/role_groups_spec.rb
@@ -1,14 +1,12 @@
require 'rspec'
require 'cantango'

require 'spec_helper'
require 'cantango/configuration/role_registry_ex'

describe CanTango::Configuration::RoleGroups do
subject { CanTango.config.role_groups }

it_should_behave_like "Role Registry" do
let (:has) { :in_role_group? }
let (:list) { :role_groups_list }
specify { subject.system_api(:has).should == :in_role_group? }
specify { subject.system_api(:list).should == :role_groups_list }
end
end

Expand Down
8 changes: 2 additions & 6 deletions spec/cantango/configuration/roles_spec.rb
@@ -1,5 +1,4 @@
require 'rspec'
require 'cantango'
require 'spec_helper'
require 'cantango/configuration/role_registry_ex'

describe CanTango::Configuration::Roles do
Expand All @@ -9,7 +8,4 @@
let (:has) { :has_role? }
let (:list) { :roles_list }
end
end



end
16 changes: 16 additions & 0 deletions spec/cantango/configuration/system_ex.rb
@@ -0,0 +1,16 @@
require 'cantango/configuration/shared/role_registry_ex'

shared_examples_for 'System' do
it_should_behave_like "Role Registry"

describe 'system' do
its(:system) { should == nil }

decribe 'system=' do
before do
system = :my_sys
end
its(:system) { should == :my_sys }
end
end
end
Empty file.
26 changes: 26 additions & 0 deletions spec/cantango/helpers/role.rb
@@ -0,0 +1,26 @@
require 'spec_helper'
require 'cantango/configuration/role_registry_ex'

class Subject
end

describe CanTango::Helpers::Role do
before do
CanTango.config.roles.system = :troles
end

subject do
Subject.new
end

specify do
subject.role_method(:has).should == :has_role?
end

specify do
subject.role_method(:list).should == :role_list?
end
end



0 comments on commit d6d2eb8

Please sign in to comment.