penetrator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .rvmrc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in penetrator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yury Batenko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # Penetrator
2
+
3
+ This gem aimed to help with reuse code in ruby projects.
4
+ Highly inspired from http://github.com/makandra/modularity gem but slightly modified for supporting
5
+ conventional *super* inheritance chaining methods.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'penetrator'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install penetrator
21
+
22
+ ## Usage
23
+ (Rails specific example)
24
+
25
+ *config/application.rb*
26
+ ```ruby
27
+ config.autoload_paths += Dir[Rails.root.join( 'app', '**/*' )].select { |fn| File.directory?(fn) }
28
+ ```
29
+
30
+ *app/controllers/traits/crudable_trait.rb*
31
+ ```ruby
32
+ module CrudableTrait
33
+ #
34
+ # Implementation
35
+ public
36
+
37
+ def index
38
+ @accomodation = resource
39
+ respond_to do |format|
40
+ format.html { render layout: take_layout }
41
+ format.json { render json: resources }
42
+ format.js
43
+ end
44
+ end
45
+
46
+ def show
47
+ respond_to do |format|
48
+ format.html { render layout: take_layout }
49
+ format.json { render json: resource }
50
+ format.js
51
+ end
52
+ end
53
+
54
+ # ... and so on
55
+
56
+ private
57
+ def take_layout
58
+ # ...
59
+ end
60
+
61
+ def resource
62
+ @_resource ||= resource_class.all
63
+ end
64
+ end
65
+ ```
66
+
67
+ *app/controllers/accomodations_controller.rb*
68
+ ```ruby
69
+ class AccomodationsController < ApplicationController
70
+ #
71
+ # CrudableTrait required parameters
72
+ private
73
+ def resource_class
74
+ Accomodation
75
+ end
76
+
77
+ behave_like "crudable"
78
+
79
+ # Override public traits method
80
+ def index
81
+ if current_user.is_admin?
82
+ # ...
83
+ else
84
+ super
85
+ end
86
+ end
87
+
88
+ private
89
+ # Override traits methods
90
+ #
91
+ def default_order
92
+ "accomodations.name desc"
93
+ end
94
+ end
95
+ ```
96
+
97
+
98
+
99
+ ## Contributing
100
+
101
+ 1. Fork it
102
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
103
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
104
+ 4. Push to the branch (`git push origin my-new-feature`)
105
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ desc 'Default: Run all specs.'
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs = ["lib"]
10
+ t.warning = true
11
+ t.test_files = FileList['spec/**/*_spec.rb']
12
+ end
@@ -0,0 +1,61 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # These methods are backported from Rails so modularity works with plain Ruby.
3
+
4
+ module Penetrator
5
+ class Inflector
6
+ class << self
7
+
8
+ # File activesupport/lib/active_support/inflector.rb, line 178
9
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
10
+ if first_letter_in_uppercase
11
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
12
+ else
13
+ lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
14
+ end
15
+ end
16
+
17
+
18
+ if Module.method(:const_get).arity == 1
19
+ # Tries to find a constant with the name specified in the argument string:
20
+ #
21
+ # "Module".constantize # => Module
22
+ # "Test::Unit".constantize # => Test::Unit
23
+ #
24
+ # The name is assumed to be the one of a top-level constant, no matter whether
25
+ # it starts with "::" or not. No lexical context is taken into account:
26
+ #
27
+ # C = 'outside'
28
+ # module M
29
+ # C = 'inside'
30
+ # C # => 'inside'
31
+ # "C".constantize # => 'outside', same as ::C
32
+ # end
33
+ #
34
+ # NameError is raised when the name is not in CamelCase or the constant is
35
+ # unknown.
36
+ def constantize(camel_cased_word)
37
+ names = camel_cased_word.split('::')
38
+ names.shift if names.empty? || names.first.empty?
39
+
40
+ constant = Object
41
+ names.each do |name|
42
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
43
+ end
44
+ constant
45
+ end
46
+ else
47
+ def constantize(camel_cased_word) #:nodoc:
48
+ names = camel_cased_word.split('::')
49
+ names.shift if names.empty? || names.first.empty?
50
+
51
+ constant = Object
52
+ names.each do |name|
53
+ constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
54
+ end
55
+ constant
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Penetrator
3
+ VERSION = "0.0.1"
4
+ end
data/lib/penetrator.rb ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "penetrator/inflector"
3
+ require "penetrator/version"
4
+
5
+ module Penetrator
6
+ module Behavior
7
+
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def behave_like(trait_name, *args)
14
+ full_name = "#{Penetrator::Inflector.camelize(trait_name.to_s)}Trait"
15
+ trait = Penetrator::Inflector.constantize(full_name)
16
+ trait_args_var = "@@" + trait_name.to_s.downcase.gsub(/[^_a-z]+/, '_')+"_args"
17
+ self.class_variable_set(trait_args_var.to_sym, (args || nil))
18
+ include trait
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ Object.send :include, Penetrator::Behavior
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/penetrator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Yury Batenko"]
6
+ gem.email = ["jurbat@gmail.com"]
7
+ gem.description = %q{Implement traits behavior to get rid of code repetition}
8
+ gem.summary = %q{Implement traits behavior to get rid of code repetition}
9
+ gem.homepage = "https://github.com/svenyurgensson/penetrator"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "penetrator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Penetrator::VERSION
17
+ if RUBY_VERSION < '1.9.0'
18
+ gem.add_development_dependency "minitest"
19
+ end
20
+ gem.add_development_dependency "mocha"
21
+ end
data/spec/core_spec.rb ADDED
@@ -0,0 +1,140 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'penetrator'
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
5
+ require 'mocha'
6
+
7
+ describe 'Class' do
8
+ it 'should be accesable' do
9
+ Class.must_respond_to :behave_like
10
+ end
11
+ end # Class method
12
+
13
+
14
+ describe 'mixing behavior' do
15
+
16
+ module FirstTrait
17
+ def test; end
18
+ end
19
+
20
+ it 'add trait methods' do
21
+ class Victim
22
+ behave_like 'first'
23
+ end
24
+
25
+ Victim.new.must_respond_to :test
26
+ end
27
+
28
+
29
+ module Outer
30
+ module InnerTrait
31
+ def inner_test; end
32
+ end
33
+ end
34
+
35
+
36
+ it 'add traits methods from nested modules' do
37
+ class Victim
38
+ behave_like 'outer/inner'
39
+ end
40
+ Victim.new.must_respond_to :inner_test
41
+ end
42
+
43
+ end # mixing behavior
44
+
45
+
46
+ describe 'methods chainings' do
47
+
48
+ module SuperTrait
49
+ def test
50
+ 'from trait'
51
+ end
52
+ end
53
+
54
+ it 'have traits methods' do
55
+ class Victim
56
+ behave_like 'super'
57
+ def test
58
+ 'this from class and that ' + super
59
+ end
60
+ end # Victim
61
+
62
+ Victim.new.test.must_equal 'this from class and that from trait'
63
+ end
64
+
65
+ end # methods chainings
66
+
67
+ describe 'callbacks' do
68
+ module CallbackTrait
69
+ end
70
+
71
+ it 'have traits methods' do
72
+ class Victim; end
73
+ CallbackTrait.expects(:included).with(Victim)
74
+ class Victim
75
+ behave_like 'callback'
76
+ end
77
+ end
78
+ end # callbacks
79
+
80
+ describe 'visibility' do
81
+ module VisibilityTrait
82
+ def public_method_from_trait; end
83
+ protected
84
+ def protected_method_from_trait; end
85
+ private
86
+ def private_method_from_trait; end
87
+ end
88
+
89
+ it 'allow trait to define methods with different visibility' do
90
+ class Victim
91
+ behave_like 'visibility'
92
+ end
93
+
94
+ instance = Victim.new
95
+ instance.public_methods.collect(&:to_s).must_include('public_method_from_trait')
96
+ instance.protected_methods.collect(&:to_s).must_include('protected_method_from_trait')
97
+ instance.private_methods.collect(&:to_s).must_include('private_method_from_trait')
98
+
99
+ end
100
+
101
+ end # visibility
102
+
103
+ describe 'trait arguments' do
104
+ module HaveArgsTrait; end
105
+
106
+ it 'receive trait arguments' do
107
+ class Victim; end
108
+ Victim.expects(:behave_like).with('have_args', 'arg1', 'arg2')
109
+
110
+ class Victim
111
+ behave_like 'have_args', 'arg1', 'arg2'
112
+ end
113
+
114
+ end
115
+
116
+ it 'base holds arguments' do
117
+ class Victim
118
+ behave_like 'have_args', 'arg1', 'arg2'
119
+ end
120
+ Victim.class_variable_get(:@@have_args_args).must_equal ['arg1','arg2']
121
+ end
122
+
123
+
124
+ describe 'arguments defined before trait included' do
125
+ module HandyTrait
126
+ def self.included(base)
127
+ base.send :stub, base.class_variable_get(:@@handy_args)
128
+ end
129
+ end
130
+
131
+ class Victim; end
132
+ Victim.expects(:stub).with(['arg'])
133
+
134
+ class Victim
135
+ behave_like :handy, 'arg'
136
+ end
137
+ end # 'trait use arguments'
138
+
139
+
140
+ end # trait argument
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: penetrator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yury Batenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mocha
16
+ requirement: &70150947089080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70150947089080
25
+ description: Implement traits behavior to get rid of code repetition
26
+ email:
27
+ - jurbat@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/penetrator.rb
38
+ - lib/penetrator/inflector.rb
39
+ - lib/penetrator/version.rb
40
+ - penetrator.gemspec
41
+ - spec/core_spec.rb
42
+ homepage: https://github.com/svenyurgensson/penetrator
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.15
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Implement traits behavior to get rid of code repetition
66
+ test_files:
67
+ - spec/core_spec.rb