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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0522d4946a438ae61d4e9fdc77d59b6b249363b8
4
- data.tar.gz: 19fc48e5d52f76931497cab5a2e319e20e907039
3
+ metadata.gz: fd289b056663d262256eb39d461f7ca8d0cb7daf
4
+ data.tar.gz: d41bebcad80b96d86c320982e355f01e7125a610
5
5
  SHA512:
6
- metadata.gz: 085c44754437976e7109044724eb5f71006c130e82d0643296b88446f6377f84ffa248a31a00edf575072c8bbf91b62c72fe98e9f4c9e3a7353f54aad2771b79
7
- data.tar.gz: 047fd6fc33b4d5f65258ee0467b52d9696b7de83130072784bcb062ace83611bd9c2401aaf73141373607987be6a7da72d035af7e059b9510b847483ba89d911
6
+ metadata.gz: 66dfa4baa5bf7bfced7383c222a971284f9d68d9018e5513e658104846bd75a78d2921c065fcc1a0180b251d2e0408a04e2a5b08f2b529e2d40349c0d4b5ca79
7
+ data.tar.gz: fb49353dc85d0cb7570539496de97427d2fb1695889e27c6491d5a04b49bbc38aab74cc798d95d12e2b9610dcbd540a25a138b56b0bbf27511c61795e6a83dbb
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Yahm
2
2
 
3
- TODO: Write a gem description
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
- TODO: Write usage instructions here
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
 
@@ -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!({ sanitized_target_path.last => source_value })
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
@@ -1,3 +1,3 @@
1
1
  class Yahm
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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", 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
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.2
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-24 00:00:00.000000000 Z
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler