zweikopf 0.0.3 → 0.0.4

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.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use jruby-1.6.7.2
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  rvm:
2
2
  - jruby
3
3
  jdk:
4
- - openjdk6
5
4
  - openjdk7
6
5
  script: "rake && lein2 test"
@@ -9,5 +9,13 @@ module Zweikopf
9
9
  end)
10
10
  end # to_persistent_vector
11
11
 
12
+ def self.from_clj(clj_arr)
13
+ [].tap do |ruby_arr|
14
+ clj_arr.each do |v|
15
+ ruby_arr<< Zweikopf::Transformer.from_clj(v);
16
+ end
17
+ end
18
+ end # self.from_clj
19
+
12
20
  end # Array
13
21
  end # Zweikopf
data/lib/zweikopf/hash.rb CHANGED
@@ -16,14 +16,10 @@ module Zweikopf
16
16
  ret.persistent
17
17
  end
18
18
 
19
- def self.from_clj(clj_hash)
19
+ def self.from_clj(clj_hash, &block)
20
20
  {}.tap do |ruby_map|
21
21
  clj_hash.each do |k, v|
22
- if v.is_a?(PersistentHashMap) || v.is_a?(PersistentArrayMap)
23
- ruby_map[Keyword.to_ruby(k)] = self.from_clj(v)
24
- else
25
- ruby_map[Keyword.to_ruby(k)] = v
26
- end
22
+ ruby_map[Keyword.from_clj(k)] = Zweikopf::Transformer.from_clj(v, &block);
27
23
  end
28
24
  end
29
25
  end # self.from_clj
@@ -7,9 +7,9 @@ module Zweikopf
7
7
  ::Keyword.intern(keyword.to_s)
8
8
  end # self.from_ruby
9
9
 
10
- def self.to_ruby(keyword)
10
+ def self.from_clj(keyword)
11
11
  keyword.to_s.gsub(/:/, "").to_sym
12
- end # self.to_ruby
12
+ end # self.from_clj
13
13
 
14
14
  end # Keyword
15
15
  end # Zweikopf
@@ -1,3 +1,8 @@
1
+ java_import "clojure.lang.IPersistentMap"
2
+ java_import "clojure.lang.IPersistentVector"
3
+ java_import "clojure.lang.IPersistentList"
4
+ java_import "clojure.lang.Keyword"
5
+
1
6
  module Zweikopf
2
7
  module Transformer
3
8
 
@@ -17,5 +22,21 @@ module Zweikopf
17
22
  end
18
23
  end # self.from_ruby
19
24
 
25
+ def self.from_clj(obj, &block)
26
+ if Primitive.is_primitive_type?(obj)
27
+ obj
28
+ elsif obj.is_a?(IPersistentMap)
29
+ Hash.from_clj(obj, &block)
30
+ elsif obj.is_a?(IPersistentVector || IPersistentList)
31
+ Array.from_clj(obj, &block)
32
+ elsif obj.is_a?(::Keyword)
33
+ Keyword.from_clj(obj)
34
+ elsif !block.nil?
35
+ from_clj(yield(obj))
36
+ else
37
+ obj
38
+ end
39
+ end # self.from_clj
40
+
20
41
  end # Transformer
21
42
  end # Zweikopf
@@ -1,3 +1,3 @@
1
1
  module Zweikopf
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1 @@
1
+ {:a 1 :b [2 3 4]}
data/spec/spec_helper.rb CHANGED
@@ -1,15 +1,10 @@
1
- puts RUBY_PLATFORM
2
-
3
- require 'java'
4
-
5
-
6
1
  deps = File.join(File.dirname(__FILE__), "..", "deps")
7
2
 
8
- # Dir["#{deps}/\*.jar"].each do |jar|
9
- require "/Users/alexp/p/zweikopf/deps/clojure-1.4.0.jar" # jar
10
- # end
11
-
3
+ Dir["#{deps}/\*.jar"].each do |jar|
4
+ require jar
5
+ end
12
6
 
