hash_mapper 0.2.7 → 0.2.8

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
2
  SHA256:
3
- metadata.gz: e16c3b3b73cba0fc400759652542dfe19c0f5516f04c0e7ae83ddd07c46eb549
4
- data.tar.gz: cf6abbd34bd06c262ecff7db0333052e99064cdf74d6d46afd83328d4b46c008
3
+ metadata.gz: 48c1f85ef574a126509499c46c0cd4022022acf58b74c262bc680eb6e9a472ce
4
+ data.tar.gz: 37eeb0e69111e1e0b0da2f3fe463adca1202e7dd5310715564f9c955dadbb56c
5
5
  SHA512:
6
- metadata.gz: cad09d8c2835a211c74e70d4556415942467dcf7c333f8e0703948b1febb786fd6a5cb54e2e211c459e41c6a3807aacffb734bafec7bc1b54c93c6c89b4c0057
7
- data.tar.gz: 7d22e3ef4ed97d6d6d4af226e78f07b1ab8e5ed7b6be63d9145a52e32c4f2551a2becc08399bca65bded5bbb7b7013e9fd218776b60dd2b9323aa0d25e69012f
6
+ metadata.gz: 4203160fe7ed6480638532b928ad6477b981b23ecb471d9a442e4ff764b8485079b3a1a4b85cab15e67da8ed97f3d5deadc894666939b2d369ed4d9443647940
7
+ data.tar.gz: c31c08907811d20e6b240505b54579364b8c9c8d55607bde2d482ba75dd729fcb44593aa2c64930089c00c87bcfcbb92d04c08b487d41b5afa950581b316c912
@@ -0,0 +1,19 @@
1
+ name: Tests
2
+
3
+ on: push
4
+
5
+ jobs:
6
+ rspec:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby:
11
+ - '3.2.2'
12
+ name: Ruby ${{ matrix.ruby }} RSpec
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: ruby/setup-ruby@v1
16
+ with:
17
+ bundler-cache: true
18
+ ruby-version: ${{ matrix.ruby }}
19
+ - run: bundle exec rspec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [ ![Codeship Status for ismasan/hash_mapper](https://www.codeship.io/projects/85d172c0-4668-0132-e925-7a7d3d72b19b/status)](https://www.codeship.io/projects/45296)
1
+ [![GitHub Actions status](https://github.com/ismasan/hash_mapper/workflows/Tests/badge.svg?123)](https://github.com/ismasan/hash_mapper)
2
2
 
3
3
  # hash_mapper
4
4
 
@@ -306,6 +306,24 @@ EggMapper.normalize({}, options: { no_default: true })
306
306
  EggMapper.denormalize({fried: 4})
307
307
  ```
308
308
 
309
+ #### Importing mappings from another mapper
310
+
311
+ Use `.import_mapper(another_mapper)` to import mappings from another mapper. This is useful when you want to reuse mappings from another mapper.
312
+
313
+ ```ruby
314
+ class Mapper1
315
+ extend HashMapper
316
+ map from('/name'), to('/name')
317
+ end
318
+
319
+ class Mapper2
320
+ extend HashMapper
321
+ import_mapper Mapper1
322
+ map from('/age'), to('/age')
323
+ end
324
+
325
+ # Mapper2 will have mappings from Mapper1 ('/name' => '/name') and its own mappings ('/age' => '/age')
326
+ ```
309
327
 
310
328
  ## REQUIREMENTS:
311
329
 
@@ -1,3 +1,3 @@
1
1
  module HashMapper
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
data/lib/hash_mapper.rb CHANGED
@@ -71,6 +71,10 @@ module HashMapper
71
71
  path_map
72
72
  end
73
73
 
74
+ def import_mapper(mapper)
75
+ self.maps = self.maps + mapper.maps.dup
76
+ end
77
+
74
78
  alias :to :from
75
79
 
76
80
  def using(mapper_class)
@@ -156,7 +156,7 @@ end
156
156
 
157
157
  class PersonWithBlock
158
158
  extend HashMapper
159
- def self.normalize(*_)
159
+ def self.normalize(...)
160
160
  super
161
161
  end
162
162
  map from('/names/first'){|n| n.gsub('+','')}, to('/first_name'){|n| "+++#{n}+++"}
@@ -396,7 +396,7 @@ describe "inherited mappers" do
396
396
  end
397
397
 
398
398
  it "should not affect other mappers" do
399
- expect(NotRelated.normalize('n' => 'nn')).to eq({n: {n: 'nn'}})
399
+ expect(NotRelated.normalize({'n' => 'nn'})).to eq({n: {n: 'nn'}})
400
400
  end
401
401
  end
402
402
 
@@ -409,17 +409,17 @@ end
409
409
  describe "dealing with strings and symbols" do
410
410
 
411
411
  it "should be able to normalize from a nested hash with string keys" do
412
- expect(MixedMappings.normalize(
412
+ expect(MixedMappings.normalize({
413
413
  'big' => {'jobs' => 5},
414
414
  'timble' => 3.2
415
- )).to eq({dodo: 5, bingo: {biscuit: 3.2}})
415
+ })).to eq({dodo: 5, bingo: {biscuit: 3.2}})
416
416
  end
417
417
 
418
418
  it "should not symbolized keys in value hashes" do
419
- expect(MixedMappings.normalize(
419
+ expect(MixedMappings.normalize({
420
420
  'big' => {'jobs' => 5},
421
421
  'timble' => {'string key' => 'value'}
422
- )).to eq({dodo: 5, bingo: {biscuit: {'string key' => 'value'}}})
422
+ })).to eq({dodo: 5, bingo: {biscuit: {'string key' => 'value'}}})
423
423
  end
424
424
 
425
425
  end
@@ -433,9 +433,9 @@ end
433
433
 
434
434
  describe "default values" do
435
435
  it "should use a default value whenever a key is not set" do
436
- expect(DefaultValues.normalize(
436
+ expect(DefaultValues.normalize({
437
437
  'without_default' => 'some_value'
438
- )).to eq({ not_defaulted: 'some_value', defaulted: 'the_default_value' })
438
+ })).to eq({ not_defaulted: 'some_value', defaulted: 'the_default_value' })
439
439
  end
440
440
 
441
441
  it "should not use a default if a key is set (even if the value is falsy)" do
@@ -446,6 +446,25 @@ describe "default values" do
446
446
  end
447
447
  end
448
448
 
449
+ describe '.import_mapper(another_mapper)' do
450
+ let(:mapper) do
451
+ Class.new do
452
+ extend HashMapper
453
+
454
+ import_mapper DefaultValues
455
+ map from('/name'), to('/nombre')
456
+ end
457
+ end
458
+
459
+ it 'imports the mappings from another mapper' do
460
+ expect(mapper.maps.size).to eq(3)
461
+ expect(mapper.normalize({
462
+ 'without_default' => 'some_value',
463
+ 'name' => 'ismael'
464
+ })).to eq({ not_defaulted: 'some_value', defaulted: 'the_default_value', nombre: 'ismael' })
465
+ end
466
+ end
467
+
449
468
  class MultiBeforeFilter
450
469
  extend HashMapper
451
470
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-23 00:00:00.000000000 Z
11
+ date: 2024-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -59,6 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".github/workflows/tests.yml"
62
63
  - ".gitignore"
63
64
  - Gemfile
64
65
  - History.txt
@@ -88,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
89
  - !ruby/object:Gem::Version
89
90
  version: '0'
90
91
  requirements: []
91
- rubygems_version: 3.0.3
92
+ rubygems_version: 3.4.22
92
93
  signing_key:
93
94
  specification_version: 4
94
95
  summary: Maps input hashes to a normalized format