Skip to content
Browse files

readme done

  • Loading branch information...
1 parent 66564c0 commit f55e2f7ad8a15d34a07d6a215eeafa519ebc5ab9 @kristianmandrup committed May 9, 2012
Showing with 106 additions and 35 deletions.
  1. +74 −0 README.md
  2. +0 −19 README.rdoc
  3. +2 −2 Rakefile
  4. +11 −0 lib/concerned.rb
  5. +18 −13 lib/concerned/module_ext.rb
  6. +1 −1 spec/shared/associations.rb
View
74 README.md
@@ -0,0 +1,74 @@
+# Concerned
+
+Use the "concerns" pattern in any Ruby or Rails project.
+
+## Rails 3
+
+In Gemfile:
+
+`gem 'concerned'`
+
+### Usage
+
+This gem comes with the following three helpers, that are added to `Module`.
+To require concern modules (or classes)
+
+* concerned_with
+* shared_concerns
+
+And to include concern modules
+
+* include_concerns
+* include_shared_concerns
+
+The `concerned_with` helper method expects to find a module or class matching the current namespace, fx the following will include `FixtureUser::Scopes` and `FixtureUser::Validations` into `FixtureUser`.
+
+```ruby
+class FixtureUser
+ include_concerns :scopes, :validations
+end
+```
+
+The `include_shared_concerns` expects to find a module in a `shared` folder somewhere in the _load path_ that has a name of either `Caching` or `Shared::Caching` for the following example:
+
+```ruby
+class FixtureUser
+ include_shared_concerns :caching
+end
+```
+
+The `concerned_with` and `shared_concerns` methods are used simply to require shared files following the namespace convention, here: `project/job_matches` and `shared/associations`.
+
+```ruby
+class Project
+ concerned_with :job_matches
+ shared_concerns :associations
+end
+```
+
+## Global config
+
+You can use the `Concerned.extend_enable!` to let the concern helpers also attempt to extend the host module/class with the ClassMethods module of the concerns module (if such exists). Disable it by using: `Concerned.extend_disable!`
+
+Use `Concerned.extend_enabled?` to see if it is currently enabled or not.
+By default this feature is turned off.
+
+It is usually better to use `ActionSupport::Concern` using `extend ActiveSupport::Concern`.
+
+See fx [concerns](http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/)
+
+## Contributing to concerned
+
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
+* Fork the project.
+* Start a feature/bugfix branch.
+* Commit and push until you are happy with your contribution.
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+
+## Copyright
+
+Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
+further details.
+
View
19 README.rdoc
@@ -1,19 +0,0 @@
-= concerned
-
-Description goes here.
-
-== Contributing to concerned
-
-* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
-* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
-* Fork the project.
-* Start a feature/bugfix branch.
-* Commit and push until you are happy with your contribution.
-* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
-* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
-
-== Copyright
-
-Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
-further details.
-
View
4 Rakefile
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
gem.name = "concerned"
gem.homepage = "http://github.com/kristianmandrup/concerned"
gem.license = "MIT"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{Concerns for Ruby (and Rails) projects}
+ gem.description = %Q{Makes it easy to apply the concerns pattern in any Ruby context}
gem.email = "kmandrup@gmail.com"
gem.authors = ["Kristian Mandrup"]
# dependencies defined in Gemfile
View
11 lib/concerned.rb
@@ -19,5 +19,16 @@ def shared_ns_const concern_ns
raise "No module could be found for: #{concern_ns} or #{const_name}"
end
+ def extend_enable!
+ @extend_enabled = true
+ end
+
+ def extend_disable!
+ @extend_enabled = false
+ end
+
+ def extend_enabled?
+ @extend_enabled
+ end
end
end
View
31 lib/concerned/module_ext.rb
@@ -8,28 +8,31 @@ def concerned_with(*concerns)
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_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
+
+ if Concerned.extend_enabled?
+ begin
+ self.extend [concern_ns, 'ClassMethods'].join('::').constantize
+ rescue
+ end
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?
@@ -38,9 +41,11 @@ def include_shared_concerns(*concerns)
self.send :include, Concerned.shared_const(concern_ns)
- begin
- self.extend [concern_ns, 'ClassMethods'].join('::').constantize
- rescue
+ if Concerned.extend_enabled?
+ begin
+ self.extend [concern_ns, 'ClassMethods'].join('::').constantize
+ rescue
+ end
end
end
end
View
2 spec/shared/associations.rb
@@ -1 +1 @@
-puts "+++++++++"
+puts "shared/associations was required ok"

0 comments on commit f55e2f7

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