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 CHANGED
@@ -15,6 +15,7 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ tags
18
19
 
19
20
  ## generic files to ignore
20
21
  *~
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
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - ruby-head
5
+ - rbx-19mode
6
+
7
+ matrix:
8
+ allow_failures:
9
+ - rvm: ruby-head
10
+ - rvm: rbx-19mode
11
+
12
+ before_install: gem install bundler --pre
13
+
14
+ script:
15
+ - "bundle exec rake ci:all"
data/README.md CHANGED
@@ -60,5 +60,5 @@ Contributing
60
60
 
61
61
  Copyright
62
62
  ---------
63
- Copyright © 2012 Alexander Paramonov.
63
+ Copyright © 2013 Alexander Paramonov.
64
64
  Released under the MIT License. See the LICENSE file for further details.
data/Rakefile CHANGED
@@ -1 +1,11 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ namespace :ci do
5
+ desc "Run tests"
6
+ RSpec::Core::RakeTask.new('all') do |t|
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ t.rspec_opts = '-fprogress'
9
+ t.verbose = true
10
+ end
11
+ end
@@ -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{[name reserved]Plug functionality to the application}
12
- gem.description = %q{[name reserved]Allows to create an isolated functionality and plug it into the application}
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/}).map{ |f| File.basename(f) }
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
 
@@ -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,16 @@
1
+ module Extentions
2
+ module Base
3
+ class Factory
4
+ def initialize(adapter = nil)
5
+ @adapter = adapter
6
+ end
7
+
8
+ def role_for(model)
9
+ raise NotImplementedError
10
+ end
11
+
12
+ private
13
+ attr_reader :adapter
14
+ end
15
+ end
16
+ 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,12 @@
1
+ module Extentions
2
+ module ExtentionsHelper
3
+ def extentions_for(model, context=self)
4
+ Extentions.extentions_for(model, context)
5
+ end
6
+
7
+ def presenter_for(model, context = self)
8
+ extentions_for(model, context).build_presenter
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,12 @@
1
+ module Extentions
2
+ module Null
3
+ class Extention
4
+ def render(*); end
5
+ def process(*); end
6
+ def to_token
7
+ :null
8
+ end
9
+ def valid?(*); true end
10
+ end
11
+ end
12
+ end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Extentions
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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
@@ -0,0 +1,6 @@
1
+ require 'pry'
2
+
3
+ ENV['RAILS_ENV'] ||= 'test'
4
+ require "rspec"
5
+
6
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
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.1
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
- description: ! '[name reserved]Allows to create an isolated functionality and plug
47
- it into the application'
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: ! '[name reserved]Plug functionality to the application'
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