adequate_exposure 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +18 -0
- data/Appraisals +20 -0
- data/Gemfile +3 -2
- data/Rakefile +3 -0
- data/adequate_exposure.gemspec +2 -2
- data/gemfiles/rails_4.0.gemfile +12 -0
- data/gemfiles/rails_4.1.gemfile +12 -0
- data/gemfiles/rails_4.2.gemfile +12 -0
- data/gemfiles/rails_5.0.gemfile +12 -0
- data/lib/adequate_exposure/attribute.rb +22 -0
- data/lib/adequate_exposure/behavior.rb +42 -1
- data/lib/adequate_exposure/context.rb +19 -0
- data/lib/adequate_exposure/controller.rb +27 -0
- data/lib/adequate_exposure/exposure.rb +33 -0
- data/lib/adequate_exposure/flow.rb +17 -0
- data/lib/adequate_exposure/version.rb +1 -1
- data/spec/support/rails_app.rb +1 -1
- metadata +24 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b45371ec2f76686959ecea9daafa48bfdcbff365
|
4
|
+
data.tar.gz: 7b9d2f20e7b8817d26aa546cfa540f40cc9bd9e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b5a55b1ed6f423981c3a8763da33f5b753f463ef1f95e0c3057f01b72fc77b1a5cd0b9b8c501c28c8ab9279b77df637bf9ecaf6e01676b18e8716063cf4d9b6
|
7
|
+
data.tar.gz: 94fc24e721c06a468fec3a88371b625d7ea7325b7f4c3ea3892bea709b92098ffb8432754a07f9c39f5fcad97610cfce0760976b45e06ccc2db8a5607ee7a99d
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
before_install:
|
2
|
+
- gem install bundler
|
3
|
+
|
4
|
+
gemfile:
|
5
|
+
- gemfiles/rails_4.0.gemfile
|
6
|
+
- gemfiles/rails_4.1.gemfile
|
7
|
+
- gemfiles/rails_4.2.gemfile
|
8
|
+
- gemfiles/rails_5.0.gemfile
|
9
|
+
|
1
10
|
rvm:
|
2
11
|
- 2.0
|
3
12
|
- 2.1
|
13
|
+
- 2.2
|
14
|
+
- 2.3
|
15
|
+
|
16
|
+
matrix:
|
17
|
+
exclude:
|
18
|
+
- rvm: 2.0
|
19
|
+
gemfile: gemfiles/rails_5.0.gemfile
|
20
|
+
- rvm: 2.1
|
21
|
+
gemfile: gemfiles/rails_5.0.gemfile
|
data/Appraisals
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
appraise "rails-5.0" do
|
2
|
+
gem "railties", "~> 5.0.0"
|
3
|
+
gem "activesupport", "~> 5.0.0"
|
4
|
+
end
|
5
|
+
|
6
|
+
appraise "rails-4.2" do
|
7
|
+
gem "railties", "~> 4.2.0"
|
8
|
+
gem "activesupport", "~> 4.2.0"
|
9
|
+
end
|
10
|
+
|
11
|
+
appraise "rails-4.1" do
|
12
|
+
gem "railties", "~> 4.1.0"
|
13
|
+
gem "activesupport", "~> 4.1.0"
|
14
|
+
end
|
15
|
+
|
16
|
+
appraise "rails-4.0" do
|
17
|
+
gem "railties", "~> 4.0.0"
|
18
|
+
gem "activesupport", "~> 4.0.0"
|
19
|
+
end
|
20
|
+
|
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/adequate_exposure.gemspec
CHANGED
@@ -14,6 +14,6 @@ Gem::Specification.new do |spec|
|
|
14
14
|
|
15
15
|
spec.required_ruby_version = "~> 2.0"
|
16
16
|
|
17
|
-
spec.add_dependency "railties", "
|
18
|
-
spec.add_dependency "activesupport", "
|
17
|
+
spec.add_dependency "railties", ">= 4.0", "< 6"
|
18
|
+
spec.add_dependency "activesupport", ">= 4.0", "< 6"
|
19
19
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", "~> 2.1"
|
6
|
+
gem "pry", "~> 0.10"
|
7
|
+
gem "rake", "~> 10.3"
|
8
|
+
gem "rspec-rails", "~> 3.4"
|
9
|
+
gem "railties", "~> 4.0.0"
|
10
|
+
gem "activesupport", "~> 4.0.0"
|
11
|
+
|
12
|
+
gemspec :path => "../"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", "~> 2.1"
|
6
|
+
gem "pry", "~> 0.10"
|
7
|
+
gem "rake", "~> 10.3"
|
8
|
+
gem "rspec-rails", "~> 3.4"
|
9
|
+
gem "railties", "~> 4.1.0"
|
10
|
+
gem "activesupport", "~> 4.1.0"
|
11
|
+
|
12
|
+
gemspec :path => "../"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", "~> 2.1"
|
6
|
+
gem "pry", "~> 0.10"
|
7
|
+
gem "rake", "~> 10.3"
|
8
|
+
gem "rspec-rails", "~> 3.4"
|
9
|
+
gem "railties", "~> 4.2.0"
|
10
|
+
gem "activesupport", "~> 4.2.0"
|
11
|
+
|
12
|
+
gemspec :path => "../"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal", "~> 2.1"
|
6
|
+
gem "pry", "~> 0.10"
|
7
|
+
gem "rake", "~> 10.3"
|
8
|
+
gem "rspec-rails", "~> 3.4"
|
9
|
+
gem "railties", "~> 5.0.0"
|
10
|
+
gem "activesupport", "~> 5.0.0"
|
11
|
+
|
12
|
+
gemspec :path => "../"
|
@@ -2,20 +2,42 @@ module AdequateExposure
|
|
2
2
|
class Attribute
|
3
3
|
attr_reader :name, :fetch, :ivar_name
|
4
4
|
|
5
|
+
# Public: Initialize an Attribute
|
6
|
+
#
|
7
|
+
# options - Hash of options for the Attribute
|
8
|
+
# :name - The String name of the Attribute instance
|
9
|
+
# :fetch - The Proc fetch to calculate
|
10
|
+
# the value of the Attribute instance.
|
11
|
+
# This is only called if the attribute's
|
12
|
+
# instance variable is not defined.
|
13
|
+
# :ivar_name - The String instance variable name that
|
14
|
+
# is associated with the attribute.
|
5
15
|
def initialize(options)
|
6
16
|
@name = options.fetch(:name)
|
7
17
|
@fetch = options.fetch(:fetch)
|
8
18
|
@ivar_name = options.fetch(:ivar_name)
|
9
19
|
end
|
10
20
|
|
21
|
+
# Public: The getter method for the Attribute.
|
22
|
+
#
|
23
|
+
# Returns the name of the Attribute as a Symbol.
|
11
24
|
def getter_method_name
|
12
25
|
name.to_sym
|
13
26
|
end
|
14
27
|
|
28
|
+
# Public: The setter method for the Attribute.
|
29
|
+
#
|
30
|
+
# Returns the name of the attribute as a Symbol with an appended '='.
|
15
31
|
def setter_method_name
|
16
32
|
"#{name}=".to_sym
|
17
33
|
end
|
18
34
|
|
35
|
+
|
36
|
+
# Public: Expose a getter and setter method for the Attribute
|
37
|
+
# on the passed in Controller class.
|
38
|
+
#
|
39
|
+
# klass - The Controller class where the Attribute getter and setter
|
40
|
+
# methods will be exposed.
|
19
41
|
def expose!(klass)
|
20
42
|
attribute = self
|
21
43
|
|
@@ -1,10 +1,20 @@
|
|
1
1
|
module AdequateExposure
|
2
2
|
module Behavior
|
3
|
+
# Public: Fetches a scope.
|
4
|
+
#
|
5
|
+
# Finds an object. If it isn't found, the object gets instantiated.
|
6
|
+
#
|
7
|
+
# Returns the decorated object.
|
3
8
|
def fetch
|
4
9
|
instance = id ? find(id, computed_scope) : build(build_params, computed_scope)
|
5
10
|
decorate(instance)
|
6
11
|
end
|
7
12
|
|
13
|
+
# Public: Checks a params hash for an id attribute.
|
14
|
+
#
|
15
|
+
# Checks a hash of parameters for keys that represent an object's id.
|
16
|
+
#
|
17
|
+
# Returns the value of the id parameter, if it exists. Otherwise nil.
|
8
18
|
def id
|
9
19
|
params_id_key_candidates.each do |key|
|
10
20
|
value = params[key]
|
@@ -14,26 +24,57 @@ module AdequateExposure
|
|
14
24
|
nil
|
15
25
|
end
|
16
26
|
|
27
|
+
# Public: An object query. Essentially, this method is designed to be
|
28
|
+
# overridden.
|
29
|
+
#
|
30
|
+
# model - The Class to be scoped or queried.
|
31
|
+
#
|
32
|
+
# Returns the object scope.
|
17
33
|
def scope(model)
|
18
34
|
model
|
19
35
|
end
|
20
36
|
|
37
|
+
# Public: Converts a name into a standard Class name.
|
38
|
+
#
|
39
|
+
# Examples
|
40
|
+
# 'egg_and_hams'.model # => EggAndHam
|
41
|
+
#
|
42
|
+
# Returns a standard Class name.
|
21
43
|
def model
|
22
44
|
name.to_s.classify.constantize
|
23
45
|
end
|
24
46
|
|
47
|
+
# Public: Find an object on the supplied scope.
|
48
|
+
#
|
49
|
+
# id - The Integer id attribute of the desired object
|
50
|
+
# scope - The collection that will be searched.
|
51
|
+
#
|
52
|
+
# Returns the found object.
|
25
53
|
def find(id, scope)
|
26
54
|
scope.find(id)
|
27
55
|
end
|
28
56
|
|
57
|
+
# Public: Builds a new object on the passed-in scope.
|
58
|
+
#
|
59
|
+
# params - A Hash of attributes for the object to-be built.
|
60
|
+
# scope - The collection that will be searched.
|
61
|
+
#
|
62
|
+
# Returns the new object.
|
29
63
|
def build(params, scope)
|
30
64
|
scope.new(params)
|
31
65
|
end
|
32
66
|
|
67
|
+
# Public: Returns a decorated object. This method is designed to be
|
68
|
+
# overridden.
|
69
|
+
#
|
70
|
+
# Returns the decorated object.
|
33
71
|
def decorate(instance)
|
34
72
|
instance
|
35
73
|
end
|
36
74
|
|
75
|
+
# Public: Get all the parameters of the current request.
|
76
|
+
#
|
77
|
+
# Returns the controller's parameters for the current request.
|
37
78
|
def build_params
|
38
79
|
if controller.respond_to?(params_method_name, true) && !get_request?
|
39
80
|
controller.send(params_method_name)
|
@@ -45,7 +86,7 @@ module AdequateExposure
|
|
45
86
|
protected
|
46
87
|
|
47
88
|
def params_id_key_candidates
|
48
|
-
[ "#{
|
89
|
+
[ "#{model_param_key}_id", "#{name}_id", "id" ].uniq
|
49
90
|
end
|
50
91
|
|
51
92
|
def model_param_key
|
@@ -2,14 +2,33 @@ module AdequateExposure
|
|
2
2
|
class Context
|
3
3
|
attr_reader :context, :attribute
|
4
4
|
|
5
|
+
# Public: Initialize a context.
|
6
|
+
#
|
7
|
+
# context - The Class where the attribute is defined.
|
8
|
+
# attribute - The attribute that will be accessed by a getter
|
9
|
+
# and setter.
|
5
10
|
def initialize(context, attribute)
|
6
11
|
@context, @attribute = context, attribute
|
7
12
|
end
|
8
13
|
|
14
|
+
# Public: Read an attribute on the context Class.
|
15
|
+
#
|
16
|
+
# Get an attribute's value. If the attribute's instance
|
17
|
+
# variable is not defined, it will create one,
|
18
|
+
# execute attribute#fetch, and assign the result
|
19
|
+
# to the instance variable.
|
20
|
+
#
|
21
|
+
# Returns the attribute's value.
|
9
22
|
def get
|
10
23
|
ivar_defined?? ivar_get : set(fetch_value)
|
11
24
|
end
|
12
25
|
|
26
|
+
# Public: Write to an attribute on the context Class.
|
27
|
+
#
|
28
|
+
# value - The value that will be set to the attribute's
|
29
|
+
# instance variable.
|
30
|
+
#
|
31
|
+
# Returns the attribute's value.
|
13
32
|
def set(value)
|
14
33
|
ivar_set(value)
|
15
34
|
end
|
@@ -8,15 +8,42 @@ module AdequateExposure
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module ClassMethods
|
11
|
+
# Public: Exposes an attribute to a controller Class.
|
12
|
+
#
|
13
|
+
# *args - An Array of attributes for the new exposure. See
|
14
|
+
# Exposure#initialize for attribute details.
|
15
|
+
# block - If supplied, the exposed attribute method executes
|
16
|
+
# the Proc when accessed.
|
17
|
+
#
|
18
|
+
# Returns the helper methods that are now defined on the class
|
19
|
+
# where this method is included.
|
11
20
|
def expose(*args, &block)
|
12
21
|
Exposure.expose! self, *args, &block
|
13
22
|
end
|
14
23
|
|
24
|
+
# Public: Exposes an attribute to a controller Class.
|
25
|
+
# The exposed methods are then set to a before_action
|
26
|
+
# callback.
|
27
|
+
#
|
28
|
+
# name - The String name of the Exposure instance.
|
29
|
+
# *args - An Array of attributes for the new exposure. See
|
30
|
+
# Exposure#initialize for attribute details.
|
31
|
+
# block - If supplied, the exposed attribute method executes
|
32
|
+
# the Proc when accessed.
|
33
|
+
#
|
34
|
+
# Sets the exposed attribute to a before_action callback in the
|
35
|
+
# controller.
|
15
36
|
def expose!(name, *args, &block)
|
16
37
|
expose name, *args, &block
|
17
38
|
before_action name
|
18
39
|
end
|
19
40
|
|
41
|
+
# Public: Configures an Exposure instance for a controller Class.
|
42
|
+
#
|
43
|
+
# name - The String name of the Exposure instance.
|
44
|
+
# options - The Hash of options to configure the Exposure instance.
|
45
|
+
#
|
46
|
+
# Returns the exposure configuration Hash.
|
20
47
|
def exposure_config(name, options)
|
21
48
|
store = self.exposure_configuration ||= {}
|
22
49
|
self.exposure_configuration = store.merge(name => options)
|
@@ -2,10 +2,38 @@ module AdequateExposure
|
|
2
2
|
class Exposure
|
3
3
|
attr_reader :controller, :options
|
4
4
|
|
5
|
+
# Public: Initializes an Exposure and makes it accessible to a controller.
|
6
|
+
# For each Exposure, a getter and setter is defined.
|
7
|
+
# Those getters and setters are made available to
|
8
|
+
# the controller as helper methods.
|
9
|
+
#
|
10
|
+
# *args - An Array of all parameters for the new Exposure. See
|
11
|
+
# #initialize.
|
12
|
+
# block - If supplied, the exposed attribute method executes
|
13
|
+
# the Proc when called.
|
14
|
+
#
|
15
|
+
# Returns a collection of exposed helper methods.
|
5
16
|
def self.expose!(*args, &block)
|
6
17
|
new(*args, &block).expose!
|
7
18
|
end
|
8
19
|
|
20
|
+
# Public: Initalize an Exposure with a hash of options.
|
21
|
+
#
|
22
|
+
# If a block is given, the Proc is assigned to value
|
23
|
+
# of options[name].
|
24
|
+
#
|
25
|
+
# The `asserts_*` section raise errors if the controller
|
26
|
+
# was initialized with an unacceptable options Hash.
|
27
|
+
#
|
28
|
+
# controller - The Controller class where methods will be exposed.
|
29
|
+
# name - The String name of the Exposure instance.
|
30
|
+
# fetch_block - Proc that will be executed if the exposed
|
31
|
+
# attribute has no value (default: nil).
|
32
|
+
# options - Hash of options for the Behavior of the exposed methods.
|
33
|
+
# block - If supplied, the exposed attribute method executes
|
34
|
+
# the Proc.
|
35
|
+
#
|
36
|
+
# Returns a normalized options Hash.
|
9
37
|
def initialize(controller, name, fetch_block=nil, **options, &block)
|
10
38
|
@controller = controller
|
11
39
|
@options = options.with_indifferent_access.merge(name: name)
|
@@ -22,6 +50,11 @@ module AdequateExposure
|
|
22
50
|
normalize_options
|
23
51
|
end
|
24
52
|
|
53
|
+
# Public: Creates a getter and setter methods for the attribute.
|
54
|
+
# Those methods are made avaiable to the controller as
|
55
|
+
# helper methods.
|
56
|
+
#
|
57
|
+
# Returns a collection of exposed helper methods.
|
25
58
|
def expose!
|
26
59
|
expose_attribute!
|
27
60
|
expose_helper_methods!
|
@@ -2,12 +2,24 @@ module AdequateExposure
|
|
2
2
|
class Flow
|
3
3
|
attr_reader :controller, :options, :name
|
4
4
|
|
5
|
+
# Public: Initialize a Flow. This object responds to missing
|
6
|
+
# methods errors and attempts to delegate them to other objects.
|
7
|
+
#
|
8
|
+
# controller - The Controller class where the method was called.
|
9
|
+
# options - The options Hash of the Exposure instance being called.
|
10
|
+
# name - The String name of the Exposure instance.
|
5
11
|
def initialize(controller, options)
|
6
12
|
@controller = controller
|
7
13
|
@options = options
|
8
14
|
@name = options.fetch(:name)
|
9
15
|
end
|
10
16
|
|
17
|
+
# Public: Attempts to re-delegate a method missing to the
|
18
|
+
# supplied block or the Behavior object.
|
19
|
+
#
|
20
|
+
# name - The String name of the Exposure instance.
|
21
|
+
# *args - The arguments given for the missing method.
|
22
|
+
# block - The Proc invoked by the method.
|
11
23
|
def method_missing(name, *args, &block)
|
12
24
|
if respond_to_missing?(name)
|
13
25
|
handle_flow_method(name, *args, &block)
|
@@ -16,6 +28,11 @@ module AdequateExposure
|
|
16
28
|
end
|
17
29
|
end
|
18
30
|
|
31
|
+
# Public: Checks if the Behavior class can handle the missing method.
|
32
|
+
#
|
33
|
+
# method_name - The name of method that has been called.
|
34
|
+
# include_private - Prevents this method from catching calls to private
|
35
|
+
# method (default: false).
|
19
36
|
def respond_to_missing?(method_name, include_private = false)
|
20
37
|
Behavior.method_defined?(method_name) || super
|
21
38
|
end
|
data/spec/support/rails_app.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adequate_exposure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Pravosud
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: activesupport
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
|
-
- - "
|
37
|
+
- - ">="
|
32
38
|
- !ruby/object:Gem::Version
|
33
39
|
version: '4.0'
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '6'
|
34
43
|
type: :runtime
|
35
44
|
prerelease: false
|
36
45
|
version_requirements: !ruby/object:Gem::Requirement
|
37
46
|
requirements:
|
38
|
-
- - "
|
47
|
+
- - ">="
|
39
48
|
- !ruby/object:Gem::Version
|
40
49
|
version: '4.0'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '6'
|
41
53
|
description:
|
42
54
|
email:
|
43
55
|
- pavel@pravosud.com
|
@@ -47,11 +59,16 @@ extra_rdoc_files: []
|
|
47
59
|
files:
|
48
60
|
- ".gitignore"
|
49
61
|
- ".travis.yml"
|
62
|
+
- Appraisals
|
50
63
|
- Gemfile
|
51
64
|
- LICENSE.txt
|
52
65
|
- README.md
|
53
66
|
- Rakefile
|
54
67
|
- adequate_exposure.gemspec
|
68
|
+
- gemfiles/rails_4.0.gemfile
|
69
|
+
- gemfiles/rails_4.1.gemfile
|
70
|
+
- gemfiles/rails_4.2.gemfile
|
71
|
+
- gemfiles/rails_5.0.gemfile
|
55
72
|
- lib/adequate_exposure.rb
|
56
73
|
- lib/adequate_exposure/attribute.rb
|
57
74
|
- lib/adequate_exposure/behavior.rb
|
@@ -84,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
101
|
version: '0'
|
85
102
|
requirements: []
|
86
103
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.
|
104
|
+
rubygems_version: 2.6.4
|
88
105
|
signing_key:
|
89
106
|
specification_version: 4
|
90
107
|
summary: Exposing things, adequately
|