extentions 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rvmrc +38 -0
- data/.travis.yml +15 -0
- data/README.md +1 -1
- data/Rakefile +10 -0
- data/extentions.gemspec +5 -3
- data/lib/extentions.rb +83 -1
- data/lib/extentions/base/extention.rb +72 -0
- data/lib/extentions/base/factory.rb +16 -0
- data/lib/extentions/base/router.rb +18 -0
- data/lib/extentions/extentions_helper.rb +12 -0
- data/lib/extentions/null/extention.rb +12 -0
- data/lib/extentions/renderer.rb +14 -0
- data/lib/extentions/version.rb +1 -1
- data/spec/base/extention_spec.rb +26 -0
- data/spec/extentions_spec.rb +81 -0
- data/spec/null/extention_spec.rb +16 -0
- data/spec/spec_helper_lite.rb +6 -0
- metadata +44 -6
data/.gitignore
CHANGED
data/.rvmrc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3@ex"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.15.8 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
if [[ $- == *i* ]] # check for interactive shells
|
29
|
+
then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
|
30
|
+
else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
|
31
|
+
fi
|
32
|
+
else
|
33
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
34
|
+
rvm --create use "$environment_id" || {
|
35
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
36
|
+
return 1
|
37
|
+
}
|
38
|
+
fi
|
data/.travis.yml
ADDED
data/README.md
CHANGED
data/Rakefile
CHANGED
data/extentions.gemspec
CHANGED
@@ -8,16 +8,18 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = Extentions::VERSION
|
9
9
|
gem.authors = ["Alexander Paramonov"]
|
10
10
|
gem.email = ["alexander.n.paramonov@gmail.com"]
|
11
|
-
gem.summary = %q{
|
12
|
-
gem.description = %q{
|
11
|
+
gem.summary = %q{Plug functionality to the application}
|
12
|
+
gem.description = %q{Allows to create an isolated functionality and plug it into the application}
|
13
13
|
gem.homepage = "http://github.com/AlexParamonov/extentions"
|
14
|
+
gem.license = "MIT"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
|
-
gem.executables = gem.files.grep(%r{^bin/})
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
19
|
gem.require_paths = ["lib"]
|
19
20
|
|
20
21
|
gem.add_development_dependency "rake"
|
21
22
|
gem.add_development_dependency "rspec", ">= 2.6"
|
23
|
+
gem.add_development_dependency "pry"
|
22
24
|
end
|
23
25
|
|
data/lib/extentions.rb
CHANGED
@@ -1,4 +1,86 @@
|
|
1
1
|
require "extentions/version"
|
2
|
-
|
2
|
+
# THINK: Create, Find, Display extention parts
|
3
3
|
module Extentions
|
4
|
+
def self.extentions_for(model, context = nil)
|
5
|
+
Collection.new model, apply_extentions(model, context)
|
6
|
+
# extentions.select do |extention|
|
7
|
+
# extention.applicable_to? params
|
8
|
+
# end.map do |extention|
|
9
|
+
# extention.new(params)
|
10
|
+
# end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.register(extention_class)
|
14
|
+
@@extentions = extentions << extention_class
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.reset
|
18
|
+
@@extentions = []
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def self.apply_extentions(*params)
|
23
|
+
extentions.map do |extention|
|
24
|
+
extention.apply(*params)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.extentions
|
29
|
+
@@extentions ||= []
|
30
|
+
@@extentions.uniq.compact
|
31
|
+
end
|
32
|
+
|
33
|
+
class Collection
|
34
|
+
def initialize(model, extentions)
|
35
|
+
@extentions = extentions
|
36
|
+
@model = model
|
37
|
+
end
|
38
|
+
|
39
|
+
def process!(*params)
|
40
|
+
extentions.each do |extention|
|
41
|
+
extention.process(*params)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_presenter
|
46
|
+
rendered = extentions.each_with_object({}) do |extention, rendered|
|
47
|
+
rendered[extention.to_token] = -> { extention.render }
|
48
|
+
end
|
49
|
+
Presenter.new(model, rendered)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
attr_reader :extentions, :model
|
54
|
+
|
55
|
+
class Presenter < SimpleDelegator
|
56
|
+
def initialize(model, attributes)
|
57
|
+
@attributes = attributes
|
58
|
+
super(model)
|
59
|
+
# TODO is this really needed?
|
60
|
+
__getobj__.pointer = self if __getobj__.respond_to? :pointer=
|
61
|
+
end
|
62
|
+
|
63
|
+
# TODO lazy init
|
64
|
+
def method_missing(method, *args, &block)
|
65
|
+
return cached[method] if cached.include? method
|
66
|
+
|
67
|
+
if attributes.include? method
|
68
|
+
@cached[method] = attributes[method].call.to_s.html_safe
|
69
|
+
else
|
70
|
+
super
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
alias_method :__class__, :class
|
75
|
+
def class
|
76
|
+
__getobj__.class
|
77
|
+
end
|
78
|
+
|
79
|
+
def cached
|
80
|
+
@cached ||= {}
|
81
|
+
end
|
82
|
+
private
|
83
|
+
attr_reader :attributes
|
84
|
+
end
|
85
|
+
end
|
4
86
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative '../null/extention'
|
2
|
+
require_relative '../renderer' if defined? Rails
|
3
|
+
|
4
|
+
module Extentions
|
5
|
+
module Base
|
6
|
+
class Extention
|
7
|
+
def self.apply(*params)
|
8
|
+
extention = new(*params)
|
9
|
+
extention.valid? ? extention : Null::Extention.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure(&block)
|
13
|
+
block.call(config)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.config
|
17
|
+
@config ||= OpenStruct.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def render
|
21
|
+
router.render
|
22
|
+
end
|
23
|
+
|
24
|
+
def process
|
25
|
+
router.process
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_token
|
29
|
+
self.class.name.split("::").at(1).downcase.to_sym
|
30
|
+
# Naming.new(self).tokens.last
|
31
|
+
end
|
32
|
+
|
33
|
+
def valid?(*)
|
34
|
+
raise NotImplementedError
|
35
|
+
end
|
36
|
+
|
37
|
+
private_class_method :new
|
38
|
+
private
|
39
|
+
attr_reader :model, :context
|
40
|
+
|
41
|
+
def initialize(model, context)
|
42
|
+
@model = model
|
43
|
+
@context = context
|
44
|
+
end
|
45
|
+
|
46
|
+
def config
|
47
|
+
self.class.config
|
48
|
+
end
|
49
|
+
|
50
|
+
def view
|
51
|
+
Extentions::Renderer.new(self)
|
52
|
+
end
|
53
|
+
|
54
|
+
def role
|
55
|
+
Factory.new(config.adapter).role_for(model)
|
56
|
+
end
|
57
|
+
|
58
|
+
def controller
|
59
|
+
Controller.new(model, view)
|
60
|
+
end
|
61
|
+
|
62
|
+
def router
|
63
|
+
Router.new(controller, role, context)
|
64
|
+
end
|
65
|
+
|
66
|
+
def model_is_any_of?(*classes)
|
67
|
+
# Note that '&' is the set intersection operator for Arrays.
|
68
|
+
(classes.map(&:to_s) & model.class.ancestors.map(&:name)).any?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Extentions
|
2
|
+
module Base
|
3
|
+
class Router
|
4
|
+
def initialize(controller, role, context)
|
5
|
+
@controller, @role, @context = controller, role, context
|
6
|
+
end
|
7
|
+
|
8
|
+
def process; end
|
9
|
+
def render; "" end
|
10
|
+
|
11
|
+
private
|
12
|
+
def context_action
|
13
|
+
@context_action ||= context.action_name.to_sym
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Extentions
|
2
|
+
class Renderer < AbstractController::Base
|
3
|
+
include AbstractController::Rendering
|
4
|
+
include AbstractController::Helpers
|
5
|
+
include AbstractController::Translation
|
6
|
+
include AbstractController::AssetPaths
|
7
|
+
include ActionController::UrlFor
|
8
|
+
include Rails.application.routes.url_helpers
|
9
|
+
|
10
|
+
def initialize(extention)
|
11
|
+
lookup_context.view_paths = Rails.root + "app/extentions/#{extention.to_token}/views"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/extentions/version.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative "../spec_helper_lite"
|
2
|
+
require_relative "../../lib/extentions/base/extention"
|
3
|
+
require_relative "../../lib/extentions/null/extention"
|
4
|
+
|
5
|
+
describe Extentions::Base::Extention do
|
6
|
+
let(:extention) { Extentions::Base::Extention }
|
7
|
+
let(:args) { [stub, stub] }
|
8
|
+
let(:applied_extention) { extention.apply(*args) }
|
9
|
+
|
10
|
+
describe "#apply" do
|
11
|
+
it "should return new instance if extention is valid" do
|
12
|
+
extention.any_instance.stub(:valid?).and_return(true)
|
13
|
+
applied_extention.should be_a extention
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return NullExtention if extention is invalid"do
|
17
|
+
extention.any_instance.stub(:valid?).and_return(false)
|
18
|
+
applied_extention.should be_a Extentions::Null::Extention
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be convertable to token" do
|
23
|
+
extention.any_instance.stub(valid?: true)
|
24
|
+
applied_extention.to_token.should eq :base
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require_relative "spec_helper_lite"
|
2
|
+
require_relative "../lib/extentions"
|
3
|
+
|
4
|
+
describe Extentions do
|
5
|
+
before(:each) do
|
6
|
+
Extentions.reset
|
7
|
+
end
|
8
|
+
|
9
|
+
after(:each) do
|
10
|
+
Extentions.reset
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#extentions_for" do
|
14
|
+
it "should apply all extentions to arguments" do
|
15
|
+
args = [stub, stub]
|
16
|
+
extention1 = stub(:extention1)
|
17
|
+
extention2 = stub(:extention2)
|
18
|
+
Extentions.register extention1
|
19
|
+
Extentions.register extention2
|
20
|
+
|
21
|
+
extention1.should_receive(:apply).with(*args)
|
22
|
+
extention2.should_receive(:apply).with(*args)
|
23
|
+
|
24
|
+
Extentions.extentions_for(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return Extentions::Collection" do
|
28
|
+
Extentions.extentions_for(stub).should be_a Extentions::Collection
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "Collection" do
|
33
|
+
describe "#build_presener" do
|
34
|
+
it "should return an object in format token => string" do
|
35
|
+
first_rendered_content = stub(:first_rendered_content)
|
36
|
+
first_rendered_content.stub_chain(:to_s, :html_safe).and_return('first extention rendered')
|
37
|
+
second_rendered_content = stub(:second_rendered_content)
|
38
|
+
second_rendered_content.stub_chain(:to_s, :html_safe).and_return('second extention rendered')
|
39
|
+
|
40
|
+
first_extention = stub(:first_extention, to_token: :first_token, render: first_rendered_content)
|
41
|
+
second_extention = stub(:second_extention, to_token: :second_token, render: second_rendered_content)
|
42
|
+
|
43
|
+
collection = Extentions::Collection.new stub, [first_extention, second_extention]
|
44
|
+
presenter = collection.build_presenter
|
45
|
+
|
46
|
+
presenter.first_token.should eq 'first extention rendered'
|
47
|
+
presenter.second_token.should eq 'second extention rendered'
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should decorate a model" do
|
51
|
+
model = stub(:model)
|
52
|
+
model.stub(:name).and_return('Model name')
|
53
|
+
presenter = Extentions.extentions_for(model).build_presenter
|
54
|
+
presenter.name.should eq model.name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#process!" do
|
59
|
+
it "should process all extentions" do
|
60
|
+
optional_args = stub
|
61
|
+
first_extention = stub(:first_extention)
|
62
|
+
second_extention = stub(:second_extention)
|
63
|
+
|
64
|
+
first_extention.should_receive(:process).with(optional_args)
|
65
|
+
second_extention.should_receive(:process).with(optional_args)
|
66
|
+
|
67
|
+
collection = Extentions::Collection.new stub, [first_extention, second_extention]
|
68
|
+
collection.process!(optional_args)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "Presenter" do
|
73
|
+
it "should delegate #class method to a model" do
|
74
|
+
model = stub
|
75
|
+
presenter = Extentions::Collection::Presenter.new(model, {})
|
76
|
+
presenter.class.should eq model.class
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative "../spec_helper_lite"
|
2
|
+
require_relative "../../lib/extentions/null/extention"
|
3
|
+
|
4
|
+
describe Extentions::Null::Extention do
|
5
|
+
it "#to_token should be :null" do
|
6
|
+
subject.to_token.should eq :null
|
7
|
+
end
|
8
|
+
|
9
|
+
it "#render should be null" do
|
10
|
+
subject.render.should be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "#valid? should be true" do
|
14
|
+
subject.should be_valid
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: extentions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -43,8 +43,23 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '2.6'
|
46
|
-
|
47
|
-
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Allows to create an isolated functionality and plug it into the application
|
48
63
|
email:
|
49
64
|
- alexander.n.paramonov@gmail.com
|
50
65
|
executables: []
|
@@ -52,15 +67,28 @@ extensions: []
|
|
52
67
|
extra_rdoc_files: []
|
53
68
|
files:
|
54
69
|
- .gitignore
|
70
|
+
- .rvmrc
|
71
|
+
- .travis.yml
|
55
72
|
- Gemfile
|
56
73
|
- LICENSE
|
57
74
|
- README.md
|
58
75
|
- Rakefile
|
59
76
|
- extentions.gemspec
|
60
77
|
- lib/extentions.rb
|
78
|
+
- lib/extentions/base/extention.rb
|
79
|
+
- lib/extentions/base/factory.rb
|
80
|
+
- lib/extentions/base/router.rb
|
81
|
+
- lib/extentions/extentions_helper.rb
|
82
|
+
- lib/extentions/null/extention.rb
|
83
|
+
- lib/extentions/renderer.rb
|
61
84
|
- lib/extentions/version.rb
|
85
|
+
- spec/base/extention_spec.rb
|
86
|
+
- spec/extentions_spec.rb
|
87
|
+
- spec/null/extention_spec.rb
|
88
|
+
- spec/spec_helper_lite.rb
|
62
89
|
homepage: http://github.com/AlexParamonov/extentions
|
63
|
-
licenses:
|
90
|
+
licenses:
|
91
|
+
- MIT
|
64
92
|
post_install_message:
|
65
93
|
rdoc_options: []
|
66
94
|
require_paths:
|
@@ -71,16 +99,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
99
|
- - ! '>='
|
72
100
|
- !ruby/object:Gem::Version
|
73
101
|
version: '0'
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
hash: 197766875278069449
|
74
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
106
|
none: false
|
76
107
|
requirements:
|
77
108
|
- - ! '>='
|
78
109
|
- !ruby/object:Gem::Version
|
79
110
|
version: '0'
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
hash: 197766875278069449
|
80
114
|
requirements: []
|
81
115
|
rubyforge_project:
|
82
116
|
rubygems_version: 1.8.25
|
83
117
|
signing_key:
|
84
118
|
specification_version: 3
|
85
|
-
summary:
|
86
|
-
test_files:
|
119
|
+
summary: Plug functionality to the application
|
120
|
+
test_files:
|
121
|
+
- spec/base/extention_spec.rb
|
122
|
+
- spec/extentions_spec.rb
|
123
|
+
- spec/null/extention_spec.rb
|
124
|
+
- spec/spec_helper_lite.rb
|