Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first version - extracted from sugar-high

  • Loading branch information...
commit 137526bf55fe3522b350980a9b314bf6344cec76 1 parent 06a2835
Kristian Mandrup authored May 09, 2012
2  .gitignore
@@ -28,7 +28,7 @@ pkg
28 28
 #
29 29
 # For MacOS:
30 30
 #
31  
-#.DS_Store
  31
+.DS_Store
32 32
 
33 33
 # For TextMate
34 34
 #*.tmproj
21  Gemfile
... ...
@@ -1,14 +1,13 @@
1  
-source "http://rubygems.org"
2  
-# Add dependencies required to use your gem here.
3  
-# Example:
4  
-#   gem "activesupport", ">= 2.3.5"
  1
+source :rubygems
  2
+
  3
+group :test do
  4
+  gem 'rails'
  5
+end
5 6
 
6  
-# Add dependencies to develop your gem here.
7  
-# Include everything needed to run rake, tests, features, etc.
8 7
 group :development do
9  
-  gem "rspec", "~> 2.8.0"
10  
-  gem "rdoc", "~> 3.12"
11  
-  gem "bundler", "~> 1.0.0"
12  
-  gem "jeweler", "~> 1.8.3"
13  
-  gem "rcov", ">= 0"
  8
+  gem "rspec",    "~> 2.8.0"
  9
+  gem "rdoc",     "~> 3.12"
  10
+  gem "bundler",  "~> 1.1.0"
  11
+  gem "jeweler",  "~> 1.8.3"
  12
+  gem "simplecov", ">= 0.5"
14 13
 end
109  Gemfile.lock
... ...
@@ -0,0 +1,109 @@
  1
+GEM
  2
+  remote: http://rubygems.org/
  3
+  specs:
  4
+    actionmailer (3.2.3)
  5
+      actionpack (= 3.2.3)
  6
+      mail (~> 2.4.4)
  7
+    actionpack (3.2.3)
  8
+      activemodel (= 3.2.3)
  9
+      activesupport (= 3.2.3)
  10
+      builder (~> 3.0.0)
  11
+      erubis (~> 2.7.0)
  12
+      journey (~> 1.0.1)
  13
+      rack (~> 1.4.0)
  14
+      rack-cache (~> 1.2)
  15
+      rack-test (~> 0.6.1)
  16
+      sprockets (~> 2.1.2)
  17
+    activemodel (3.2.3)
  18
+      activesupport (= 3.2.3)
  19
+      builder (~> 3.0.0)
  20
+    activerecord (3.2.3)
  21
+      activemodel (= 3.2.3)
  22
+      activesupport (= 3.2.3)
  23
+      arel (~> 3.0.2)
  24
+      tzinfo (~> 0.3.29)
  25
+    activeresource (3.2.3)
  26
+      activemodel (= 3.2.3)
  27
+      activesupport (= 3.2.3)
  28
+    activesupport (3.2.3)
  29
+      i18n (~> 0.6)
  30
+      multi_json (~> 1.0)
  31
+    arel (3.0.2)
  32
+    builder (3.0.0)
  33
+    diff-lcs (1.1.3)
  34
+    erubis (2.7.0)
  35
+    git (1.2.5)
  36
+    hike (1.2.1)
  37
+    i18n (0.6.0)
  38
+    jeweler (1.8.3)
  39
+      bundler (~> 1.0)
  40
+      git (>= 1.2.5)
  41
+      rake
  42
+      rdoc
  43
+    journey (1.0.3)
  44
+    json (1.7.1)
  45
+    mail (2.4.4)
  46
+      i18n (>= 0.4.0)
  47
+      mime-types (~> 1.16)
  48
+      treetop (~> 1.4.8)
  49
+    mime-types (1.18)
  50
+    multi_json (1.3.4)
  51
+    polyglot (0.3.3)
  52
+    rack (1.4.1)
  53
+    rack-cache (1.2)
  54
+      rack (>= 0.4)
  55
+    rack-ssl (1.3.2)
  56
+      rack
  57
+    rack-test (0.6.1)
  58
+      rack (>= 1.0)
  59
+    rails (3.2.3)
  60
+      actionmailer (= 3.2.3)
  61
+      actionpack (= 3.2.3)
  62
+      activerecord (= 3.2.3)
  63
+      activeresource (= 3.2.3)
  64
+      activesupport (= 3.2.3)
  65
+      bundler (~> 1.0)
  66
+      railties (= 3.2.3)
  67
+    railties (3.2.3)
  68
+      actionpack (= 3.2.3)
  69
+      activesupport (= 3.2.3)
  70
+      rack-ssl (~> 1.3.2)
  71
+      rake (>= 0.8.7)
  72
+      rdoc (~> 3.4)
  73
+      thor (~> 0.14.6)
  74
+    rake (0.9.2.2)
  75
+    rdoc (3.12)
  76
+      json (~> 1.4)
  77
+    rspec (2.8.0)
  78
+      rspec-core (~> 2.8.0)
  79
+      rspec-expectations (~> 2.8.0)
  80
+      rspec-mocks (~> 2.8.0)
  81
+    rspec-core (2.8.0)
  82
+    rspec-expectations (2.8.0)
  83
+      diff-lcs (~> 1.1.2)
  84
+    rspec-mocks (2.8.0)
  85
+    simplecov (0.6.2)
  86
+      multi_json (~> 1.3)
  87
+      simplecov-html (~> 0.5.3)
  88
+    simplecov-html (0.5.3)
  89
+    sprockets (2.1.3)
  90
+      hike (~> 1.2)
  91
+      rack (~> 1.0)
  92
+      tilt (~> 1.1, != 1.3.0)
  93
+    thor (0.14.6)
  94
+    tilt (1.3.3)
  95
