any2tmx 1.0.2 → 1.0.3

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
  SHA1:
3
- metadata.gz: 9d4a0d4bd6929c4b054a9dbb5fa4a22b64a080ab
4
- data.tar.gz: 76c7f0b7b3d31125efa7afb977d5f4c4c884bcf0
3
+ metadata.gz: 254e2e49f9066afc36e3680b124e270f39aeffcf
4
+ data.tar.gz: 4c98b41da4be3aea62dc8c3ee708e340caf493d6
5
5
  SHA512:
6
- metadata.gz: 352cb9d0d316b99ee20011d0576f9ff368be72490f59c6367e417356c8caeedf3dcbe30ac338bd073135407fb94249d4769b3da11015361eceb3760f5705a19d
7
- data.tar.gz: 44c1dd91dc6384d6ef4ba988c11518b6272df3b39bc9b082ce0012308d718df9472ebe788348eb54adb60e3e92560c24ec2f95e6714fe825c8e36d5c98ddd508
6
+ metadata.gz: 393d789effc3dc7ec69e7d6ed672999c70a8000a314e91d8d8abdae771e64f1a324778625ab9f93067432e11c6904e767a588aadd5ebf4e3dd95f5f49e1e660a
7
+ data.tar.gz: d459eca4642a71e6ca71fc1599c2be0cf329f37a72162cbebb960fb212478f179bec612f46cdba032c0fd68345fbd77226ab17856c4b97b41ad4f60679710fc4
@@ -14,3 +14,8 @@
14
14
  == 1.0.2
15
15
 
16
16
  * Fix Android transform bug
17
+
18
+ == 1.0.3
19
+
20
+ * Stringify keys when processing YAML
21
+
@@ -17,7 +17,7 @@ module Any2Tmx
17
17
  contents = contents[locale]
18
18
  end
19
19
 
20
- traversable = Any2Tmx::Traversable.new(contents)
20
+ traversable = Any2Tmx::Traversable.new(contents).stringify_keys
21
21
  Any2Tmx::PhraseSet.new(traversable, locale)
22
22
  end
23
23
  end
@@ -8,7 +8,9 @@ module Any2Tmx
8
8
 
9
9
  def each_entry(&block)
10
10
  if block_given?
11
- each_entry_helper(collection, [], &block)
11
+ each_entry_helper(collection, [], nil) do |entry, path, last_key|
12
+ yield entry, path + [last_key]
13
+ end
12
14
  else
13
15
  to_enum(__method__)
14
16
  end
@@ -18,17 +20,46 @@ module Any2Tmx
18
20
  each_entry.inject(0) { |ret, _| ret + 1 }
19
21
  end
20
22
 
23
+ def dig_each(path)
24
+ if block_given?
25
+ path.inject(:start) do |ret, seg|
26
+ if ret == :start
27
+ yield collection, seg
28
+ collection[seg]
29
+ elsif ret
30
+ if seg.is_a?(Numeric) && ret.is_a?(Array)
31
+ yield ret, seg
32
+ ret[seg] # array index case
33
+ elsif ret.is_a?(Hash)
34
+ yield ret, seg
35
+ ret[seg] # hash key case
36
+ end
37
+ end
38
+ end
39
+ else
40
+ to_enum(__method__, path)
41
+ end
42
+ end
43
+
21
44
  def dig(path)
22
- path.inject(:start) do |ret, seg|
23
- if ret == :start
24
- collection[seg]
25
- elsif ret
26
- if seg.is_a?(Numeric) && ret.is_a?(Array)
27
- ret[seg] # array index case
28
- elsif seg.is_a?(String) && ret.is_a?(Hash)
29
- ret[seg] # hash key case
45
+ return collection if path.empty?
46
+ dig_each(path) {}
47
+ end
48
+
49
+ def transform
50
+ if block_given?
51
+ self.class.new(make_empty(sym_from(collection))).tap do |copy|
52
+ each_entry_helper(collection, [], nil) do |entry, path, last_key|
53
+ dig_each(path).inject(copy.collection) do |copy_obj, (obj, key)|
54
+ copy_obj[key] ||= make_empty(sym_from(obj[key]))
55
+ copy_obj[key]
56
+ end
57
+
58
+ yield copy.dig(path), last_key, entry
30
59
  end
31
60
  end
61
+ else
62
+ to_enum(__method__)
32
63
  end
33
64
  end
34
65
 
@@ -40,22 +71,56 @@ module Any2Tmx
40
71
  end
41
72
  end
42
73
 
74
+ def stringify_keys
75
+ transform do |obj, key, value|
76
+ obj[stringify(key)] = value
77
+ end
78
+ end
79
+
43
80
  private
44
81
 
45
- def each_entry_helper(coll, path, &block)
82
+ def stringify(obj)
83
+ obj.is_a?(Symbol) ? obj.to_s : obj
84
+ end
85
+
86
+ def sym_from(obj)
87
+ case obj
88
+ when Hash
89
+ :hash
90
+ when Array
91
+ :array
92
+ end
93
+ end
94
+
95
+ def make_empty(sym)
96
+ case sym
97
+ when :hash
98
+ {}
99
+ when :array
100
+ []
101
+ end
102
+ end
103
+
104
+ def each_entry_helper(coll, path, last_key, &block)
105
+ next_path = if last_key
106
+ path + [last_key]
107
+ else
108
+ path
109
+ end
110
+
46
111
  case coll
47
112
  when Hash
48
113
  coll.each_pair do |key, value|
49
- each_entry_helper(value, path + [key], &block)
114
+ each_entry_helper(value, next_path, key, &block)
50
115
  end
51
116
 
52
117
  when Array
53
118
  coll.each_with_index do |element, idx|
54
- each_entry_helper(element, path + [idx], &block)
119
+ each_entry_helper(element, next_path, idx, &block)
55
120
  end
56
121
 
57
122
  when String
58
- yield coll, path
123
+ yield coll, path, last_key
59
124
  end
60
125
  end
61
126
  end
@@ -1,3 +1,3 @@
1
1
  module Any2Tmx
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: any2tmx
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-10 00:00:00.000000000 Z
11
+ date: 2016-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xml-write-stream