7
+ require 'java'
13
8
  java_import "clojure.lang.RT"
14
9
 
15
10
  require 'zweikopf'
@@ -1,11 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe :performance do
4
- it "this spec simply shows performance" do
4
+
5
+ #
6
+ # Environment
7
+ #
8
+
9
+ let(:clj_hash) { load_fixture(:clj_hash1) }
10
+
11
+ #
12
+ # Examples
13
+ #
14
+
15
+ it "this spec simply shows performance, very roughly, please do your testing depending on your needs" do
16
+ time_before = Time.now.to_f
17
+ 100000.times do |i|
18
+ Zweikopf::Transformer.from_ruby({:a => i, :b => i})
19
+ end
20
+ puts "Ruby to Clojure, time elapsed: #{Time.now.to_f - time_before} seconds"
21
+
5
22
  time_before = Time.now.to_f
6
23
  100000.times do |i|
7
- Zweikopf::Hash.create({:a => i, :b => i})
24
+ Zweikopf::Transformer.from_clj(clj_hash)
8
25
  end
9
- puts "Time elapsed: #{Time.now.to_f - time_before} seconds"
26
+
27
+ puts "Clojure to Ruby, time elapsed: #{Time.now.to_f - time_before} seconds"
10
28
  end
29
+
11
30
  end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ java_import 'clojure.lang.Util'
3
+
4
+ describe Zweikopf do
5
+ context "when given a non-recursive hash structure" do
6
+
7
+ #
8
+ # Environment
9
+ #
10
+
11
+ let(:hash) { load_fixture(:clj_hash1) }
12
+
13
+ #
14
+ # Examples
15
+ #
16
+
17
+ it "creates a Clojure hash" do
18
+ Zweikopf::Transformer.from_clj(hash).should eql({:a => 1, :b => 2})
19
+ end
20
+ end
21
+
22
+ context "when given a recursive hash structure" do
23
+
24
+ #
25
+ # Environment
26
+ #
27
+
28
+ let(:hash) { load_fixture(:clj_deep_hash1) }
29
+
30
+ #
31
+ # Examples
32
+ #
33
+
34
+ it "creates a Clojure hash" do
35
+ Zweikopf::Transformer.from_clj(hash).should eql({:a => 1, :b => {:c => 3, :d =>4}})
36
+ end
37
+ end
38
+
39
+ context "when given a clojure array" do
40
+
41
+ #
42
+ # Environment
43
+ #
44
+
45
+ let(:clj_array) { load_fixture(:clj_array1) }
46
+
47
+ #
48
+ # Examples
49
+ #
50
+
51
+ it "creates a Ruby array" do
52
+ Zweikopf::Transformer.from_clj(clj_array).should eql([:a, 1, :b, 2, :c, 3])
53
+ end
54
+ end
55
+
56
+
57
+ context "when given a hash that contains array that has a hash as an item" do
58
+
59
+ #
60
+ # Environment
61
+ #
62
+
63
+ let(:complex_hash) { load_fixture(:complex_hash) }
64
+
65
+ #
66
+ # Examples
67
+ #
68
+
69
+ it "creates a Ruby array" do
70
+ Zweikopf::Transformer.from_clj(complex_hash).should eql({:a => 1, :b => [2, 3, 4]})
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+ java_import 'clojure.lang.Util'
3
+
4
+ describe Zweikopf do
5
+ describe :empty do
6
+ let(:empty_hash) { load_fixture(:empty_hash) }
7
+
8
+ it "returns empty clojure hash" do
9
+ Zweikopf::Hash.empty.should eql empty_hash
10
+ end
11
+ end
12
+
13
+ context "when given a non-recursive hash structure" do
14
+
15
+ #
16
+ # Environment
17
+ #
18
+
19
+ let(:hash) { load_fixture(:clj_hash1) }
20
+
21
+ #
22
+ # Examples
23
+ #
24
+
25
+ it "creates a Clojure hash" do
26
+ Zweikopf::Transformer.from_ruby({:a => 1, :b => 2}).should eql hash
27
+ end
28
+ end
29
+
30
+ context "when given a recursive hash structure" do
31
+
32
+ #
33
+ # Environment
34
+ #
35
+
36
+ let(:hash) { load_fixture(:clj_deep_hash1) }
37
+
38
+ #
39
+ # Examples
40
+ #
41
+
42
+ it "creates a Clojure hash" do
43
+ Zweikopf::Transformer.from_ruby({:a => 1, :b => {:c => 3, :d =>4}}).should eql hash
44
+ end
45
+ end
46
+
47
+ context "given a data structure that requires custom transformations" do
48
+
49
+ #
50
+ # Environment
51
+ #
52
+
53
+ class CustomTransformedEntry
54
+ def serializable_hash
55
+ {:c => 3, :d => 4}
56
+ end
57
+ end
58
+
59
+ let(:hash) { load_fixture(:clj_deep_hash1) }
60
+
61
+ it "creates a Clojure hash with given block" do
62
+ Zweikopf::Transformer.from_ruby({:a => 1, :b => CustomTransformedEntry.new }) do |v|
63
+ if v.is_a?(CustomTransformedEntry)
64
+ v.serializable_hash
65
+ else
66
+ v
67
+ end
68
+ end.should eql hash
69
+ end
70
+
71
+ end
72
+
73
+ context "when given a clojure array" do
74
+
75
+ #
76
+ # Environment
77
+ #
78
+
79
+ let(:clj_array) { load_fixture(:clj_array1) }
80
+
81
+ #
82
+ # Examples
83
+ #
84
+
85
+ it "creates a Ruby array" do
86
+ Zweikopf::Transformer.from_ruby([:a, 1, :b, 2, :c, 3]).should eql(clj_array)
87
+ end
88
+ end
89
+
90
+
91
+ context "when given a hash that contains array that has a hash as an item" do
92
+
93
+ #
94
+ # Environment
95
+ #
96
+
97
+ let(:complex_hash) { load_fixture(:complex_hash) }
98
+
99
+ #
100
+ # Examples
101
+ #
102
+
103
+ it "creates a Ruby array" do
104
+ Zweikopf::Transformer.from_ruby({:a => 1, :b => [2, 3, 4]}).should eql(complex_hash)
105
+ end
106
+ end
107
+
108
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: zweikopf
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Oleksandr Petrov
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-07-18 00:00:00 Z
13
+ date: 2012-09-07 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -46,6 +46,7 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - .gitignore
48
48
  - .rspec
49
+ - .rvmrc
49
50
  - .travis.yml
50
51
  - Gemfile
51
52
  - LICENSE
@@ -63,11 +64,13 @@ files:
63
64
  - spec/fixtures/clj_array1.clj
64
65
  - spec/fixtures/clj_deep_hash1.clj
65
66
  - spec/fixtures/clj_hash1.clj
67
+ - spec/fixtures/complex_hash.clj
66
68
  - spec/fixtures/empty_hash.clj
67
69
  - spec/spec_helper.rb
68
70
  - spec/support/loader.rb
69
- - spec/zweikopf/hash_spec.rb
70
71
  - spec/zweikopf/performance_spec.rb
72
+ - spec/zweikopf/transformer_clojure_to_ruby_spec.rb
73
+ - spec/zweikopf/transformer_ruby_to_clojure_spec.rb
71
74
  - src/zweikopf/core.clj
72
75
  - test/zweikopf/core_test.clj
73
76
  - zweikopf.gemspec
@@ -102,10 +105,11 @@ test_files:
102
105
  - spec/fixtures/clj_array1.clj
103
106
  - spec/fixtures/clj_deep_hash1.clj
104
107
  - spec/fixtures/clj_hash1.clj
108
+ - spec/fixtures/complex_hash.clj
105
109
  - spec/fixtures/empty_hash.clj
