arcane 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation --tty
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.9.2
6
+ - jruby-19mode
7
+ - rbx-19mode
8
+ - ruby-head
9
+ - jruby-head
10
+
11
+ env:
12
+ - "strong_parameters=0.2.0"
13
+ - "strong_parameters=0.2.1"
14
+ - "activesupport=3.2.13"
15
+ - "activesupport=4.0.0"
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec # Specify your gem's dependencies in arcane.gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Philip Vieira
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.
@@ -0,0 +1,32 @@
1
+ # Arcane
2
+ Easy to use parameter filter extending strong parameters.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'arcane'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ Or install it yourself as:
19
+
20
+ ```bash
21
+ $ gem install arcane
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rspec/core/rake_task'
3
+ require "bundler/gem_tasks"
4
+
5
+ desc "Run all examples"
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ #t.rspec_path = 'bin/rspec'
8
+ t.rspec_opts = %w[--color]
9
+ end
10
+
11
+ task :default => [:spec]
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arcane/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+
8
+ gem.name = "arcane"
9
+ gem.version = Arcane::VERSION
10
+ gem.authors = ["Philip Vieira", "Cloudsdale"]
11
+ gem.email = ["philip@vallin.se"]
12
+
13
+ gem.description = "Parameter filter done OO, extending strong parameters."
14
+ gem.summary = "Extension for strong_parameters."
15
+ gem.homepage = "https://github.com/cloudsdaleapp/arcane"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency "activesupport", ">= 3.0.0"
23
+ gem.add_dependency "strong_parameters", ">= 0.2.0"
24
+
25
+ gem.add_development_dependency "activerecord", ">= 3.0.0"
26
+ gem.add_development_dependency "rspec", "~>2.0"
27
+ gem.add_development_dependency "pry"
28
+ gem.add_development_dependency "rake"
29
+
30
+ end
@@ -0,0 +1,28 @@
1
+ require "arcane/version"
2
+ require "arcane/finder"
3
+ require "arcane/chain"
4
+ require "arcane/refinery"
5
+
6
+ require 'active_support/concern'
7
+ require 'active_support/core_ext/string/inflections'
8
+ require 'active_support/core_ext/object/blank'
9
+ require 'strong_parameters'
10
+
11
+ module Arcane
12
+
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ if respond_to?(:helper_method)
17
+ helper_method :arcane
18
+ end
19
+ if respond_to?(:hide_action)
20
+ hide_action :arcane
21
+ end
22
+ end
23
+
24
+ def refine(object,*args)
25
+ Arcane::Chain.new(params,object,current_user)
26
+ end
27
+
28
+ end
@@ -0,0 +1,43 @@
1
+ module Arcane
2
+
3
+ class Chain
4
+
5
+ attr_reader :_params, :_user, :_object, :_arcane, :_arcane_class
6
+
7
+ def initialize(_params,_object,_user)
8
+ @_user = _user
9
+ @_object = _object
10
+ @_params = ActionController::Parameters.new(_params)
11
+ @_arcane_class = Arcane::Finder.new(_object).arcane
12
+ @_arcane = @_arcane_class.new(_object,_user)
13
+ end
14
+
15
+ def method_missing(_arcane_method,*args)
16
+
17
+ if _arcane.respond_to?(_arcane_method)
18
+ _computed = _arcane.public_send(_arcane_method)
19
+ elsif _arcane.respond_to?(:default)
20
+ _computed = _arcane.default
21
+ else
22
+ _computed = []
23
+ end
24
+
25
+ if _arcane.respond_to?(:root)
26
+ _root = arcane.root
27
+ elsif _arcane_class.respond_to?(:root)
28
+ _root = _arcane_class.root
29
+ else
30
+ _root = Arcane::Finder.object_name(_object)
31
+ end
32
+
33
+ if _root.present?
34
+ _params.require(_root.parameterize).permit(*_computed)
35
+ else
36
+ _params.permit(*_computed)
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,47 @@
1
+ module Arcane
2
+
3
+ class Finder
4
+
5
+ attr_reader :object
6
+
7
+ def initialize(object)
8
+ @object = object
9
+ end
10
+
11
+ def arcane
12
+ klass = find
13
+ klass = klass.constantize if klass.is_a?(String)
14
+ klass
15
+ rescue NameError
16
+ Arcane::Refinery
17
+ end
18
+
19
+ def self.object_name(object)
20
+ klass = if object.respond_to?(:model_name)
21
+ object.model_name
22
+ elsif object.class.respond_to?(:model_name)
23
+ object.class.model_name
24
+ elsif object.is_a?(Class)
25
+ object
26
+ else
27
+ object.class
28
+ end
29
+ klass.to_s
30
+ end
31
+
32
+ private
33
+
34
+ def find
35
+ if object.respond_to?(:arcane_class)
36
+ object.arcane_class
37
+ elsif object.class.respond_to?(:arcane_class)
38
+ object.class.arcane_class
39
+ else
40
+ klass = self.class.object_name(object)
41
+ "#{klass}Refinery"
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,18 @@
1
+ module Arcane
2
+
3
+ class Refinery
4
+
5
+ attr_reader :object, :user
6
+
7
+ def initialize(object,user=nil)
8
+ @object = object
9
+ @user = user
10
+ end
11
+
12
+ def default
13
+ []
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,3 @@
1
+ module Arcane
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Arcane::Chain do
4
+
5
+ let(:chain) { Arcane::Chain.new(params,object,user) }
6
+
7
+ let(:user) { double }
8
+ let(:object) { Article.new }
9
+ let(:arcane_class) { ArticleRefinery }
10
+ let(:params) { ActionController::Parameters.new(hash_params) }
11
+ let(:hash_params) do
12
+ {
13
+ article: {
14
+ title: "hello",
15
+ content: "world",
16
+ links: [
17
+ { blog: "http://blog.example.com" },
18
+ { site: "http://www.example.com" },
19
+ ]
20
+ }
21
+ }
22
+ end
23
+
24
+ describe '.new' do
25
+
26
+ it 'sets attributes' do
27
+ chain._params.should eq(params)
28
+ chain._object.should eq(object)
29
+ chain._user.should eq(user)
30
+ chain._arcane.should be_a(arcane_class)
31
+ chain._arcane_class.should eq(arcane_class)
32
+ end
33
+
34
+ it 'converts the object to ActionController::Parameters' do
35
+ chain = Arcane::Chain.new(hash_params,object,user)
36
+ chain._params.should be_a ActionController::Parameters
37
+ end
38
+
39
+ end
40
+
41
+ describe '#method_missing' do
42
+
43
+ it 'returns the filtered parameters' do
44
+ chain.void_method.should be_a ActionController::Parameters
45
+ end
46
+
47
+ it 'filters the same as the default constraint' do
48
+ chain.void_method.should eq chain.default
49
+ end
50
+
51
+ context 'without root in params' do
52
+ let(:chain) { Arcane::Chain.new(params[:article],object,user) }
53
+ it 'raises an error' do
54
+ expect { chain.void_method }.to raise_error ActionController::ParameterMissing
55
+ end
56
+ it 'successds with nil root in arcane' do
57
+ arcane_class.stub(:root) { false }
58
+ expect { chain.void_method }.to_not raise_error
59
+ arcane_class.stub(:root) { nil }
60
+ expect { chain.void_method }.to_not raise_error
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Arcane::Finder do
4
+
5
+ let(:object_with_arcane) { Task.new }
6
+ let(:object_without_arcane) { Shape.new }
7
+
8
+ before(:each) { @arcane = Arcane::Finder.new(object_with_arcane) }
9
+
10
+ describe '.new' do
11
+ it 'sets the object' do
12
+ @arcane.object.should eq(object_with_arcane)
13
+ end
14
+ end
15
+
16
+ describe '#find' do
17
+
18
+ it 'finds existing arcane' do
19
+ @arcane.send(:find).should eq "TaskRefinery"
20
+ end
21
+
22
+ end
23
+
24
+ describe '#arcane' do
25
+
26
+ it 'returns arcane' do
27
+ @arcane.arcane.should eq TaskRefinery
28
+ end
29
+
30
+ it 'returns base arcane class when no arcane exists' do
31
+ @arcane = Arcane::Finder.new(object_without_arcane)
32
+ @arcane.arcane.should eq Arcane::Refinery
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Arcane::Refinery do
4
+
5
+ let(:user) { double }
6
+ let(:object) { Article.new }
7
+
8
+ before(:each) do
9
+ @arcane = Arcane::Refinery.new(object,user)
10
+ end
11
+
12
+ describe '.new' do
13
+
14
+ it 'sets attributes' do
15
+ @arcane.object.should eq(object)
16
+ @arcane.user.should eq(user)
17
+ end
18
+
19
+ it 'does not set user if no user present' do
20
+ @arcane = Arcane::Refinery.new(object)
21
+ @arcane.user.should be_nil
22
+ end
23
+
24
+ end
25
+
26
+ describe '#default' do
27
+
28
+ it { @arcane.default.should be_empty }
29
+
30
+ it 'can be overridden when inherited' do
31
+ @arcane = ArticleRefinery.new(object,user)
32
+ @arcane.default.should eq [:title]
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Arcane do
4
+
5
+ let(:user) { double }
6
+ let(:article) { Article.new }
7
+ let(:controller) { double(:current_user => user, :params => params).tap { |c| c.extend(Arcane) } }
8
+ let(:params) do
9
+ HashWithIndifferentAccess.new({
10
+ action: 'create',
11
+ article: {
12
+ title: "hello",
13
+ content: "world",
14
+ links: [
15
+ { blog: "http://blog.example.com" },
16
+ { site: "http://www.example.com" },
17
+ ],
18
+ tags: ["test","greeting"]
19
+ }
20
+ })
21
+ end
22
+
23
+ describe '#refine' do
24
+
25
+ it { controller.refine(article).should be_a Arcane::Chain }
26
+ it { controller.refine(article).update.should be_a ActionController::Parameters }
27
+
28
+ it 'filters parameters correctly' do
29
+ controller.refine(article).update.should eq expected_params(:title,:content)
30
+ end
31
+
32
+ it 'filters nested object parameters correctly' do
33
+ controller.refine(article).create.should eq expected_params(:title,:content,:links)
34
+ end
35
+
36
+ it 'filters nested scalar parameters correctly' do
37
+ controller.refine(article).publish.should eq expected_params(:title,:content,:tags)
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ def expected_params(*includes)
45
+ params[:article].reject { |k,_| !includes.include? k.to_sym }
46
+ end
@@ -0,0 +1,36 @@
1
+ require "arcane"
2
+ require "pry"
3
+ require "active_support/core_ext"
4
+ require "active_model/naming"
5
+
6
+ class Article; end
7
+ class ArticleRefinery < Arcane::Refinery
8
+
9
+ def create
10
+ update + [ { links: [:blog, :site] } ]
11
+ end
12
+
13
+ def publish
14
+ update + [ { tags: [] } ]
15
+ end
16
+
17
+ def update
18
+ default + [:content]
19
+ end
20
+
21
+ def default
22
+ [:title]
23
+ end
24
+
25
+ end
26
+
27
+ class Task; end
28
+ class TaskRefinery < Arcane::Refinery
29
+
30
+ def self.root
31
+ false
32
+ end
33
+
34
+ end
35
+
36
+ class Shape; end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arcane
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philip Vieira
9
+ - Cloudsdale
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-07-11 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 3.0.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: strong_parameters
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 0.2.0
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 0.2.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: activerecord
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 3.0.0
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: '2.0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: '2.0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: pry
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rake
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Parameter filter done OO, extending strong parameters.
112
+ email:
113
+ - philip@vallin.se
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - .travis.yml
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - arcane.gemspec
126
+ - lib/arcane.rb
127
+ - lib/arcane/chain.rb
128
+ - lib/arcane/finder.rb
129
+ - lib/arcane/refinery.rb
130
+ - lib/arcane/version.rb
131
+ - spec/arcane/chain_spec.rb
132
+ - spec/arcane/finder_spec.rb
133
+ - spec/arcane/refinery_spec.rb
134
+ - spec/arcane_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: https://github.com/cloudsdaleapp/arcane
137
+ licenses: []
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ segments:
149
+ - 0
150
+ hash: -3172912698727836139
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ segments:
158
+ - 0
159
+ hash: -3172912698727836139
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 1.8.25
163
+ signing_key:
164
+ specification_version: 3
165
+ summary: Extension for strong_parameters.
166
+ test_files:
167
+ - spec/arcane/chain_spec.rb
168
+ - spec/arcane/finder_spec.rb
169
+ - spec/arcane/refinery_spec.rb
170
+ - spec/arcane_spec.rb
171
+ - spec/spec_helper.rb