contextualizer 0.0.3 → 0.0.4
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/lib/contextualizer.rb +11 -5
- data/lib/contextualizer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 213d3ab93e2c302ffa373e24042fecc4aa483586
|
|
4
|
+
data.tar.gz: 962895326c717c818af6e99e1c91ba9e5eeffda8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7d5aaa12c07248aec59c58e01785030e3789ce2793d85a4043685e16e79023e1ddc7b33946723d68419bb42fdfc566cd5ae3e5d13a20a7f9c70f34472c8ecd15
|
|
7
|
+
data.tar.gz: 15964bd4677634957c413ff8cb5364db752e49853a00d6e5af8e062bd7de7e4da627822cae12168d635457927e891fa4d8c18c762349175a371dc51bbb426d4f
|
data/lib/contextualizer.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
require 'contextualizer/version'
|
|
2
2
|
|
|
3
3
|
module Contextualizer
|
|
4
|
+
OPTIONAL = Object.new.freeze
|
|
5
|
+
|
|
4
6
|
def context(*attrs, **opt_attrs)
|
|
5
7
|
unless @__setter
|
|
6
8
|
@__setter = Setter.new
|
|
@@ -37,19 +39,23 @@ module Contextualizer
|
|
|
37
39
|
|
|
38
40
|
class Setter
|
|
39
41
|
def initialize
|
|
40
|
-
@
|
|
42
|
+
@mandatory, @with_default, @optional = [], {}, []
|
|
41
43
|
end
|
|
42
44
|
|
|
43
45
|
def add_attrs(*attrs, **opt_attrs)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
optional, with_default = opt_attrs.partition { |_, v| v == OPTIONAL }.map(&:to_h)
|
|
47
|
+
|
|
48
|
+
@mandatory |= attrs
|
|
49
|
+
@optional |= optional.keys
|
|
50
|
+
@with_default.merge!(with_default)
|
|
46
51
|
end
|
|
47
52
|
|
|
48
53
|
def set(obj, args)
|
|
49
54
|
context = obj.context&.dup || {}
|
|
50
55
|
|
|
51
|
-
@
|
|
52
|
-
@
|
|
56
|
+
@with_default.each { |key, default| context[key] = args.fetch(key, default) }
|
|
57
|
+
@optional.each { |key| context[key] = args[key] if args.key?(key) }
|
|
58
|
+
@mandatory.each do |key|
|
|
53
59
|
fail ":#{key} was not found in scope" unless args.key?(key)
|
|
54
60
|
context[key] = args[key]
|
|
55
61
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: contextualizer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Pablo Herrero
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2018-05-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|