yahm 0.0.1 → 0.0.2

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: f213aa22618a1d60c870526fc85dc3e9b2a67357
4
- data.tar.gz: 26023f9fb09290b29111cdc4e36b2e4d9dc5fd60
3
+ metadata.gz: 0522d4946a438ae61d4e9fdc77d59b6b249363b8
4
+ data.tar.gz: 19fc48e5d52f76931497cab5a2e319e20e907039
5
5
  SHA512:
6
- metadata.gz: 9740821a0ed3d3cdfd3bff113d59ce86b4576b0f6c41e4425b4a296647c6fa300ced3225393ae30481b8a284eb1e7e0497970dbb820487aa0c1dbd2be4d62a87
7
- data.tar.gz: 2c5f468c73d829df3173b35a882f425b12b94196d994dc44ab9dd6a2e154e65f88f8128ed07b11aaf2fb8666ae1cbc4b73cb20b6f925cf0f3c7316e7109dc5f8
6
+ metadata.gz: 085c44754437976e7109044724eb5f71006c130e82d0643296b88446f6377f84ffa248a31a00edf575072c8bbf91b62c72fe98e9f4c9e3a7353f54aad2771b79
7
+ data.tar.gz: 047fd6fc33b4d5f65258ee0467b52d9696b7de83130072784bcb062ace83611bd9c2401aaf73141373607987be6a7da72d035af7e059b9510b847483ba89d911
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/yahm.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "yahm/version"
2
2
 
3
- module Yahm
4
- # Your code goes here...
3
+ class Yahm
4
+ require "yahm/hash_mapper"
5
+ require "yahm/mapping"
5
6
  end
