simple_controller 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20f1e8a4f6f58fa76e1ee3679e43aae283a7fd4e
4
+ data.tar.gz: 43d4c9d22826ae45ad8b8dac49e7a3b8b4de47c6
5
+ SHA512:
6
+ metadata.gz: a11908aca45ebbfa600a58167c38b1cbbc6d84983bee759f3c0120fcbac25c716e2ce8450d2553edb69321389be53b871802d842148c4e077ec87a7dffd516a6
7
+ data.tar.gz: 0af55254de294c9d72731feedb60f05f455982e14839f7943b5a794fe7dd4b607b5c5c39c741dc08a7efd4d10c49fefc6493fcce1ed1d6a55d3d4f5c39c944de
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 2.1.0
5
+ - ruby-head
6
+ matrix:
7
+ allow_failures:
8
+ - rvm: ruby-head
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_controller.gemspec
4
+ gemspec
5
+
6
+ gem "bundler", "> 1.3"
7
+ gem "rake", "~> 10.0"
8
+
9
+ group :test do
10
+ gem "rspec"
11
+ gem "codeclimate-test-reporter"
12
+ gem 'pry-byebug'
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Steve Chung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # SimpleController
2
+
3
+ Use the Ruby on Rails Controller pattern outside of the Rails request stack.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_controller'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_controller
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class UserController < SimpleController::Base
25
+ before_action do
26
+ @user = User.find(params[:user_id])
27
+ end
28
+
29
+ def touch
30
+ @user.touch
31
+ @user
32
+ end
33
+ end
34
+
35
+ UserController.call(:touch, user_id: 1) # => returns User object
36
+ UserController.new.call(:touch, user_id: 1) # => same as above
37
+ ```
38
+
39
+ It works like a Rails Controller, but has only has the following features:
40
+ - Callbacks
41
+ - `params`
42
+ - `action_name`
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_controller"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ autoload = false
2
+ # autoload = true #uncomment for testing purposes only, not covered by rspec
3
+
4
+ require "simple_controller/version"
5
+
6
+ require 'active_support/callbacks'
7
+ require 'active_support/hash_with_indifferent_access'
8
+
9
+ if autoload && defined?(Rails)
10
+ require 'simple_controller/engine'
11
+ else
12
+ require 'simple_controller/base'
13
+ end
14
+
15
+
16
+ module SimpleController
17
+ # Your code goes here...
18
+ end
@@ -0,0 +1,9 @@
1
+ require 'simple_controller/core'
2
+ require 'simple_controller/callbacks'
3
+
4
+ module SimpleController
5
+ class Base
6
+ include Core
7
+ include Callbacks # included last to wrap method
8
+ end
9
+ end
@@ -0,0 +1,188 @@
1
+ # based on: https://github.com/rails/rails/blob/d2876141d08341ec67cf6a11a073d1acfb920de7/actionpack/lib/abstract_controller/callbacks.rb
2
+ module SimpleController
3
+ module Callbacks
4
+ extend ActiveSupport::Concern
5
+
6
+ # Uses ActiveSupport::Callbacks as the base functionality. For
7
+ # more details on the whole callback system, read the documentation
8
+ # for ActiveSupport::Callbacks.
9
+ include ActiveSupport::Callbacks
10
+
11
+ included do
12
+ define_callbacks :call_action
13
+ end
14
+
15
+ # Override SimpleController::Base's call to run the
16
+ # call callbacks around the normal behavior.
17
+ def call_action(*args)
18
+ run_callbacks(:call_action) do
19
+ super
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+ # If +:only+ or +:except+ are used, convert the options into the
25
+ # +:if+ and +:unless+ options of ActiveSupport::Callbacks.
26
+ #
27
+ # The basic idea is that <tt>:only => :index</tt> gets converted to
28
+ # <tt>:if => proc {|c| c.action_name == "index" }</tt>.
29
+ #
30
+ # Note that <tt>:only</tt> has priority over <tt>:if</tt> in case they
31
+ # are used together.
32
+ #
33
+ # only: :index, if: -> { true } # the :if option will be ignored.
34
+ #
35
+ # Note that <tt>:if</tt> has priority over <tt>:except</tt> in case they
36
+ # are used together.
37
+ #
38
+ # except: :index, if: -> { true } # the :except option will be ignored.
39
+ #
40
+ # ==== Options
41
+ # * <tt>only</tt> - The callback should be run only for this action
42
+ # * <tt>except</tt> - The callback should be run for all actions except this action
43
+ def _normalize_callback_options(options)
44
+ _normalize_callback_option(options, :only, :if)
45
+ _normalize_callback_option(options, :except, :unless)
46
+ end
47
+
48
+ def _normalize_callback_option(options, from, to) # :nodoc:
49
+ if from = options[from]
50
+ from = Array(from).map {|o| "action_name == '#{o}'"}.join(" || ")
51
+ options[to] = Array(options[to]).unshift(from)
52
+ end
53
+ end
54
+
55
+ # Take callback names and an optional callback proc, normalize them,
56
+ # then call the block with each callback. This allows us to abstract
57
+ # the normalization across several methods that use it.
58
+ #
59
+ # ==== Parameters
60
+ # * <tt>callbacks</tt> - An array of callbacks, with an optional
61
+ # options hash as the last parameter.
62
+ # * <tt>block</tt> - A proc that should be added to the callbacks.
63
+ #
64
+ # ==== Block Parameters
65
+ # * <tt>name</tt> - The callback to be added
66
+ # * <tt>options</tt> - A hash of options to be used when adding the callback
67
+ def _insert_callbacks(callbacks, block = nil)
68
+ options = callbacks.extract_options!
69
+ _normalize_callback_options(options)
70
+ callbacks.push(block) if block
71
+ callbacks.each do |callback|
72
+ yield callback, options
73
+ end
74
+ end
75
+
76
+ ##
77
+ # :method: before_action
78
+ #
79
+ # :call-seq: before_action(names, block)
80
+ #
81
+ # Append a callback before actions. See _insert_callbacks for parameter details.
82
+
83
+ ##
84
+ # :method: prepend_before_action
85
+ #
86
+ # :call-seq: prepend_before_action(names, block)
87
+ #
88
+ # Prepend a callback before actions. See _insert_callbacks for parameter details.
89
+
90
+ ##
91
+ # :method: skip_before_action
92
+ #
93
+ # :call-seq: skip_before_action(names)
94
+ #
95
+ # Skip a callback before actions. See _insert_callbacks for parameter details.
96
+
97
+ ##
98
+ # :method: append_before_action
99
+ #
100
+ # :call-seq: append_before_action(names, block)
101
+ #
102
+ # Append a callback before actions. See _insert_callbacks for parameter details.
103
+
104
+ ##
105
+ # :method: after_action
106
+ #
107
+ # :call-seq: after_action(names, block)
108
+ #
109
+ # Append a callback after actions. See _insert_callbacks for parameter details.
110
+
111
+ ##
112
+ # :method: prepend_after_action
113
+ #
114
+ # :call-seq: prepend_after_action(names, block)
115
+ #
116
+ # Prepend a callback after actions. See _insert_callbacks for parameter details.
117
+
118
+ ##
119
+ # :method: skip_after_action
120
+ #
121
+ # :call-seq: skip_after_action(names)
122
+ #
123
+ # Skip a callback after actions. See _insert_callbacks for parameter details.
124
+
125
+ ##
126
+ # :method: append_after_action
127
+ #
128
+ # :call-seq: append_after_action(names, block)
129
+ #
130
+ # Append a callback after actions. See _insert_callbacks for parameter details.
131
+
132
+ ##
133
+ # :method: around_action
134
+ #
135
+ # :call-seq: around_action(names, block)
136
+ #
137
+ # Append a callback around actions. See _insert_callbacks for parameter details.
138
+
139
+ ##
140
+ # :method: prepend_around_action
141
+ #
142
+ # :call-seq: prepend_around_action(names, block)
143
+ #
144
+ # Prepend a callback around actions. See _insert_callbacks for parameter details.
145
+
146
+ ##
147
+ # :method: skip_around_action
148
+ #
149
+ # :call-seq: skip_around_action(names)
150
+ #
151
+ # Skip a callback around actions. See _insert_callbacks for parameter details.
152
+
153
+ ##
154
+ # :method: append_around_action
155
+ #
156
+ # :call-seq: append_around_action(names, block)
157
+ #
158
+ # Append a callback around actions. See _insert_callbacks for parameter details.
159
+
160
+ # set up before_action, prepend_before_action, skip_before_action, etc.
161
+ # for each of before, after, and around.
162
+ [:before, :after, :around].each do |callback|
163
+ define_method "#{callback}_action" do |*names, &blk|
164
+ _insert_callbacks(names, blk) do |name, options|
165
+ set_callback(:call_action, callback, name, options)
166
+ end
167
+ end
168
+
169
+ define_method "prepend_#{callback}_action" do |*names, &blk|
170
+ _insert_callbacks(names, blk) do |name, options|
171
+ set_callback(:call_action, callback, name, options.merge(:prepend => true))
172
+ end
173
+ end
174
+
175
+ # Skip a before, after or around callback. See _insert_callbacks
176
+ # for details on the allowed parameters.
177
+ define_method "skip_#{callback}_action" do |*names|
178
+ _insert_callbacks(names) do |name, options|
179
+ skip_callback(:call_action, callback, name, options)
180
+ end
181
+ end
182
+
183
+ # *_action is the same as append_*_action
184
+ alias_method :"append_#{callback}_action", :"#{callback}_action"
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,28 @@
1
+ module SimpleController
2
+ module Core
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attr_reader :params, :action_name
7
+ end
8
+
9
+ def call(action_name, params={})
10
+ @params ||= ActiveSupport::HashWithIndifferentAccess.new(params)
11
+ @action_name ||= action_name.to_s
12
+
13
+ call_action
14
+ end
15
+
16
+ protected
17
+
18
+ def call_action
19
+ public_send action_name
20
+ end
21
+
22
+ module ClassMethods
23
+ def call(*args)
24
+ new.call *args
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module SimpleController
2
+ class Engine < ::Rails::Engine
3
+ config.autoload_paths += %W[#{config.root}/lib/]
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleController
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_controller/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_controller"
8
+ spec.version = SimpleController::VERSION
9
+ spec.authors = ["Steve Chung"]
10
+ spec.email = ["hello@stevenchung.ca"]
11
+
12
+ spec.summary = "Rails Controllers, but general purpose."
13
+ spec.homepage = "https://github.com/FinalCAD/simple_controller"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Chung
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - hello@stevenchung.ca
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/simple_controller.rb
44
+ - lib/simple_controller/base.rb
45
+ - lib/simple_controller/callbacks.rb
46
+ - lib/simple_controller/core.rb
47
+ - lib/simple_controller/engine.rb
48
+ - lib/simple_controller/version.rb
49
+ - simple_controller.gemspec
50
+ homepage: https://github.com/FinalCAD/simple_controller
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.4.5.1
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Rails Controllers, but general purpose.
74
+ test_files: []