grape_has_scope 0.1.0
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.
- checksums.yaml +7 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +14 -0
- data/README.rdoc +3 -0
- data/Rakefile +2 -0
- data/lib/grape_has_scope.rb +150 -0
- metadata +53 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 5781a062b32b540e375568c51c45fe38c92729df
|
|
4
|
+
data.tar.gz: 4e02b3a418cd8d02d5a7417071eef84bb94af337
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4529bd0a950ce35b9e3087fa84f76674384c17d620bc708c7669478149a4a955381944f5bb347cfce821499ea77af6e661bf0cbff819434c6ea3e7361effc74d
|
|
7
|
+
data.tar.gz: f0b49285d0bbb10daae6eef30689519572bd124d2b1ceee6f014708d9b18417bf92ab0160e6f0eae16caf92469d66d597ceb21340e5d836fe8de4b21a93fac83
|
data/Gemfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
source "http://rubygems.org"
|
|
2
|
+
|
|
3
|
+
# Declare your gem's dependencies in dalli-delete-matched.gemspec.
|
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
|
5
|
+
# development dependencies will be added by default to the :development group.
|
|
6
|
+
gemspec
|
|
7
|
+
|
|
8
|
+
# jquery-rails is used by the dummy application
|
|
9
|
+
# gem "jquery-rails"
|
|
10
|
+
|
|
11
|
+
# Declare any dependencies that are still in development here instead of in
|
|
12
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
|
13
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
|
14
|
+
# your gem to rubygems.org.
|
|
15
|
+
|
|
16
|
+
# To use debugger
|
|
17
|
+
# gem 'debugger'
|
data/Gemfile.lock
ADDED
data/README.rdoc
ADDED
data/Rakefile
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
module GrapeHasScope
|
|
2
|
+
TRUE_VALUES = ["true", true, "1", 1]
|
|
3
|
+
|
|
4
|
+
ALLOWED_TYPES = {
|
|
5
|
+
:array => [ Array ],
|
|
6
|
+
:hash => [ Hash ],
|
|
7
|
+
:boolean => [ Object ],
|
|
8
|
+
:default => [ String, Numeric ]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
def self.included(base)
|
|
12
|
+
base.class_eval do
|
|
13
|
+
class_attribute :scopes_configuration
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def has_scope(*scopes, &block)
|
|
18
|
+
options = scopes.extract_options!
|
|
19
|
+
options.symbolize_keys!
|
|
20
|
+
options.assert_valid_keys(:type, :only, :except, :if, :unless, :default, :as, :using, :allow_blank)
|
|
21
|
+
|
|
22
|
+
if options.key?(:using)
|
|
23
|
+
if options.key?(:type) && options[:type] != :hash
|
|
24
|
+
raise "You cannot use :using with another :type different than :hash"
|
|
25
|
+
else
|
|
26
|
+
options[:type] = :hash
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
options[:using] = Array(options[:using])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
options[:only] = Array(options[:only])
|
|
33
|
+
options[:except] = Array(options[:except])
|
|
34
|
+
|
|
35
|
+
self.class.scopes_configuration = (self.class.scopes_configuration || {}).dup
|
|
36
|
+
|
|
37
|
+
scopes.each do |scope|
|
|
38
|
+
self.class.scopes_configuration[scope] ||= { :as => scope, :type => :default, :block => block }
|
|
39
|
+
self.class.scopes_configuration[scope] = self.class.scopes_configuration[scope].merge(options)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Receives an object where scopes will be applied to.
|
|
44
|
+
#
|
|
45
|
+
# class GraduationsController < InheritedResources::Base
|
|
46
|
+
# has_scope :featured, :type => true, :only => :index
|
|
47
|
+
# has_scope :by_degree, :only => :index
|
|
48
|
+
#
|
|
49
|
+
# def index
|
|
50
|
+
# @graduations = apply_scopes(Graduation).all
|
|
51
|
+
# end
|
|
52
|
+
# end
|
|
53
|
+
#
|
|
54
|
+
def apply_scopes(target, hash=params)
|
|
55
|
+
return target unless scopes_configuration
|
|
56
|
+
|
|
57
|
+
self.class.scopes_configuration.each do |scope, options|
|
|
58
|
+
next unless apply_scope_to_action?(options)
|
|
59
|
+
key = options[:as]
|
|
60
|
+
|
|
61
|
+
if hash.key?(key)
|
|
62
|
+
value, call_scope = hash[key], true
|
|
63
|
+
elsif options.key?(:default)
|
|
64
|
+
value, call_scope = options[:default], true
|
|
65
|
+
value = value.call(self) if value.is_a?(Proc)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
value = parse_value(options[:type], key, value)
|
|
69
|
+
value = normalize_blanks(value)
|
|
70
|
+
|
|
71
|
+
if call_scope && (value.present? || options[:allow_blank])
|
|
72
|
+
current_scopes[key] = value
|
|
73
|
+
target = call_scope_by_type(options[:type], scope, target, value, options)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
target
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Set the real value for the current scope if type check.
|
|
81
|
+
def parse_value(type, key, value) #:nodoc:
|
|
82
|
+
if type == :boolean
|
|
83
|
+
TRUE_VALUES.include?(value)
|
|
84
|
+
elsif value && ALLOWED_TYPES[type].any?{ |klass| value.is_a?(klass) }
|
|
85
|
+
value
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Screens pseudo-blank params.
|
|
90
|
+
def normalize_blanks(value) #:nodoc:
|
|
91
|
+
return value if value.nil?
|
|
92
|
+
if value.is_a?(Array)
|
|
93
|
+
value.select { |v| v.present? }
|
|
94
|
+
elsif value.is_a?(Hash)
|
|
95
|
+
value.select { |k, v| normalize_blanks(v).present? }.with_indifferent_access
|
|
96
|
+
else
|
|
97
|
+
value
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Call the scope taking into account its type.
|
|
102
|
+
def call_scope_by_type(type, scope, target, value, options) #:nodoc:
|
|
103
|
+
block = options[:block]
|
|
104
|
+
|
|
105
|
+
if type == :boolean
|
|
106
|
+
block ? block.call(self, target) : target.send(scope)
|
|
107
|
+
elsif value && options.key?(:using)
|
|
108
|
+
value = value.values_at(*options[:using])
|
|
109
|
+
block ? block.call(self, target, value) : target.send(scope, *value)
|
|
110
|
+
else
|
|
111
|
+
block ? block.call(self, target, value) : target.send(scope, value)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Given an options with :only and :except arrays, check if the scope
|
|
116
|
+
# can be performed in the current action.
|
|
117
|
+
def apply_scope_to_action?(options) #:nodoc:
|
|
118
|
+
return false unless applicable?(options[:if], true) && applicable?(options[:unless], false)
|
|
119
|
+
|
|
120
|
+
if options[:only].empty?
|
|
121
|
+
options[:except].empty? || !options[:except].include?(action_name.to_sym)
|
|
122
|
+
else
|
|
123
|
+
options[:only].include?(action_name.to_sym)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Evaluates the scope options :if or :unless. Returns true if the proc
|
|
128
|
+
# method, or string evals to the expected value.
|
|
129
|
+
def applicable?(string_proc_or_symbol, expected) #:nodoc:
|
|
130
|
+
case string_proc_or_symbol
|
|
131
|
+
when String
|
|
132
|
+
eval(string_proc_or_symbol) == expected
|
|
133
|
+
when Proc
|
|
134
|
+
string_proc_or_symbol.call(self) == expected
|
|
135
|
+
when Symbol
|
|
136
|
+
send(string_proc_or_symbol) == expected
|
|
137
|
+
else
|
|
138
|
+
true
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Returns the scopes used in this action.
|
|
143
|
+
def current_scopes
|
|
144
|
+
@current_scopes ||= {}
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
class Grape::Endpoint
|
|
149
|
+
include GrapeHasScope
|
|
150
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: grape_has_scope
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kourza Ivan a.k.a. Phobos98
|
|
8
|
+
autorequire: builder
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-01-09 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: has_scope implementation to use with Grape API
|
|
14
|
+
email: phobos98@phobos98.net
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files:
|
|
18
|
+
- Gemfile
|
|
19
|
+
- Rakefile
|
|
20
|
+
- README.rdoc
|
|
21
|
+
- Gemfile.lock
|
|
22
|
+
files:
|
|
23
|
+
- lib/grape_has_scope.rb
|
|
24
|
+
- Gemfile
|
|
25
|
+
- Rakefile
|
|
26
|
+
- README.rdoc
|
|
27
|
+
- Gemfile.lock
|
|
28
|
+
homepage: https://github.com/Phobos98/grape_has_scope.git
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata: {}
|
|
32
|
+
post_install_message:
|
|
33
|
+
rdoc_options: []
|
|
34
|
+
require_paths:
|
|
35
|
+
- lib
|
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 1.3.6
|
|
46
|
+
requirements: []
|
|
47
|
+
rubyforge_project: grape_has_scope
|
|
48
|
+
rubygems_version: 2.0.3
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: has_scope implementation to use with Grape API
|
|
52
|
+
test_files: []
|
|
53
|
+
has_rdoc: true
|