data_processor 0.1.0 → 0.2.0

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: c5d3e4b459833af7ae7a6fae536aed4e0fe7c0bf
4
- data.tar.gz: 16eebb950572912b767e7d81560642568add98da
3
+ metadata.gz: 8992f6b170310848c8c18c5d9e6dda6f61c60e6e
4
+ data.tar.gz: 2fbc81f4985ac7edc2e7c253817ed8da22e0067b
5
5
  SHA512:
6
- metadata.gz: 42727dbc3633d50c8e5f2fe21e2b76d20426a76c9b88b25fa07a0afbf452cb01505c536d57e3edc36a4ba29206f01657b88c79c245efc5d25086e8e8d02a0f6f
7
- data.tar.gz: 3d6d00e5015020c34a8add41c3801cda92349d265a2b4647d79d180ab39f5e2b0f2ded48561bb2bc7b50c7444c896cd3dfe889798e24bf9baea4c81e1a39c404
6
+ metadata.gz: e550edb2a630fd902e2306dc59d8e923f85750cf2bd183b640766961182cdac820f691e762805b05d4f49fe866dc2d5d8603521437e3747fff32b37c30a97c2f
7
+ data.tar.gz: 2334a1d7b9810dc805235e4992cc97afe5a12d97103509c081650c46dfc164c1d1375f5436f526f6e8871b8e44de7b283d85ae6c65287dc484f6e3e86a56ee38
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+
4
+ ## 0.2.0
5
+
6
+ - Path traversing is now a seperate method (helpers module)
7
+ - Add ability to output specific path in json
data/README.md CHANGED
@@ -81,6 +81,15 @@ d.output_json # json
81
81
 
82
82
 
83
83
 
84
+ ## Install
85
+
86
+ ```ruby
87
+ # Gemfile
88
+ gem 'data_processor'
89
+ ```
90
+
91
+
92
+
84
93
  ## Markdown
85
94
 
86
95
  Redcarpet is used here to parse markdown. You can override the markdown renderer by overriding `DataProcessor::markdown_renderer`, which returns a renderer.
@@ -3,12 +3,14 @@ require "redcarpet"
3
3
  require "oj"
4
4
 
5
5
  require "data_processor/getters"
6
+ require "data_processor/helpers"
6
7
  require "data_processor/import"
7
8
  require "data_processor/manipulate"
8
9
  require "data_processor/output"
9
10
 
10
11
  class DataProcessor
11
12
  include DataProcessor::Getters
13
+ include DataProcessor::Helpers
12
14
  include DataProcessor::Import
13
15
  include DataProcessor::Manipulate
14
16
  include DataProcessor::Output
@@ -0,0 +1,30 @@
1
+ class DataProcessor
2
+ module Helpers
3
+
4
+ def traverse_path(object, path, use_symbols=false)
5
+ obj = object
6
+ parent_obj = nil
7
+ path_split = path.split("/")
8
+ counter = 0
9
+
10
+ # down the rabbit hole
11
+ path_split.each do |p|
12
+ p = p.to_sym if use_symbols
13
+
14
+ if obj && obj[p]
15
+ parent_obj = obj
16
+ obj = obj[p]
17
+ counter = counter + 1
18
+ else
19
+ parent_obj = nil
20
+ obj = nil
21
+ end
22
+ end
23
+
24
+ if obj && (counter == path_split.length)
25
+ return obj, parent_obj
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -2,35 +2,15 @@ class DataProcessor
2
2
  module Manipulate
3
3
 
4
4
  def manipulate(path, override=false)
5
- obj = @data
6
- parent_obj = nil
5
+ obj, parent_obj = traverse_path(@data, path)
7
6
 
8
- if block_given?
9
- counter = 0
10
- path_split = path.split("/")
11
-
12
- # down the rabbit hole
13
- path_split.each do |p|
14
- if obj && obj[p]
15
- parent_obj = obj
16
- obj = obj[p]
17
- counter = counter + 1
18
- else
19
- parent_obj = nil
20
- obj = nil
21
- end
22
- end
23
-
24
- # execute block with object as param
25
- # ie. if the object is found
26
- # + override object with return value of block if needed
27
- if obj && (counter == path_split.length)
28
- return_value = yield(obj, parent_obj)
29
- parent_obj[path_split.last] = return_value if override
30
- end
7
+ # execute block with object and parent object as params
8
+ # ie. if the object is found
9
+ # + override object with return value of block if needed
10
+ if block_given? && obj
11
+ block_return_value = yield(obj, parent_obj)
12
+ parent_obj[path.split("/").last] = block_return_value if override
31
13
  end
32
-
33
- obj
34
14
  end
35
15
 
36
16
  end
@@ -1,8 +1,15 @@
1
1
  class DataProcessor
2
2
  module Output
3
3
 
4
- def output_json
5
- Oj.dump(@data)
4
+ def output_json(path=nil)
5
+ if path
6
+ obj, parent_obj = traverse_path(@data, path)
7
+ Oj.dump(obj || {})
8
+
9
+ else
10
+ Oj.dump(@data)
11
+
12
+ end
6
13
  end
7
14
 
8
15
  end
@@ -1,3 +1,3 @@
1
1
  class DataProcessor
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/test/test.rb CHANGED
@@ -65,12 +65,41 @@ class TestDataProcessor < MiniTest::Unit::TestCase
65
65
  # data
66
66
  data = @d.get_data
67
67
  parsed_json = Oj.load(@d.output_json)
68
+ sub_parsed_json = Oj.load(@d.output_json("en"))
68
69
 
69
70
  # assertions
70
71
  assert_equal(
71
72
  parsed_json["en"]["pages"]["index"]["value_from_index_yaml"],
72
73
  data["en"]["pages"]["index"]["value_from_index_yaml"]
73
74
  )
75
+
76
+ assert_equal(
77
+ sub_parsed_json["pages"]["index"]["value_from_index_yaml"],
78
+ data["en"]["pages"]["index"]["value_from_index_yaml"]
79
+ )
80
+ end
81
+
82
+
83
+ def test_helpers
84
+ data = { "a" => { "b" => { "c" => "D" }}}
85
+ data_symbols = { a: { b: { c: "D" }}}
86
+
87
+ # test 1
88
+ obj, parent_obj = @d.traverse_path(data, "a/b")
89
+
90
+ assert_kind_of(Hash, obj)
91
+ assert_kind_of(String, obj["c"])
92
+
93
+ # test 2
94
+ obj, parent_obj = @d.traverse_path(data_symbols, "a/b", true)
95
+
96
+ assert_kind_of(Hash, obj)
97
+ assert_kind_of(String, obj[:c])
98
+
99
+ # test 3
100
+ obj, parent_obj = @d.traverse_path(data, "unknown")
101
+
102
+ assert_kind_of(NilClass, obj)
74
103
  end
75
104
 
76
105
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_processor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Vandevelde
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-25 00:00:00.000000000 Z
11
+ date: 2014-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redcarpet
@@ -89,6 +89,7 @@ extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
91
  - ".gitignore"
92
+ - CHANGELOG.md
92
93
  - Gemfile
93
94
  - LICENSE.txt
94
95
  - README.md
@@ -96,6 +97,7 @@ files:
96
97
  - data_processor.gemspec
97
98
  - lib/data_processor.rb
98
99
  - lib/data_processor/getters.rb
100
+ - lib/data_processor/helpers.rb
99
101
  - lib/data_processor/import.rb
100
102
  - lib/data_processor/manipulate.rb
101
103
  - lib/data_processor/output.rb