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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 656da6e97d5a4805a96b5aae76332fd738c9727a
4
- data.tar.gz: 0ec330dd1f6919269230eda301084792ac83be30
3
+ metadata.gz: 213d3ab93e2c302ffa373e24042fecc4aa483586
4
+ data.tar.gz: 962895326c717c818af6e99e1c91ba9e5eeffda8
5
5
  SHA512:
6
- metadata.gz: 70d5adca95a69de4c41a852ccc290920008ab7fc7b58d95675039b7394adadb447f7ea15df842a2e4653b415c3c643d5d1e5976dcce7a3048e257d63445f1fd8
7
- data.tar.gz: d83fac648dadc104b6c898dbdbf61c0d39f251a6bf73194eb6d3b7a41ec0947c1c6df7cd499d40957055f3eaceea3e0ca1dc9b196ebbbbb637e10aa0e5d26b33
6
+ metadata.gz: 7d5aaa12c07248aec59c58e01785030e3789ce2793d85a4043685e16e79023e1ddc7b33946723d68419bb42fdfc566cd5ae3e5d13a20a7f9c70f34472c8ecd15
7
+ data.tar.gz: 15964bd4677634957c413ff8cb5364db752e49853a00d6e5af8e062bd7de7e4da627822cae12168d635457927e891fa4d8c18c762349175a371dc51bbb426d4f
@@ -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
- @attrs, @opt_attrs = [], {}
42
+ @mandatory, @with_default, @optional = [], {}, []
41
43
  end
42
44
 
43
45
  def add_attrs(*attrs, **opt_attrs)
44
- @attrs |= attrs
45
- @opt_attrs.merge!(opt_attrs)
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
- @opt_attrs.each { |key, default| context[key] = args.key?(key) ? args[key] : default }
52
- @attrs.each do |key|
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
@@ -1,3 +1,3 @@
1
1
  module Contextualizer
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  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.3
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: 2017-06-30 00:00:00.000000000 Z
11
+ date: 2018-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler