hash_map 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ef09a5a9361c24f1699f79e236d3d0b08da994c3
4
- data.tar.gz: eae0a632e2c1ba49782a5f22925bf86461e4e491
2
+ SHA256:
3
+ metadata.gz: dc971280a4ffad89cb9b4f93132e673eae4412558bb5bde8e3f3748873c67f7f
4
+ data.tar.gz: '060278780641e8248af95dcd7cacd7d222f7a4bdc2f5b5f522b030c35a0433fa'
5
5
  SHA512:
6
- metadata.gz: 808a0077718557db480351584190c2745f68dfb781f108bae18bf1e60c408f5722de02f63ed3fddd2b068391056d7b6a9baf2c330b48864ce3f01ce110801a12
7
- data.tar.gz: f1dba0212065c7d2abd947c3ff78589dbe7255fdce9028abac8c62376bb84695092714887ad5e38cc35a43d23113284ee18db26f203bb4cbe7225b4cae8a0a78
6
+ metadata.gz: 44801fbd00a2c195e8583b977f8145d2552df02a361aab725d4677d7e5e4bbdfb551daba17d501fadb5995f354d89a05100d95002c78aa344746b2a05df2da0f
7
+ data.tar.gz: 3a4850f7da85c482f3d912913367b6c22389496fb11364c788402a00c97dfe6c96429a78fd84fc573bbb555fe0e7ead0666d6228551f03a2ad2f3f91b8a941f0
@@ -8,6 +8,8 @@ rvm:
8
8
  - 2.1.6
9
9
  - 2.2
10
10
  - 2.3.0
11
+ - 2.4.4
12
+ - 2.5.1
11
13
  - ruby-head
12
14
  - jruby-19mode
13
15
  - jruby-9.0.1.0
@@ -18,6 +20,10 @@ env:
18
20
  - JRUBY_OPTS="--debug"
19
21
  matrix:
20
22
  allow_failures:
23
+ - rvm: 1.9.3
24
+ - rvm: 2.0.0
25
+ - rvm: 2.1
26
+ - rvm: 2.1.6
21
27
  - rvm: ruby-head
22
28
  - rvm: rbx-3
23
29
  - rvm: jruby-19mode
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :development, :test do
7
+ gem 'rails', '~> 5.2'
7
8
  gem 'simplecov', require: false
8
9
  gem 'rubocop', '~> 0.37.2', require: false unless RUBY_VERSION =~ /^1.8/
9
10
  gem 'coveralls', require: false
data/README.md CHANGED
@@ -365,6 +365,26 @@ AfterEach.call(booleans)
365
365
  #=> {"name"=>true, "age"=>false}
366
366
  ```
367
367
 
368
+ ### `only_provided_keys`
369
+
370
+ ```ruby
371
+ class RegularMapper < HashMap::Base
372
+ properties :name, :lastname, :phone
373
+ from_child :address do
374
+ to_child :address do
375
+ properties :street, :number
376
+ end
377
+ end
378
+ end
379
+ class OnlyProvidedKeysMapper < RegularMapper
380
+ only_provided_keys
381
+ end
382
+
383
+ input = { name: "john", address: {street: "Batu Mejan" }, phone: nil }
384
+ RegularMapper.call(input) # => {"name"=>"john", "lastname"=>nil, "phone"=>nil, "address"=>{"street"=>"Batu Mejan", "number"=>nil}}
385
+ OnlyProvidedKeysMapper.call(input) # => {"name"=>"john", phone: nil, "address"=>{"street"=>"Batu Mejan"}}
386
+ ```
387
+
368
388
  ### JSON Adapter
369
389
  ```ruby
370
390
  class UserMapper < HashMap::Base
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.10"
23
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "rspec"
25
- spec.add_dependency "fusu", "~> 0.2"
25
+ spec.add_dependency "fusu", "~> 0.2.1"
26
26
  end
@@ -4,8 +4,33 @@ module HashMap
4
4
  def self.root
5
5
  File.expand_path '../..', __FILE__
6
6
  end
7
+
8
+ class KeyNotProvided
9
+ def to_s
10
+ 'nil'
11
+ end
12
+ def to_json
13
+ nil
14
+ end
15
+ def as_json(arg = nil)
16
+ nil
17
+ end
18
+ end
19
+
20
+ def self.deep_reject(hash, &block)
21
+ hash.each_with_object(Fusu::HashWithIndifferentAccess.new) do |(k, v), memo|
22
+ unless block.call(k, v)
23
+ if v.is_a?(Hash)
24
+ memo[k] = deep_reject(v, &block)
25
+ else
26
+ memo[k] = v
27
+ end
28
+ end
29
+ end
30
+ end
7
31
  end
8
32
  require 'hash_map/dsl'
33
+ require 'hash_map/after_each_context'
9
34
  require 'hash_map/mapper'
10
35
  require 'hash_map/base'
11
36
  require 'hash_map/json_adapter'
@@ -0,0 +1,31 @@
1
+ module HashMap
2
+ class AfterEachContext
3
+ attr_reader :original, :struct, :value
4
+ def initialize(original, struct, value)
5
+ @original = original
6
+ @struct = struct
7
+ @value = value
8
+ end
9
+
10
+ def provided?
11
+ has_key?
12
+ end
13
+
14
+ def block?
15
+ !!struct[:proc]
16
+ end
17
+
18
+ def has_key?
19
+ return true if block?
20
+ found = true
21
+ struct[:from].reduce(original) do |prv, nxt|
22
+ unless prv.respond_to?(:key?) && prv.key?(nxt)
23
+ found = false
24
+ break
25
+ end
26
+ prv.send(:[], nxt)
27
+ end
28
+ found
29
+ end
30
+ end
31
+ end
@@ -10,7 +10,7 @@ module HashMap
10
10
  subclass.extend ToDSL
11
11
  return unless self < HashMap::Base
12
12
  unless dsl.attributes.empty?
13
- subclass._set_attributes_from_inheritance(attributes)
13
+ subclass._set_attributes_from_inheritance(attributes.deep_dup)
14
14
  end
15
15
  end
16
16
 
@@ -17,7 +17,7 @@ module HashMap
17
17
  end
18
18
 
19
19
  def _set_attributes_from_inheritance(attrs)
20
- dsl._set_attributes(attrs.dup)
20
+ dsl._set_attributes(attrs.deep_dup)
21
21
  end
22
22
  end
23
23
 
@@ -28,23 +28,28 @@ module HashMap
28
28
 
29
29
  def initialize
30
30
  @attributes = []
31
+ @after_each = Set.new
32
+ @transform_output = Set.new
33
+ @transform_input = Set.new
31
34
  end
32
35
 
33
36
  def after_each(*middlewares)
34
- @after_each ||= []
35
37
  @after_each += middlewares
36
38
  end
37
39
 
38
40
  def transforms_output(*middlewares)
39
- @transform_output ||= []
40
41
  @transform_output += middlewares
41
42
  end
42
43
 
43
44
  def transforms_input(*middlewares)
44
- @transform_input ||= []
45
45
  @transform_input += middlewares
46
46
  end
47
47
 
48
+ def only_provided_keys
49
+ @transform_output << RemoveUnprovideds
50
+ @after_each << MarkUnprovided
51
+ end
52
+
48
53
  def _set_attributes(attrs)
49
54
  @attributes = attrs
50
55
  end
@@ -28,9 +28,10 @@ module HashMap
28
28
  nil_to_default(value, struct)
29
29
  end
30
30
 
31
- def after_each_middleware(value, _key)
31
+ def after_each_middleware(value, struct)
32
+ contx = AfterEachContext.new(original, struct, value)
32
33
  after_each_callbacks.inject(value) do |output, middle|
33
- middle.call(output)
34
+ middle.call(output, contx)
34
35
  end
35
36
  end
36
37
 
@@ -51,7 +52,7 @@ module HashMap
51
52
  break unless output.respond_to?(:[])
52
53
  output.send(:[], k)
53
54
  end
54
- after_each_middleware(value, struct[:key].last)
55
+ after_each_middleware(value, struct)
55
56
  end
56
57
 
57
58
  def execute_block(struct)
@@ -62,7 +63,7 @@ module HashMap
62
63
  else
63
64
  hash_map.instance_exec original, original, &block
64
65
  end
65
- after_each_middleware(value, struct[:key].last)
66
+ after_each_middleware(value, struct)
66
67
  end
67
68
 
68
69
  def build_keys(ary, value)
@@ -40,6 +40,10 @@ module HashMap
40
40
  self
41
41
  end
42
42
 
43
+ def failure_message_when_negated
44
+ "expect to not #{description}"
45
+ end
46
+
43
47
  private
44
48
 
45
49
  def mapped_has_key?
@@ -109,7 +113,7 @@ module HashMap
109
113
 
110
114
  def equality
111
115
  return true unless expected_provided
112
- if mapped_value == expected
116
+ if matches_equality?(mapped_value, expected)
113
117
  description_messages << "and eq `#{expected}`"
114
118
  else
115
119
  failure_messages << "key #{key_to_message} expected to eq `#{expected}`"
@@ -117,6 +121,14 @@ module HashMap
117
121
  end
118
122
  end
119
123
 
124
+ def matches_equality?(value, expected)
125
+ if expected.is_a?(Proc)
126
+ expected.call(value)
127
+ else
128
+ value == expected
129
+ end
130
+ end
131
+
120
132
  def _nested_keys(hash, keys)
121
133
  keys.inject({ value: hash, has_key: true }) do |out, key|
122
134
  return { value: nil, has_key: false } unless out[:value]
@@ -1,14 +1,26 @@
1
1
  module HashMap
2
- BlankToNil = lambda do |v|
2
+ BlankToNil = lambda do |v, ctx|
3
3
  Fusu.blank?(v) ? nil : v
4
4
  end
5
5
 
6
- StringToBoolean = lambda do |v|
6
+ StringToBoolean = lambda do |v, ctx|
7
7
  return false if v == 'false'
8
8
  return true if v == 'true'
9
9
  v
10
10
  end
11
11
 
12
+ MarkUnprovided = lambda do |v, ctx|
13
+ if !ctx.provided?
14
+ KeyNotProvided.new
15
+ else
16
+ v
17
+ end
18
+ end
19
+
20
+ RemoveUnprovideds = lambda do |o|
21
+ HashMap.deep_reject(o){ |k, v| v.is_a?(HashMap::KeyNotProvided) }
22
+ end
23
+
12
24
  UnderscoreKeys = lambda do |output|
13
25
  Fusu::Hash.deep_transform_keys(output){ |k| Fusu::String.underscore(k).to_sym }
14
26
  end
@@ -1,3 +1,3 @@
1
1
  module HashMap
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Pañach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-12 00:00:00.000000000 Z
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.2'
61
+ version: 0.2.1
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.2'
68
+ version: 0.2.1
69
69
  description: Nice DSL to convert hash structure to different one.
70
70
  email:
71
71
  - arturictus@gmail.com
@@ -88,6 +88,7 @@ files:
88
88
  - bin/setup
89
89
  - hash_map.gemspec
90
90
  - lib/hash_map.rb
91
+ - lib/hash_map/after_each_context.rb
91
92
  - lib/hash_map/base.rb
92
93
  - lib/hash_map/dsl.rb
93
94
  - lib/hash_map/json_adapter.rb
@@ -115,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  version: '0'
116
117
  requirements: []
117
118
  rubyforge_project:
118
- rubygems_version: 2.2.3
119
+ rubygems_version: 2.7.6
119
120
  signing_key:
120
121
  specification_version: 4
121
122
  summary: Library to map easily hashes.