Permalink
Browse files

first version - extracted from sugar-high

  • Loading branch information...
1 parent 06a2835 commit 137526bf55fe3522b350980a9b314bf6344cec76 @kristianmandrup committed May 9, 2012
View
@@ -28,7 +28,7 @@ pkg
#
# For MacOS:
#
-#.DS_Store
+.DS_Store
# For TextMate
#*.tmproj
View
21 Gemfile
@@ -1,14 +1,13 @@
-source "http://rubygems.org"
-# Add dependencies required to use your gem here.
-# Example:
-# gem "activesupport", ">= 2.3.5"
+source :rubygems
+
+group :test do
+ gem 'rails'
+end
-# Add dependencies to develop your gem here.
-# Include everything needed to run rake, tests, features, etc.
group :development do
- gem "rspec", "~> 2.8.0"
- gem "rdoc", "~> 3.12"
- gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.8.3"
- gem "rcov", ">= 0"
+ gem "rspec", "~> 2.8.0"
+ gem "rdoc", "~> 3.12"
+ gem "bundler", "~> 1.1.0"
+ gem "jeweler", "~> 1.8.3"
+ gem "simplecov", ">= 0.5"
end
View
@@ -0,0 +1,109 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.2.3)
+ actionpack (= 3.2.3)
+ mail (~> 2.4.4)
+ actionpack (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ activerecord (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ activesupport (3.2.3)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.0)
+ diff-lcs (1.1.3)
+ erubis (2.7.0)
+ git (1.2.5)
+ hike (1.2.1)
+ i18n (0.6.0)
+ jeweler (1.8.3)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ rdoc
+ journey (1.0.3)
+ json (1.7.1)
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.18)
+ multi_json (1.3.4)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.3)
+ actionmailer (= 3.2.3)
+ actionpack (= 3.2.3)
+ activerecord (= 3.2.3)
+ activeresource (= 3.2.3)
+ activesupport (= 3.2.3)
+ bundler (~> 1.0)
+ railties (= 3.2.3)
+ railties (3.2.3)
+ actionpack (= 3.2.3)
+ activesupport (= 3.2.3)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rspec (2.8.0)
+ rspec-core (~> 2.8.0)
+ rspec-expectations (~> 2.8.0)
+ rspec-mocks (~> 2.8.0)
+ rspec-core (2.8.0)
+ rspec-expectations (2.8.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.8.0)
+ simplecov (0.6.2)
+ multi_json (~> 1.3)
+ simplecov-html (~> 0.5.3)
+ simplecov-html (0.5.3)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ thor (0.14.6)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.33)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (~> 1.1.0)
+ jeweler (~> 1.8.3)
+ rails
+ rdoc (~> 3.12)
+ rspec (~> 2.8.0)
+ simplecov (>= 0.5)
View
@@ -1 +1 @@
-0.0.0
+0.1.0
View
@@ -0,0 +1,23 @@
+require 'concerned/module_ext'
+
+module Concerned
+ class << self
+ def require_shared concern
+ require_method "shared/#{concern.to_s.underscore}"
+ end
+
+ def shared_const concern_ns
+ concern_ns.constantize
+ rescue NameError
+ shared_ns_const concern_ns
+ end
+
+ def shared_ns_const concern_ns
+ const_name = "Shared::#{concern_ns}"
+ const_name.constantize
+ rescue NameError
+ raise "No module could be found for: #{concern_ns} or #{const_name}"
+ end
+
+ end
+end
@@ -0,0 +1,61 @@
+class Module
+ def concerned_with(*concerns)
+ options = concerns.extract_options!
+ concerns.flatten.each do |concern|
+ next if concern.blank?
+ require_concern name, concern
+ end
+ shared_concerns([options[:shared]].flatten.compact)
+ end
+
+ def include_concerns(*concerns)
+ options = concerns.extract_options!
+ concerns.flatten.each do |concern|
+ next if concern.blank?
+ require_concern name, concern
+ concern_ns = [name, concern.to_s.camelize].join('::')
+ self.send :include, concern_ns.constantize
+ begin
+ self.extend [concern_ns, 'ClassMethods'].join('::').constantize
+ rescue
+ end
+ end
+ include_shared_concerns([options[:shared]].flatten.compact)
+ end
+
+ def shared_concerns(*concerns)
+ concerns.flatten.each do |concern|
+ next if concern.blank?
+ Concerned.require_shared concern
+ end
+ end
+
+ def include_shared_concerns(*concerns)
+ concerns.flatten.each do |concern|
+ next if concern.blank?
+ Concerned.require_shared concern
+ concern_ns = concern.to_s.camelize
+
+ self.send :include, Concerned.shared_const(concern_ns)
+
+ begin
+ self.extend [concern_ns, 'ClassMethods'].join('::').constantize
+ rescue
+ end
+ end
+ end
+
+ alias_method :shared_concern, :shared_concerns
+ alias_method :include_concern, :include_concerns
+ alias_method :include_shared_concern, :include_shared_concerns
+
+ protected
+
+ def require_concern name, concern
+ require_method "#{name.underscore}/#{concern.to_s.underscore}"
+ end
+
+ def require_method path
+ defined?(require_dependency) ? require_dependency(path) : require(path)
+ end
+end
View
@@ -1,7 +1,29 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'spec_helper'
+require 'rails'
+require 'concerned'
+require 'active_support/dependencies'
+
+# The same procedure as one that needed for common #require - It should know path.
+$:.unshift File.dirname __FILE__
+
+class FixtureUser
+ concerned_with :scopes, :validations
+ shared_concerns :associations
+ include_shared_concerns :caching
+end
describe "Concerned" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+ describe "#concerned_with" do
+ it 'should require files from #{name.underscore}/#{concern}' do
+ [:scopes, :validations].each do |concern|
+ FixtureUser.new.should respond_to("method_from_#{concern}_concern")
+ end
+ end
+ end
+
+ describe "#include_shared_concerns" do
+ it 'should require and include from shared/#{concern}' do
+ FixtureUser.new.should respond_to("method_from_shared_concern")
+ end
end
end
@@ -0,0 +1,4 @@
+class FixtureUser
+ def method_from_scopes_concern
+ end
+end
@@ -0,0 +1,4 @@
+class FixtureUser
+ def method_from_validations_concern
+ end
+end
@@ -0,0 +1 @@
+puts "+++++++++"
View
@@ -0,0 +1,4 @@
+module Caching
+ def method_from_shared_concern
+ end
+end

0 comments on commit 137526b

Please sign in to comment.