modularity 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Henning Koch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1 @@
1
+ = modularity - Traits and partial classes for Ruby
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ # require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc 'Default: Run all specs.'
7
+ task :default => :spec
8
+
9
+ desc "Run all specs"
10
+ Spec::Rake::SpecTask.new() do |t|
11
+ t.spec_opts = ['--options', "\"spec/spec.opts\""]
12
+ t.spec_files = FileList['spec/**/*_spec.rb']
13
+ end
14
+
15
+ desc 'Generate documentation for the modularity gem'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'modularity'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gemspec|
27
+ gemspec.name = "modularity"
28
+ gemspec.summary = "Traits and partial classes for Ruby"
29
+ gemspec.email = "github@makandra.de"
30
+ gemspec.homepage = "http://github.com/makandra/modularity"
31
+ gemspec.description = "Traits and partial classes for Ruby"
32
+ gemspec.authors = ["Henning Koch"]
33
+ end
34
+ rescue LoadError
35
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
36
+ end
37
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,11 @@
1
+ module Modularity
2
+ module AsTrait
3
+
4
+ def as_trait(&block)
5
+ @as_trait = block
6
+ end
7
+
8
+ end
9
+ end
10
+
11
+ Module.send :include, Modularity::AsTrait
@@ -0,0 +1,24 @@
1
+ require 'modularity/as_trait'
2
+ require 'modularity/inflector'
3
+
4
+ module Modularity
5
+ module Does
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def does(trait_name, *args)
13
+ trait_name = Modularity::Inflector.camelize(trait_name.to_s)
14
+ trait = Modularity::Inflector.constantize(trait_name)
15
+ include(trait)
16
+ macro = trait.instance_variable_get("@as_trait") or raise "Missing as_trait directive in #{trait_name}"
17
+ class_exec(*args, &macro)
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+
24
+ Object.send :include, Modularity::Does
@@ -0,0 +1,27 @@
1
+ # These methods are backported from Rails so modularity works with plain Ruby.
2
+
3
+ module Modularity
4
+ class Inflector
5
+ class << self
6
+
7
+ # File activesupport/lib/active_support/inflector.rb, line 160
8
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
9
+ if first_letter_in_uppercase
10
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
11
+ else
12
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
13
+ end
14
+ end
15
+
16
+ # File activesupport/lib/active_support/inflector.rb, line 278
17
+ def constantize(camel_cased_word)
18
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
19
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
20
+ end
21
+
22
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
23
+ end
24
+
25
+ end
26
+ end
27
+ end
data/lib/modularity.rb ADDED
@@ -0,0 +1 @@
1
+ require 'modularity/does'
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{modularity}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Henning Koch"]
12
+ s.date = %q{2010-02-13}
13
+ s.description = %q{Traits and partial classes for Ruby}
14
+ s.email = %q{github@makandra.de}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/modularity.rb",
24
+ "lib/modularity/as_trait.rb",
25
+ "lib/modularity/does.rb",
26
+ "lib/modularity/inflector.rb",
27
+ "modularity.gemspec",
28
+ "spec/as_trait_spec.rb",
29
+ "spec/does_spec.rb",
30
+ "spec/rcov.opts",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/makandra/modularity}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Traits and partial classes for Ruby}
39
+ s.test_files = [
40
+ "spec/spec_helper.rb",
41
+ "spec/does_spec.rb",
42
+ "spec/as_trait_spec.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
55
+
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ module Trait
4
+ as_trait do
5
+ "hi world"
6
+ end
7
+ end
8
+
9
+ module ParametrizedTrait
10
+ as_trait do |name|
11
+ "hi, #{name}"
12
+ end
13
+ end
14
+
15
+ describe Modularity::AsTrait do
16
+
17
+ describe 'as_trait' do
18
+
19
+ it "should let modules save a proc upon loading" do
20
+ Trait.instance_variable_get("@as_trait").call.should == "hi world"
21
+ end
22
+
23
+ it "should let modules save a proc with parameters upon loading" do
24
+ ParametrizedTrait.instance_variable_get("@as_trait").call("jean").should == "hi, jean"
25
+ end
26
+
27
+ end
28
+
29
+ end
data/spec/does_spec.rb ADDED
@@ -0,0 +1,87 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ module Trait
4
+ as_trait {}
5
+ end
6
+
7
+ module Trait::Child
8
+ as_trait {}
9
+ end
10
+
11
+ module CallMethod
12
+ as_trait do |field|
13
+ send(field)
14
+ end
15
+ end
16
+
17
+ module Visibility
18
+ as_trait do
19
+ def public_method_from_trait
20
+ end
21
+ protected
22
+ def protected_method_from_trait
23
+ end
24
+ private
25
+ def private_method_from_trait
26
+ end
27
+ end
28
+ end
29
+
30
+ module DefineConstantMethod
31
+ as_trait do |name, return_value|
32
+ define_method name do
33
+ return_value
34
+ end
35
+ end
36
+ end
37
+
38
+ class Doer
39
+ end
40
+
41
+ describe Modularity::AsTrait do
42
+
43
+ describe 'does' do
44
+
45
+ it "should include the named module" do
46
+ Doer.should_receive(:include).with(Trait)
47
+ Doer.class_eval do
48
+ does "trait"
49
+ end
50
+ end
51
+
52
+ it "should include a namespaced module, using slash-notation like require" do
53
+ Doer.should_receive(:include).with(Trait::Child)
54
+ Doer.class_eval do
55
+ does "trait/child"
56
+ end
57
+ end
58
+
59
+ it "should class_eval the as_trait proc on the doer" do
60
+ Doer.should_receive(:foo)
61
+ Doer.class_eval do
62
+ does "call_method", :foo
63
+ end
64
+ end
65
+
66
+ it "should allow the trait to define methods with different visibility" do
67
+ Doer.class_eval do
68
+ does "visibility"
69
+ end
70
+ instance = Doer.new
71
+ instance.public_methods.should include("public_method_from_trait")
72
+ instance.protected_methods.should include("protected_method_from_trait")
73
+ instance.private_methods.should include("private_method_from_trait")
74
+ end
75
+
76
+ it "should allow the trait to perform metaprogramming acrobatics" do
77
+ Doer.class_eval do
78
+ does "define_constant_method", "some_method", "some_return_value"
79
+ end
80
+ instance = Doer.new
81
+ instance.should respond_to(:some_method)
82
+ instance.some_method.should == "some_return_value"
83
+ end
84
+
85
+ end
86
+
87
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1,2 @@
1
+ --exclude "spec/*,gems/*"
2
+ --rails
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,3 @@
1
+ $: << File.join(File.dirname(__FILE__), "/../lib" )
2
+
3
+ require "#{File.dirname(__FILE__)}/../lib/modularity"
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modularity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Henning Koch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Traits and partial classes for Ruby
17
+ email: github@makandra.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.rdoc
27
+ - Rakefile
28
+ - VERSION
29
+ - lib/modularity.rb
30
+ - lib/modularity/as_trait.rb
31
+ - lib/modularity/does.rb
32
+ - lib/modularity/inflector.rb
33
+ - modularity.gemspec
34
+ - spec/as_trait_spec.rb
35
+ - spec/does_spec.rb
36
+ - spec/rcov.opts
37
+ - spec/spec.opts
38
+ - spec/spec_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/makandra/modularity
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Traits and partial classes for Ruby
67
+ test_files:
68
+ - spec/spec_helper.rb
69
+ - spec/does_spec.rb
70
+ - spec/as_trait_spec.rb