+    treetop (1.4.10)
  96
+      polyglot
  97
+      polyglot (>= 0.3.1)
  98
+    tzinfo (0.3.33)
  99
+
  100
+PLATFORMS
  101
+  ruby
  102
+
  103
+DEPENDENCIES
  104
+  bundler (~> 1.1.0)
  105
+  jeweler (~> 1.8.3)
  106
+  rails
  107
+  rdoc (~> 3.12)
  108
+  rspec (~> 2.8.0)
  109
+  simplecov (>= 0.5)
2  VERSION
... ...
@@ -1 +1 @@
1  
-0.0.0
  1
+0.1.0
23  lib/concerned.rb
... ...
@@ -0,0 +1,23 @@
  1
+require 'concerned/module_ext'
  2
+
  3
+module Concerned
  4
+  class << self
  5
+    def require_shared concern
  6
+      require_method "shared/#{concern.to_s.underscore}"
  7
+    end
  8
+
  9
+    def shared_const concern_ns
  10
+      concern_ns.constantize
  11
+    rescue NameError
  12
+      shared_ns_const concern_ns
  13
+    end
  14
+
  15
+    def shared_ns_const concern_ns
  16
+      const_name = "Shared::#{concern_ns}"
  17
+      const_name.constantize
  18
+    rescue NameError
  19
+      raise "No module could be found for: #{concern_ns} or #{const_name}"
  20
+    end
  21
+
  22
+  end
  23
+end
61  lib/concerned/module_ext.rb
... ...
@@ -0,0 +1,61 @@
  1
+class Module
  2
+  def concerned_with(*concerns)
  3
+    options = concerns.extract_options!
  4
+    concerns.flatten.each do |concern|
  5
+      next if concern.blank?
  6
+      require_concern name, concern
  7
+    end
  8
+    shared_concerns([options[:shared]].flatten.compact)
  9
+  end
  10
+
  11
+  def include_concerns(*concerns)
  12
+    options = concerns.extract_options!
  13
+    concerns.flatten.each do |concern|
  14
+      next if concern.blank?
  15
+      require_concern name, concern
  16
+      concern_ns = [name, concern.to_s.camelize].join('::')
  17
+      self.send :include, concern_ns.constantize
  18
+      begin
  19
+        self.extend [concern_ns, 'ClassMethods'].join('::').constantize
  20
+      rescue
  21
+      end
  22
+    end
  23
+    include_shared_concerns([options[:shared]].flatten.compact)
  24
+  end
  25
+
  26
+  def shared_concerns(*concerns)
  27
+    concerns.flatten.each do |concern|
  28
+      next if concern.blank?
  29
+      Concerned.require_shared concern
  30
+    end
  31
+  end
  32
+
  33
+  def include_shared_concerns(*concerns)
  34
+    concerns.flatten.each do |concern|
  35
+      next if concern.blank?
  36
+      Concerned.require_shared concern
  37
+      concern_ns = concern.to_s.camelize
  38
+
  39
+      self.send :include, Concerned.shared_const(concern_ns)
  40
+
  41
+      begin
  42
+        self.extend [concern_ns, 'ClassMethods'].join('::').constantize
  43
+      rescue
  44
+      end
  45
+    end
  46
+  end
  47
+
  48
+  alias_method :shared_concern, :shared_concerns
  49
+  alias_method :include_concern, :include_concerns
  50
+  alias_method :include_shared_concern, :include_shared_concerns
  51
+
  52
+  protected
  53
+
  54
+  def require_concern name, concern
  55
+    require_method "#{name.underscore}/#{concern.to_s.underscore}"
  56
+  end 
  57
+
  58
+  def require_method path
  59
+    defined?(require_dependency) ? require_dependency(path) : require(path)
  60
+  end
  61
+end
28  spec/concerned_spec.rb
... ...
@@ -1,7 +1,29 @@
1  
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
  1
+require 'spec_helper'
  2
+require 'rails'
  3
+require 'concerned'
  4
+require 'active_support/dependencies'
  5
+
  6
+# The same procedure as one that needed for common #require - It should know path.
  7
+$:.unshift File.dirname __FILE__
  8
+
  9
+class FixtureUser
  10
+  concerned_with :scopes, :validations
  11
+  shared_concerns :associations
  12
+  include_shared_concerns :caching
  13
+end
2 14
 
3 15
 describe "Concerned" do
4  
-  it "fails" do
5  
-    fail "hey buddy, you should probably rename this file and start specing for real"
  16
+  describe "#concerned_with" do
  17
+    it 'should require files from #{name.underscore}/#{concern}' do
  18
+      [:scopes, :validations].each do |concern|
  19
+        FixtureUser.new.should respond_to("method_from_#{concern}_concern")
  20
+      end
  21
+    end
  22
+  end 
  23
+   
  24
+  describe "#include_shared_concerns" do
  25
+    it 'should require and include from shared/#{concern}' do
  26
+      FixtureUser.new.should respond_to("method_from_shared_concern")
  27
+    end
6 28
   end
7 29
 end
4  spec/fixture_user/scopes.rb
... ...
@@ -0,0 +1,4 @@
  1
+class FixtureUser
  2
+  def method_from_scopes_concern
  3
+  end
  4
+end
4  spec/fixture_user/validations.rb
... ...
@@ -0,0 +1,4 @@
  1
+class FixtureUser
  2
+  def method_from_validations_concern
  3
+  end
  4
+end
1  spec/shared/associations.rb
... ...
@@ -0,0 +1 @@
  1
+puts "+++++++++"
4  spec/shared/caching.rb
... ...
@@ -0,0 +1,4 @@
  1
+module Caching
  2
+  def method_from_shared_concern
  3
+  end
  4
+end

0 notes on commit 137526b

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