scopie 1.0.0 → 1.0.1
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 +4 -4
- data/README.md +1 -1
- data/lib/scopie/base.rb +4 -34
- data/lib/scopie/value.rb +69 -0
- data/lib/scopie/version.rb +1 -1
- data/lib/scopie.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe59078e2e2798ce9fb21bfbaf5d84073832fec1
|
4
|
+
data.tar.gz: 04be530e19f3441fb2b4255558c5c5d5ce98628b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d33e2a61b45ff6a8d14384a70d3e0a4cbf48b4236ed172c21b0bcf4d3ec261f628984d49fafe5802931bddb3242939057126e6b8c6f0a1299bdc94b5d5da0a76
|
7
|
+
data.tar.gz: b0ebe558815608b0b9f581f61a3b00ea0be147e16bf8848c30b85503573c16596534c4aeec10c4745f5aaa6b61ecd2d55e43ae91af131a47e7317022f0782893
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ Motivation:
|
|
15
15
|
* Dedicated class for scopes mapping, so that the logic is isolated and your controller is skinny.
|
16
16
|
* Dependencies free. Please have a look at [scopie_rails](http://github.com/beorc/scopie_rails) if you are using Ruby on Rails framework.
|
17
17
|
* Ability to override default mapping behavior by definition of a method with the same name as scope in the scopie class.
|
18
|
-
* Ability to DRY your custom scopes mapping logic
|
18
|
+
* Ability to use the object oriented approach to DRY your custom scopes mapping logic and reuse the scopie class.
|
19
19
|
|
20
20
|
Imagine the following model called graduations:
|
21
21
|
|
data/lib/scopie/base.rb
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
class Scopie::Base
|
4
4
|
|
5
|
-
TRUE_VALUES = ['true', true, '1', 1].freeze
|
6
|
-
|
7
5
|
def self.scopes_configuration
|
8
6
|
instance_variable_get(:@scopes_configuration) || {}
|
9
7
|
end
|
@@ -33,7 +31,7 @@ class Scopie::Base
|
|
33
31
|
value = scope_value(scope_name, options, hash)
|
34
32
|
next unless scope_applicable?(value, options, method)
|
35
33
|
|
36
|
-
[scope_name,
|
34
|
+
[scope_name, value.coerced]
|
37
35
|
end
|
38
36
|
|
39
37
|
scopes.compact!
|
@@ -62,42 +60,14 @@ class Scopie::Base
|
|
62
60
|
key_name = key_name(scope_name, options)
|
63
61
|
reduced_hash = reduced_hash(hash, options)
|
64
62
|
|
65
|
-
|
66
|
-
options[:default]
|
67
|
-
end
|
68
|
-
|
69
|
-
def coerce_value_type(value, type)
|
70
|
-
return value unless type
|
71
|
-
|
72
|
-
coercion_method_name = "coerce_to_#{type}"
|
73
|
-
|
74
|
-
respond_to?(coercion_method_name, true) || fail(Scopie::InvalidOptionError.new("Unknown value for option 'type' provided: :#{type}"))
|
75
|
-
|
76
|
-
send(coercion_method_name, value)
|
77
|
-
end
|
78
|
-
|
79
|
-
def coerce_to_boolean(value)
|
80
|
-
TRUE_VALUES.include? value
|
81
|
-
end
|
82
|
-
|
83
|
-
def coerce_to_integer(value)
|
84
|
-
Integer(value)
|
85
|
-
end
|
86
|
-
|
87
|
-
def coerce_to_date(value)
|
88
|
-
Date.parse(value)
|
89
|
-
end
|
90
|
-
|
91
|
-
def coerce_to_float(value)
|
92
|
-
Float(value)
|
63
|
+
Scopie::Value.new(reduced_hash, key_name, options)
|
93
64
|
end
|
94
65
|
|
95
66
|
def scope_applicable?(value, options, method)
|
96
67
|
return false unless method_applicable?(method, options)
|
68
|
+
return false unless value.given?
|
97
69
|
|
98
|
-
|
99
|
-
|
100
|
-
is_value_present || !!options[:allow_blank]
|
70
|
+
value.present? || !!options[:allow_blank]
|
101
71
|
end
|
102
72
|
|
103
73
|
def reduced_hash(hash, options)
|
data/lib/scopie/value.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Scopie::Value < Struct.new(:hash, :key_name, :options)
|
4
|
+
|
5
|
+
TRUE_VALUES = ['true', true, '1', 1].freeze
|
6
|
+
|
7
|
+
def raw
|
8
|
+
return hash[key_name] if hash.key?(key_name)
|
9
|
+
fetch_default
|
10
|
+
end
|
11
|
+
|
12
|
+
def coerced
|
13
|
+
coerce_to_type(raw, fetch_type)
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch_type
|
17
|
+
options[:type]
|
18
|
+
end
|
19
|
+
|
20
|
+
def fetch_default
|
21
|
+
options[:default]
|
22
|
+
end
|
23
|
+
|
24
|
+
def has_default?
|
25
|
+
options.key?(:default)
|
26
|
+
end
|
27
|
+
|
28
|
+
def given?
|
29
|
+
key_passed? || has_default?
|
30
|
+
end
|
31
|
+
|
32
|
+
def key_passed?
|
33
|
+
hash.key?(key_name)
|
34
|
+
end
|
35
|
+
|
36
|
+
def present?
|
37
|
+
value = raw
|
38
|
+
value.respond_to?(:empty?) ? !value.empty? : !!value
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def coerce_to_type(value, type)
|
44
|
+
return value unless type
|
45
|
+
|
46
|
+
coercion_method_name = "coerce_to_#{type}"
|
47
|
+
|
48
|
+
respond_to?(coercion_method_name, true) || fail(Scopie::InvalidOptionError.new("Unknown value for option 'type' provided: :#{type}"))
|
49
|
+
|
50
|
+
send(coercion_method_name, value)
|
51
|
+
end
|
52
|
+
|
53
|
+
def coerce_to_boolean(value)
|
54
|
+
TRUE_VALUES.include? value
|
55
|
+
end
|
56
|
+
|
57
|
+
def coerce_to_integer(value)
|
58
|
+
Integer(value)
|
59
|
+
end
|
60
|
+
|
61
|
+
def coerce_to_date(value)
|
62
|
+
Date.parse(value)
|
63
|
+
end
|
64
|
+
|
65
|
+
def coerce_to_float(value)
|
66
|
+
Float(value)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/lib/scopie/version.rb
CHANGED
data/lib/scopie.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Kotov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Minimal mapping of incoming parameters to named scopes in your resources
|
14
14
|
through OO design and pure Ruby classes
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- README.md
|
23
23
|
- lib/scopie.rb
|
24
24
|
- lib/scopie/base.rb
|
25
|
+
- lib/scopie/value.rb
|
25
26
|
- lib/scopie/version.rb
|
26
27
|
homepage: https://github.com/beorc/scopie
|
27
28
|
licenses:
|