@@ -0,0 +1,19 @@
1
+ module Yahm::HashMapper
2
+ def define_mapper(mapper_method_name, options = {}, &block)
3
+ mapping = Yahm::Mapping.new
4
+
5
+ # evaluate the given block in mappings context
6
+ mapping = mapping.instance_eval(&block) || mapping
7
+
8
+ define_method mapper_method_name do |hash|
9
+ translated_hash = mapping.translate_hash(hash)
10
+ unless (setter_name = options[:call_setter]).nil?
11
+ self.send(setter_name, translated_hash)
12
+ end
13
+
14
+ translated_hash
15
+ end
16
+
17
+ self
18
+ end
19
+ end
@@ -0,0 +1,54 @@
1
+ class Yahm::Mapping
2
+ def initialize
3
+ @rules = []
4
+ @translated_hash = nil
5
+ end
6
+
7
+ def map(str, options = {})
8
+ @rules.push [str, options]
9
+ self
10
+ end
11
+
12
+ def translate_hash(input_hash)
13
+ @break = true if @rules.length == 5
14
+ @translated_hash = {}
15
+
16
+ @rules.each do |rule|
17
+ process_rule(input_hash, rule)
18
+ end
19
+
20
+ @translated_hash
21
+ end
22
+
23
+ private
24
+
25
+ def process_rule(input_hash, rule)
26
+ sanitized_source_path = rule.first
27
+ .sub(/^\//, "")
28
+ .gsub(/\[(\d+)\]/, "/\\1")
29
+ .split("/")
30
+ .map do |element|
31
+ if element[/\A\d+\Z/]
32
+ element.to_i
33
+ else
34
+ element.to_sym
35
+ end
36
+ end
37
+
38
+ sanitized_target_path = rule.last[:to].sub(/^\//, "").split("/").map(&:to_sym)
39
+ target_parent_path = sanitized_target_path.slice(0, sanitized_target_path.length - 1)
40
+
41
+ source_value = sanitized_source_path.inject(input_hash) { |hash, key| hash[key] }
42
+
43
+ unless (processed_by_method_name = rule.last[:processed_by]).nil?
44
+ source_value = if source_value.is_a?(Array)
45
+ source_value.map(&processed_by_method_name)
46
+ else
47
+ source_value.send(processed_by_method_name)
48
+ end
49
+ end
50
+
51
+ 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 })
53
+ end
54
+ end
data/lib/yahm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Yahm
2
- VERSION = "0.0.1"
1
+ class Yahm
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require "yahm"
2
+ require "pry"
3
+
4
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+
10
+ # Run specs in random order to surface order dependencies. If you find an
11
+ # order dependency and want to debug it, you can fix the order by providing
12
+ # the seed, which is printed after each run.
13
+ # --seed 1234
14
+ config.order = "random"
15
+ end
@@ -0,0 +1,48 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Yahm::HashMapper do
4
+ context "when extending a class" do
5
+ let(:class_extended_with_hash_mapper) do
6
+ class MyClass
7
+ extend Yahm::HashMapper
8
+ end
9
+ end
10
+
11
+ it "adds a class method called #define_mapper" do
12
+ expect(class_extended_with_hash_mapper.methods).to include(:define_mapper)
13
+ end
14
+ end
15
+
16
+ describe "#define_mapper" do
17
+ let(:class_extended_with_hash_mapper) do
18
+ class ClassExtendedWithHashMapper
19
+ extend Yahm::HashMapper
20
+
21
+ define_mapper :my_mapper do
22
+ end
23
+ end
24
+ end
25
+
26
+ it "adds a named mapper method to the instances of the extended class" do
27
+ expect(class_extended_with_hash_mapper.new).to respond_to(:my_mapper)
28
+ end
29
+
30
+ context "when called with an option called 'call_setter'" do
31
+ let(:class_extended_with_hash_mapper) do
32
+ class ClassExtendedWithHashMapper
33
+ extend Yahm::HashMapper
34
+
35
+ attr_accessor :my_result
36
+
37
+ define_mapper :my_mapper, call_setter: :my_result= do
38
+ end
39
+ end
40
+ end
41
+
42
+ it "calls a method with the given name with the result of the mapping" do
43
+ (instance = class_extended_with_hash_mapper.new).my_mapper({})
44
+ expect(instance.my_result).not_to be_nil
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,52 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Yahm::Mapping do
4
+ it "is a class used internally by Yahm::HashMapper#define_mapper" do
5
+ end
6
+
7
+ describe ".map" do
8
+ let(:mapping) { Yahm::Mapping.new }
9
+
10
+ it "adds a mapping rule to a mapping" do
11
+ mapping.map("/foo", to: "bar")
12
+ expect(mapping.instance_variable_get :@rules).not_to be_nil
13
+ end
14
+ end
15
+
16
+ describe ".translate_hash" do
17
+ let(:mapping) do
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
24
+ _mapping
25
+ end
26
+
27
+ let(:input_hash) do
28
+ {
29
+ record_id: "some_id123",
30
+ record: {
31
+ title: "some title",
32
+ isbns: [
33
+ "3-86680-192-0",
34
+ "3-680-08783-7"
35
+ ],
36
+ count: "3"
37
+ }
38
+ }
39
+ end
40
+
41
+ it "translates a given hash according to @rules" do
42
+ expect(mapping.translate_hash(input_hash)).to eq({
43
+ :id=>"some_id123",
44
+ :tile=>"some title",
45
+ :my_data=>{:isbns=>["3-86680-192-0", "3-680-08783-7"]},
46
+ :main_isbn=>"3-680-08783-7",
47
+ :count=>3
48
+ })
49
+ end
50
+ end
51
+
52
+ end
data/spec/yahm_spec.rb ADDED
@@ -0,0 +1,8 @@
1
+ require_relative "./spec_helper"
2
+
3
+ describe Yahm do
4
+ it "has a module called HashMapper" do
5
+ expect(Yahm.const_defined? :HashMapper).to be_true
6
+ expect(Yahm::HashMapper.class.is_a? Module)
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sievers
@@ -116,12 +116,19 @@ extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
118
  - ".gitignore"
119
+ - ".rspec"
119
120
  - Gemfile
120
121
  - LICENSE.txt
121
122
  - README.md
122
123
  - Rakefile
123
124
  - lib/yahm.rb
125
+ - lib/yahm/hash_mapper.rb
126
+ - lib/yahm/mapping.rb
124
127
  - lib/yahm/version.rb
128
+ - spec/spec_helper.rb
129
+ - spec/yahm/hash_mapper_spec.rb
130
+ - spec/yahm/mapping_spec.rb
131
+ - spec/yahm_spec.rb
125
132
  - yahm.gemspec
126
133
  homepage: https://github.com/ubpb/yahm
127
134
  licenses:
@@ -147,4 +154,8 @@ rubygems_version: 2.2.0
147
154
  signing_key:
148
155
  specification_version: 4
149
156
  summary: Yet another ruby hash mapper
150
- test_files: []
157
+ test_files:
158
+ - spec/spec_helper.rb
159
+ - spec/yahm/hash_mapper_spec.rb
160
+ - spec/yahm/mapping_spec.rb
161
+ - spec/yahm_spec.rb