pragma-rails 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: 88f74fb154bdd34d9c5a8a17a7c219a3c1b992c7
4
+ data.tar.gz: c6521f2f64a5d9df751a7d38b6dcd62e2758626e
5
+ SHA512:
6
+ metadata.gz: d69736b9d80af68b5fd3abec80cadd9e92e3bcdada3d97a7c4b665ca3434602133d87acc6777bee886aca8914a5fb0357a7713f981b3923df86596704843ebea
7
+ data.tar.gz: 343bd16bd1fcd22997623fdce61998ad3fb21fa7c12b622393b4c4c8e072ab548e9c4eee71b4be249899bd8a080eac66f09b35635f5b777afd62ba961f0fe654
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,93 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.3
5
+ Include:
6
+ - '**/Gemfile'
7
+ - '**/Rakefile'
8
+ Exclude:
9
+ - 'bin/*'
10
+ - 'db/**/*'
11
+ - 'vendor/bundle/**/*'
12
+ - 'spec/spec_helper.rb'
13
+ - 'spec/rails_helper.rb'
14
+ - 'spec/support/**/*'
15
+ - 'config/**/*'
16
+ - '**/Rakefile'
17
+ - '**/Gemfile'
18
+
19
+ RSpec/DescribeClass:
20
+ Exclude:
21
+ - 'spec/requests/**/*'
22
+
23
+ Style/BlockDelimiters:
24
+ Exclude:
25
+ - 'spec/**/*'
26
+
27
+ Style/AlignParameters:
28
+ EnforcedStyle: with_fixed_indentation
29
+
30
+ Style/ClosingParenthesisIndentation:
31
+ Enabled: false
32
+
33
+ Metrics/LineLength:
34
+ Max: 100
35
+ AllowURI: true
36
+
37
+ Style/FirstParameterIndentation:
38
+ Enabled: false
39
+
40
+ Style/MultilineMethodCallIndentation:
41
+ EnforcedStyle: indented
42
+
43
+ Style/IndentArray:
44
+ EnforcedStyle: consistent
45
+
46
+ Style/IndentHash:
47
+ EnforcedStyle: consistent
48
+
49
+ Style/SignalException:
50
+ EnforcedStyle: semantic
51
+
52
+ Style/BracesAroundHashParameters:
53
+ EnforcedStyle: context_dependent
54
+
55
+ Lint/EndAlignment:
56
+ AlignWith: variable
57
+ AutoCorrect: true
58
+
59
+ Style/AndOr:
60
+ EnforcedStyle: conditionals
61
+
62
+ Style/MultilineBlockChain:
63
+ Enabled: false
64
+
65
+ RSpec/NamedSubject:
66
+ Enabled: false
67
+
68
+ RSpec/ExampleLength:
69
+ Enabled: false
70
+
71
+ Style/MultilineMethodCallBraceLayout:
72
+ Enabled: false
73
+
74
+ Metrics/MethodLength:
75
+ Enabled: false
76
+
77
+ Metrics/AbcSize:
78
+ Enabled: false
79
+
80
+ Metrics/PerceivedComplexity:
81
+ Enabled: false
82
+
83
+ Metrics/CyclomaticComplexity:
84
+ Enabled: false
85
+
86
+ Metrics/ClassLength:
87
+ Enabled: false
88
+
89
+ RSpec/MessageExpectation:
90
+ Enabled: false
91
+
92
+ RSpec/MessageSpies:
93
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ - 2.4.0
6
+ before_install: gem install bundler -v 1.13.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pragma-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alessandro Desantis
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,41 @@
1
+ # Pragma on Rails
2
+
3
+ [![Build Status](https://img.shields.io/travis/pragmarb/pragma-rails.svg?maxAge=3600&style=flat-square)](https://travis-ci.org/pragmarb/pragma-rails)
4
+ [![Dependency Status](https://img.shields.io/gemnasium/pragmarb/pragma-rails.svg?maxAge=3600&style=flat-square)](https://gemnasium.com/github.com/pragmarb/pragma-rails)
5
+ [![Code Climate](https://img.shields.io/codeclimate/github/pragmarb/pragma-rails.svg?maxAge=3600&style=flat-square)](https://codeclimate.com/github/pragmarb/pragma-rails)
6
+ [![Coveralls](https://img.shields.io/coveralls/pragmarb/pragma-rails.svg?maxAge=3600&style=flat-square)](https://coveralls.io/github/pragmarb/pragma-rails)
7
+
8
+ This gem provides Ruby on Rails integration for the [Pragma](https://github.com/pragmarb/pragma) architecture.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'pragma-rails'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ ```console
21
+ $ bundle
22
+ ```
23
+
24
+ Or install it yourself as:
25
+
26
+ ```console
27
+ $ gem install pragma-rails
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ All documentation is in the [doc](https://github.com/pragmarb/pragma-rails/tree/master/doc)
33
+ folder.
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pragmarb/pragma-rails.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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 "pragma/rails"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ require 'pragma'
3
+ require 'rails'
4
+
5
+ require 'pragma/rails/version'
6
+ require 'pragma/rails/controller'
7
+ require 'pragma/rails/resource_controller'
8
+
9
+ module Pragma
10
+ # Ruby on Rails integration for the Pragma architecture.
11
+ #
12
+ # @author Alessandro Desantis
13
+ module Rails
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ module Pragma
3
+ module Rails
4
+ # This mixin should be included in a Rails controller to provide integration with Pragma
5
+ # operations.
6
+ #
7
+ # @author Alessandro Desantis
8
+ module Controller
9
+ def self.included(klass)
10
+ klass.include InstanceMethods
11
+ end
12
+
13
+ module InstanceMethods # :nodoc:
14
+ # Runs an operation, then responds with the returned status code, headers and resource.
15
+ #
16
+ # @param operation_klass [Class|String] a subclass of +Pragma::Operation::Base+
17
+ def run(operation_klass)
18
+ result = operation_klass.to_s.constantize.call(
19
+ params: operation_params,
20
+ current_user: operation_user
21
+ )
22
+
23
+ result.headers.each_pair do |key, value|
24
+ response.headers[key] = value
25
+ end
26
+
27
+ render status: result.status, json: result.resource.to_hash
28
+ end
29
+
30
+ protected
31
+
32
+ # Returns the parameters to pass to Pragma operations.
33
+ #
34
+ # By default, this is the +params+ hash.
35
+ #
36
+ # @return [Hash]
37
+ def operation_params
38
+ params
39
+ end
40
+
41
+ # Returns the currently authenticated user (for policies).
42
+ #
43
+ # By default, calls +current_user+ if the controller responds to it.
44
+ #
45
+ # @return [Object]
46
+ def operation_user
47
+ current_user if respond_to?(:current_user)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+ module Pragma
3
+ module Rails
4
+ # Exposes CRUD operations on a resource through a Rails controller.
5
+ #
6
+ # @author Alessandro Desantis
7
+ module ResourceController
8
+ def self.included(klass)
9
+ klass.include Controller
10
+ klass.extend ClassMethods
11
+ klass.include InstanceMethods
12
+ end
13
+
14
+ module ClassMethods # :nodoc:
15
+ # Returns the expected class of the provided operation on this resource.
16
+ #
17
+ # Note that this does not mean the operation is actually supported. Use {#operation?} for
18
+ # that.
19
+ #
20
+ # @param operation_name [Symbol] name of the operation
21
+ #
22
+ # @return [String]
23
+ #
24
+ # @see #operation?
25
+ #
26
+ # @example
27
+ # API::V1::PostsController.operation_klass(:create) => 'API::V1::Post::Operation::Create'
28
+ def operation_klass(operation_name)
29
+ [name.deconstantize].tap do |klass|
30
+ klass << name.demodulize.chomp('Controller').singularize
31
+ klass << "Operation::#{operation_name.to_s.camelize}"
32
+ end.join('::')
33
+ end
34
+
35
+ # Returns whether the provided operation is supported on this resource.
36
+ #
37
+ # @param operation_name [Symbol] name of the operation
38
+ #
39
+ # @return [Boolean]
40
+ def operation?(operation_name)
41
+ class_exists? operation_klass(operation_name)
42
+ end
43
+
44
+ private
45
+
46
+ def class_exists?(klass)
47
+ Object.const_get(klass)
48
+ true
49
+ rescue NameError
50
+ false
51
+ end
52
+ end
53
+
54
+ module InstanceMethods # :nodoc:
55
+ # If an action that does not exist is called on this controller, tries to find and run
56
+ # an operation on the current resource with the same name.
57
+ #
58
+ # For instance, +API::V1::PostsController#create+ would try to find and run
59
+ # +API::V1::Post::Operation::Create+.
60
+ #
61
+ # @param action_name [String] name of the missing action
62
+ def action_missing(action_name)
63
+ send(action_name)
64
+ end
65
+
66
+ # Supports running missing actions.
67
+ #
68
+ # @see #action_missing
69
+ def method_missing(method_name, *arguments, &block)
70
+ return super unless self.class.operation?(method_name)
71
+ run self.class.operation_klass(method_name)
72
+ end
73
+
74
+ # Supports running missing actions.
75
+ #
76
+ # @see #action_missing
77
+ def respond_to_missing?(method_name, include_private = false)
78
+ self.class.operation?(method_name) || super
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module Pragma
3
+ module Rails
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pragma/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pragma-rails'
8
+ spec.version = Pragma::Rails::VERSION
9
+ spec.authors = ['Alessandro Desantis']
10
+ spec.email = ['desa.alessandro@gmail.com']
11
+
12
+ spec.summary = 'Ruby on Rails integration for the Pragma architecture.'
13
+ spec.homepage = 'https://github.com/pragmarb/pragma-rails'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'pragma', '~> 0.1.0'
24
+ spec.add_dependency 'rails', '>= 4.2.0', '< 6'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'rubocop'
30
+ spec.add_development_dependency 'rubocop-rspec'
31
+ spec.add_development_dependency 'coveralls'
32
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pragma-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Desantis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pragma
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.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.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '6'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 4.2.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '6'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
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
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-rspec
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: coveralls
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ description:
132
+ email:
133
+ - desa.alessandro@gmail.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".gitignore"
139
+ - ".rspec"
140
+ - ".rubocop.yml"
141
+ - ".travis.yml"
142
+ - Gemfile
143
+ - LICENSE.txt
144
+ - README.md
145
+ - Rakefile
146
+ - bin/console
147
+ - bin/setup
148
+ - lib/pragma/rails.rb
149
+ - lib/pragma/rails/controller.rb
150
+ - lib/pragma/rails/resource_controller.rb
151
+ - lib/pragma/rails/version.rb
152
+ - pragma-rails.gemspec
153
+ homepage: https://github.com/pragmarb/pragma-rails
154
+ licenses:
155
+ - MIT
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 2.6.8
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Ruby on Rails integration for the Pragma architecture.
177
+ test_files: []