106
110
  - spec/spec_helper.rb
107
111
  - spec/support/loader.rb
108
- - spec/zweikopf/hash_spec.rb
109
112
  - spec/zweikopf/performance_spec.rb
113
+ - spec/zweikopf/transformer_clojure_to_ruby_spec.rb
114
+ - spec/zweikopf/transformer_ruby_to_clojure_spec.rb
110
115
  - test/zweikopf/core_test.clj
111
- has_rdoc:
@@ -1,130 +0,0 @@
1
- require 'spec_helper'
2
- java_import 'clojure.lang.Util'
3
- describe Zweikopf::Hash do
4
- describe :empty do
5
- let(:empty_hash) { load_fixture(:empty_hash) }
6
-
7
- it "returns empty clojure hash" do
8
- Zweikopf::Hash.empty.should eql empty_hash
9
- end
10
- end
11
-
12
- describe :create do
13
-
14
-
15
- context "when given a non-recursive hash structure" do
16
-
17
- #
18
- # Environment
19
- #
20
-
21
- let(:hash) { load_fixture(:clj_hash1) }
22
-
23
- #
24
- # Examples
25
- #
26
-
27
- it "creates a Clojure hash" do
28
- Zweikopf::Hash.from_ruby({:a => 1, :b => 2}).should eql hash
29
- end
30
- end
31
-
32
- context "when given a recursive hash structure" do
33
-
34
- #
35
- # Environment
36
- #
37
-
38
- let(:hash) { load_fixture(:clj_deep_hash1) }
39
-
40
- #
41
- # Examples
42
- #
43
-
44
- it "creates a Clojure hash" do
45
- Zweikopf::Hash.from_ruby({:a => 1, :b => {:c => 3, :d =>4}}).should eql hash
46
- end
47
- end
48
-
49
- context "given a data structure that requires custom transformations" do
50
-
51
- #
52
- # Environment
53
- #
54
-
55
- class CustomTransformedEntry
56
- def serializable_hash
57
- {:c => 3, :d => 4}
58
- end
59
- end
60
-
61
- let(:hash) { load_fixture(:clj_deep_hash1) }
62
-
63
- it "creates a Clojure hash with given block" do
64
- Zweikopf::Hash.from_ruby({:a => 1, :b => CustomTransformedEntry.new }) do |v|
65
- if v.is_a?(CustomTransformedEntry)
66
- v.serializable_hash
67
- else
68
- v
69
- end
70
- end.should eql hash
71
- end
72
-
73
- end
74
- end
75
-
76
- describe :from_clj do
77
- context "given a non-recursive clojure hash structure" do
78
-
79
- #
80
- # Environment
81
- #
82
-
83
- let(:clj_hash) { load_fixture(:clj_hash1) }
84
-
85
- #
86
- # Examples
87
- #
88
-
89
- it "creates a Ruby hash" do
90
- Zweikopf::Hash.from_clj(clj_hash).should eql({:a => 1, :b => 2})
91
- end
92
- end
93
-
94
- context "when given a recursive hash structure" do
95
-
96
- #
97
- # Environment
98
- #
99
-
100
- let(:clj_hash) { load_fixture(:clj_deep_hash1) }
101
-
102
- #
103
- # Examples
104
- #
105
-
106
- it "creates a Clojure hash" do
107
- Zweikopf::Hash.from_clj(clj_hash).should eql({:a => 1, :b => {:c => 3, :d =>4}})
108
- end
109
- end
110
-
111
- context "when given a clojure array" do
112
-
113
- #
114
- # Environment
115
- #
116
-
117
- let(:clj_array) { load_fixture(:clj_array1) }
118
-
119
- #
120
- # Examples
121
- #
122
-
123
- it "creates a Ruby array" do
124
- Zweikopf::Array.from_ruby([:a, 1, :b, 2, :c, 3]).should eql(clj_array)
125
- end
126
- end
127
-
128
-
129
- end
130
- end