yahm 0.0.2 → 0.0.3
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 +49 -2
- data/lib/yahm/mapping.rb +11 -1
- data/lib/yahm/version.rb +1 -1
- data/spec/yahm/mapping_spec.rb +15 -7
- 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: fd289b056663d262256eb39d461f7ca8d0cb7daf
|
4
|
+
data.tar.gz: d41bebcad80b96d86c320982e355f01e7125a610
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66dfa4baa5bf7bfced7383c222a971284f9d68d9018e5513e658104846bd75a78d2921c065fcc1a0180b251d2e0408a04e2a5b08f2b529e2d40349c0d4b5ca79
|
7
|
+
data.tar.gz: fb49353dc85d0cb7570539496de97427d2fb1695889e27c6491d5a04b49bbc38aab74cc798d95d12e2b9610dcbd540a25a138b56b0bbf27511c61795e6a83dbb
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Yahm
|
2
2
|
|
3
|
-
|
3
|
+
Yahm is a hash to hash translator for ruby.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -16,9 +16,56 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
$ gem install yahm
|
18
18
|
|
19
|
+
## Dependencies
|
20
|
+
|
21
|
+
None
|
22
|
+
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
|
25
|
+
|
26
|
+
### Most basic example
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require "yahm"
|
30
|
+
|
31
|
+
class Record
|
32
|
+
extend Yahm::HashMapper
|
33
|
+
|
34
|
+
define_mapper :from_other_record do
|
35
|
+
map "/record/id", to: "/id"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Record.new.from_other_record({ ... })
|
40
|
+
=> { :id => "..." }
|
41
|
+
```
|
42
|
+
|
43
|
+
### More advanced example
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require "yahm"
|
47
|
+
|
48
|
+
class Record
|
49
|
+
extend Yahm::HashMapper
|
50
|
+
attr_accessor :translated_hash
|
51
|
+
|
52
|
+
define_mapper :from_other_record, call_setter: :translated_hash= do
|
53
|
+
map "/record/id", to: "/id"
|
54
|
+
map "/record/count", to: "/count", processed_by: :to_i
|
55
|
+
map "/record/subject[0]", to: "/subjects/most_important_subject"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
Record.new.from_other_record({ ... })
|
60
|
+
=> { :id => "...", :count => ..., :subjects => { :most_important_subject => "..."} }
|
61
|
+
|
62
|
+
Record.translated_hash
|
63
|
+
=> { :id => "...", :count => ..., :subjects => { :most_important_subject => "..."} }
|
64
|
+
```
|
65
|
+
|
66
|
+
## Related work
|
67
|
+
|
68
|
+
* hash_mapper (https://github.com/ismasan/hash_mapper)
|
22
69
|
|
23
70
|
## Contributing
|
24
71
|
|
data/lib/yahm/mapping.rb
CHANGED
@@ -48,7 +48,17 @@ class Yahm::Mapping
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
+
unless (split_by_character = rule.last[:split_by]).nil?
|
52
|
+
source_value = source_value.split(split_by_character).map(&:strip)
|
53
|
+
end
|
54
|
+
|
55
|
+
unless rule.last[:force_array].nil?
|
56
|
+
source_value = source_value.is_a?(Array) ? source_value : [source_value]
|
57
|
+
end
|
58
|
+
|
51
59
|
target_hash_parent_element = target_parent_path.inject(@translated_hash) { |hash, key| hash[key.to_sym] ||= {} }
|
52
|
-
target_hash_parent_element.merge!({
|
60
|
+
target_hash_parent_element.merge!({
|
61
|
+
sanitized_target_path.last => ((source_value.nil? && !(default_value = rule.last[:default]).nil?) ? default_value : source_value)
|
62
|
+
})
|
53
63
|
end
|
54
64
|
end
|
data/lib/yahm/version.rb
CHANGED
data/spec/yahm/mapping_spec.rb
CHANGED
@@ -16,11 +16,14 @@ describe Yahm::Mapping do
|
|
16
16
|
describe ".translate_hash" do
|
17
17
|
let(:mapping) do
|
18
18
|
_mapping = Yahm::Mapping.new
|
19
|
-
_mapping.map "/record_id",
|
20
|
-
_mapping.map "/record/title",
|
21
|
-
_mapping.map "/record/isbns",
|
22
|
-
_mapping.map "/record/isbns[1]",
|
23
|
-
_mapping.map "/record/count",
|
19
|
+
_mapping.map "/record_id", to: "/id"
|
20
|
+
_mapping.map "/record/title", to: "/tile"
|
21
|
+
_mapping.map "/record/isbns", to: "/my_data/isbns"
|
22
|
+
_mapping.map "/record/isbns[1]", to: "/main_isbn" # when an array, one can specifiy which element to choose
|
23
|
+
_mapping.map "/record/count", to: "/count", :processed_by => :to_i # processed_by specifies a method which post_processes to value
|
24
|
+
_mapping.map "/record/languages", to: "/languages", force_array: true
|
25
|
+
_mapping.map "/record/authors", to: "/authors", split_by: ";"
|
26
|
+
_mapping.map "/record/version", to: "/version", default: 1
|
24
27
|
_mapping
|
25
28
|
end
|
26
29
|
|
@@ -33,7 +36,9 @@ describe Yahm::Mapping do
|
|
33
36
|
"3-86680-192-0",
|
34
37
|
"3-680-08783-7"
|
35
38
|
],
|
36
|
-
count: "3"
|
39
|
+
count: "3",
|
40
|
+
languages: "ger",
|
41
|
+
authors: "John Doe; Jane Doe"
|
37
42
|
}
|
38
43
|
}
|
39
44
|
end
|
@@ -44,7 +49,10 @@ describe Yahm::Mapping do
|
|
44
49
|
:tile=>"some title",
|
45
50
|
:my_data=>{:isbns=>["3-86680-192-0", "3-680-08783-7"]},
|
46
51
|
:main_isbn=>"3-680-08783-7",
|
47
|
-
:count=>3
|
52
|
+
:count=>3,
|
53
|
+
:languages=>["ger"],
|
54
|
+
:authors=>["John Doe", "Jane Doe"],
|
55
|
+
:version=>1
|
48
56
|
})
|
49
57
|
end
|
50
58
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yahm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Sievers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|