rubyfox-sfsobject 0.0.1-universal-java-1.6 → 0.1.0-universal-java-1.6
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/README.md +21 -3
- data/lib/rubyfox/sfsobject/bulk.rb +4 -4
- data/lib/rubyfox/sfsobject/core_ext.rb +24 -0
- data/lib/rubyfox/sfsobject/schema.rb +110 -0
- data/lib/rubyfox/sfsobject/version.rb +1 -1
- data/test/rubyfox/sfsobject/bulk_test.rb +5 -7
- data/test/rubyfox/sfsobject/core_ext.rb +27 -0
- data/test/rubyfox/sfsobject/schema_test.rb +96 -0
- metadata +8 -5
- data/lib/rubyfox/sfsobject/extend.rb +0 -15
- data/test/rubyfox/sfsobject/extend_test.rb +0 -9
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Rubyfox::SFSObject
|
1
|
+
# Rubyfox::SFSObject [](http://travis-ci.org/neopoly/rubyfox-sfsobject)
|
2
2
|
|
3
3
|
Converts between SmartFox's SFSObjects and Ruby Hashes.
|
4
4
|
|
@@ -18,16 +18,30 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
### Bulk mode
|
22
|
+
|
21
23
|
require 'rubyfox/sfsobject/bulk'
|
22
24
|
sfs_object = Rubyfox::SFSObject::Bulk.to_sfs({ :hello => "world" })
|
23
25
|
# => SFSObject ready to use in SmartFox
|
24
|
-
hash = Rubyfox::SFSObject::Bulk.
|
26
|
+
hash = Rubyfox::SFSObject::Bulk.to_hash(sfs_object)
|
25
27
|
# => { :hello => "world" }
|
26
28
|
|
27
29
|
|
30
|
+
### Schema mode
|
31
|
+
|
32
|
+
require 'rubyfox/sfsobject/schema'
|
33
|
+
schema = { :hello => String }
|
34
|
+
sfs_object = Rubyfox::SFSObject::Schema.to_sfs(schema, { :hello => "world" })
|
35
|
+
# => SFSObject ready to use in SmartFox
|
36
|
+
hash = Rubyfox::SFSObject::Schema.to_hash(schema, sfs_object)
|
37
|
+
# => { :hello => "world" }
|
38
|
+
|
39
|
+
|
40
|
+
### Core extension
|
41
|
+
|
28
42
|
You can extend Hash and SFSObject with method shortcuts:
|
29
43
|
|
30
|
-
require 'rubyfox/sfsobject/
|
44
|
+
require 'rubyfox/sfsobject/core_ext'
|
31
45
|
sfs_object = { :hello => "world" }.to_sfs
|
32
46
|
# => SFSObject
|
33
47
|
sfs_object.to_hash
|
@@ -37,6 +51,10 @@ You can extend Hash and SFSObject with method shortcuts:
|
|
37
51
|
|
38
52
|
*Note* that all hash keys will be converted to symbols.
|
39
53
|
|
54
|
+
## TODO
|
55
|
+
|
56
|
+
* More docs, please!
|
57
|
+
|
40
58
|
## Contributing
|
41
59
|
|
42
60
|
1. Fork it
|
@@ -12,7 +12,7 @@ module Rubyfox
|
|
12
12
|
FalseClass => :putBool,
|
13
13
|
Fixnum => :putInt,
|
14
14
|
Float => :putDouble,
|
15
|
-
Hash => proc { |o, k, v| o.putSFSObject(k,
|
15
|
+
Hash => proc { |o, k, v| o.putSFSObject(k, to_sfs(v)) },
|
16
16
|
[String] => :putUtfStringArray,
|
17
17
|
[TrueClass] => :putBoolArray,
|
18
18
|
[FalseClass] => :putBoolArray,
|
@@ -24,7 +24,7 @@ module Rubyfox
|
|
24
24
|
[Float] => :putDoubleArray,
|
25
25
|
[Hash] => proc do |o, k, v|
|
26
26
|
ary = Java::SFSArray.new
|
27
|
-
v.each { |e| ary.addSFSObject(
|
27
|
+
v.each { |e| ary.addSFSObject(to_sfs(e)) }
|
28
28
|
o.putSFSArray(k, ary)
|
29
29
|
end
|
30
30
|
}
|
@@ -41,9 +41,9 @@ module Rubyfox
|
|
41
41
|
"INT_ARRAY" => proc { |h, k, v| h[k.to_sym] = v.object.to_a },
|
42
42
|
"LONG_ARRAY" => :getLongArray,
|
43
43
|
"DOUBLE_ARRAY" => :getDoubleArray,
|
44
|
-
"SFS_OBJECT" => proc { |h, k, v| h[k.to_sym] =
|
44
|
+
"SFS_OBJECT" => proc { |h, k, v| h[k.to_sym] = to_hash(v.object) },
|
45
45
|
"SFS_ARRAY" => proc do |h, k, v|
|
46
|
-
h[k.to_sym] = v.object.iterator.map { |e|
|
46
|
+
h[k.to_sym] = v.object.iterator.map { |e| to_hash(e.object) }
|
47
47
|
end
|
48
48
|
}
|
49
49
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Extends core object with some conversion methods.
|
2
|
+
|
3
|
+
require 'rubyfox/sfsobject/bulk'
|
4
|
+
require 'rubyfox/sfsobject/schema'
|
5
|
+
|
6
|
+
class Hash
|
7
|
+
def to_sfs(schema=:none)
|
8
|
+
if schema == :none
|
9
|
+
Rubyfox::SFSObject::Bulk.to_sfs(self)
|
10
|
+
else
|
11
|
+
Rubyfox::SFSObject::Schema.to_sfs(schema, self)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Rubyfox::SFSObject::Java::SFSObject
|
17
|
+
def to_hash(schema=:none)
|
18
|
+
if schema == :none
|
19
|
+
Rubyfox::SFSObject::Bulk.to_hash(self)
|
20
|
+
else
|
21
|
+
Rubyfox::SFSObject::Schema.to_hash(schema, self)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module Boolean
|
2
|
+
end
|
3
|
+
|
4
|
+
class TrueClass
|
5
|
+
include Boolean
|
6
|
+
end
|
7
|
+
|
8
|
+
class FalseClass
|
9
|
+
include Boolean
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rubyfox/sfsobject/java'
|
13
|
+
|
14
|
+
module Rubyfox
|
15
|
+
module SFSObject
|
16
|
+
class Schema
|
17
|
+
TO_SFS = {
|
18
|
+
String => proc { |o, s, k, v| o.put_utf_string(k, v) },
|
19
|
+
Boolean => proc { |o, s, k, v| o.put_bool(k, v) },
|
20
|
+
Fixnum => proc { |o, s, k, v| o.put_int(k, v) },
|
21
|
+
Float => proc { |o, s, k, v| o.put_double(k, v) },
|
22
|
+
Hash => proc { |o, s, k, v| o.put_sfs_object(k, to_sfs(s, v)) },
|
23
|
+
[String] => proc { |o, s, k, v| o.put_utf_string_array(k, v) },
|
24
|
+
[Boolean] => proc { |o, s, k, v| o.put_bool_array(k, v) },
|
25
|
+
[Fixnum] => proc do |o, s, k, v|
|
26
|
+
collection = Java::ArrayList.new(v.size)
|
27
|
+
v.each { |e| collection.add(e.to_java(:int)) }
|
28
|
+
o.put_int_array(k, collection)
|
29
|
+
end,
|
30
|
+
[Float] => proc { |o, s, k, v| o.put_double_array(k, v) },
|
31
|
+
[Hash] => proc { |o, s, k, v|
|
32
|
+
ary = Java::SFSArray.new
|
33
|
+
schema = s[0]
|
34
|
+
v.each { |e| ary.add_sfs_object(to_sfs(schema, e)) }
|
35
|
+
o.put_sfs_array(k, ary)
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
TO_HASH = {
|
40
|
+
String => proc { |o, s, k| o.get_utf_string(k) },
|
41
|
+
Boolean => proc { |o, s, k| o.get_bool(k) },
|
42
|
+
Fixnum => proc { |o, s, k| o.get_int(k) },
|
43
|
+
Float => proc { |o, s, k| o.get_double(k) },
|
44
|
+
Hash => proc { |o, s, k| to_hash(s, o.get_sfs_object(k)) },
|
45
|
+
[String] => proc { |o, s, k| o.get_utf_string_array(k).to_a },
|
46
|
+
[Boolean] => proc { |o, s, k| o.get_bool_array(k).to_a },
|
47
|
+
[Fixnum] => proc { |o, s, k| o.get_int_array(k).to_a },
|
48
|
+
[Float] => proc { |o, s, k| o.get_double_array(k).to_a },
|
49
|
+
[Hash] => proc { |o, s, k|
|
50
|
+
sfs_ary = o.get_sfs_array(k)
|
51
|
+
ary = []
|
52
|
+
schema = s[0]
|
53
|
+
sfs_ary.size.times do |i|
|
54
|
+
ary << to_hash(schema, sfs_ary.get_sfs_object(i))
|
55
|
+
end
|
56
|
+
ary
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
def initialize(schema={})
|
61
|
+
@schema = schema
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_sfs(hash)
|
65
|
+
self.class.to_sfs(@schema, hash)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.to_sfs(schemas, hash)
|
69
|
+
object = Java::SFSObject.new_instance
|
70
|
+
schemas.each do |key, schema|
|
71
|
+
next unless hash && hash.key?(key)
|
72
|
+
value = hash[key]
|
73
|
+
if method = wrap_method(TO_SFS, schema)
|
74
|
+
method.call(object, schema, key, value)
|
75
|
+
else
|
76
|
+
raise ArgumentError, "wrapper for #{key}=#{value} (#{schema}) not found"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
object
|
80
|
+
end
|
81
|
+
|
82
|
+
def to_hash(object)
|
83
|
+
self.class.to_hash(@schema, object)
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.to_hash(schemas, object)
|
87
|
+
hash = {}
|
88
|
+
schemas.each do |key, schema|
|
89
|
+
next unless object.contains_key(key.to_s)
|
90
|
+
if method = wrap_method(TO_HASH, schema)
|
91
|
+
hash[key] = method.call(object, schema, key)
|
92
|
+
else
|
93
|
+
raise ArgumentError, "unwrapper for #{key}=#{value} (#{schema}) not found"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
hash
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.wrap_method(map, schema)
|
100
|
+
if Hash === schema
|
101
|
+
map[Hash]
|
102
|
+
elsif Array === schema && Hash === schema.first
|
103
|
+
map[[Hash]]
|
104
|
+
else
|
105
|
+
map[schema]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -4,8 +4,6 @@ require 'helper'
|
|
4
4
|
require 'rubyfox/sfsobject/bulk'
|
5
5
|
|
6
6
|
class RubyfoxSFSObjectBulkTest < RubyfoxCase
|
7
|
-
let(:bulk) { Rubyfox::SFSObject::Bulk }
|
8
|
-
|
9
7
|
test "empty" do
|
10
8
|
assert_conversion Hash.new
|
11
9
|
end
|
@@ -20,8 +18,8 @@ class RubyfoxSFSObjectBulkTest < RubyfoxCase
|
|
20
18
|
end
|
21
19
|
|
22
20
|
test "string" do
|
23
|
-
assert_conversion :
|
24
|
-
assert_conversion :
|
21
|
+
assert_conversion :string => "value"
|
22
|
+
assert_conversion :string => "üöäÜÖÄß"
|
25
23
|
end
|
26
24
|
|
27
25
|
test "boolean" do
|
@@ -66,7 +64,7 @@ class RubyfoxSFSObjectBulkTest < RubyfoxCase
|
|
66
64
|
end
|
67
65
|
|
68
66
|
test "boolean" do
|
69
|
-
assert_conversion :bool => [ true,
|
67
|
+
assert_conversion :bool => [ true, false ]
|
70
68
|
end
|
71
69
|
|
72
70
|
test "fixnum" do
|
@@ -86,7 +84,7 @@ class RubyfoxSFSObjectBulkTest < RubyfoxCase
|
|
86
84
|
private
|
87
85
|
|
88
86
|
def assert_conversion(input, output=input)
|
89
|
-
object =
|
90
|
-
assert_equal output,
|
87
|
+
object = Rubyfox::SFSObject::Bulk.to_sfs(input)
|
88
|
+
assert_equal output, Rubyfox::SFSObject::Bulk.to_hash(object)
|
91
89
|
end
|
92
90
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'rubyfox/sfsobject/core_xt'
|
3
|
+
|
4
|
+
class RubyfoxSFSObjectCoreExtTest < RubyfoxCase
|
5
|
+
context "bulk" do
|
6
|
+
test "methods" do
|
7
|
+
hash = { :key => "value"}
|
8
|
+
assert_equal hash, hash.to_sfs.to_hash
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "schema" do
|
13
|
+
test "methods" do
|
14
|
+
schema = { :key => String }
|
15
|
+
hash = { :key => "value" }
|
16
|
+
assert_equal hash, hash.to_sfs(schema).to_hash(schema)
|
17
|
+
end
|
18
|
+
|
19
|
+
test "fail w/o valid schema" do
|
20
|
+
schema = nil
|
21
|
+
hash = { :key => "value" }
|
22
|
+
assert_raises NoMethodError, :message => /each/ do
|
23
|
+
assert_equal hash, hash.to_sfs(schema).to_hash(schema)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
require 'rubyfox/sfsobject/schema'
|
5
|
+
|
6
|
+
class RubyfoxSFSObjectSchemaTest < RubyfoxCase
|
7
|
+
test "empty" do
|
8
|
+
assert_schema({}, {})
|
9
|
+
assert_schema({}, {:key => "value"}, {})
|
10
|
+
end
|
11
|
+
|
12
|
+
context "plain" do
|
13
|
+
test "nil is unknown" do
|
14
|
+
assert_raises ArgumentError, :message => /wrapper for null.*not found/ do
|
15
|
+
assert_schema({ :null => nil }, { :null => "foo" })
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test "string" do
|
20
|
+
assert_schema({ :string => String }, { :string => "value" })
|
21
|
+
assert_schema({ :string => String }, { :string => "üöäÜÖÄß" })
|
22
|
+
end
|
23
|
+
|
24
|
+
test "bool" do
|
25
|
+
assert_schema({ :bool => Boolean }, { :bool => true })
|
26
|
+
assert_schema({ :bool => Boolean }, { :bool => false })
|
27
|
+
end
|
28
|
+
|
29
|
+
test "fixnum" do
|
30
|
+
assert_schema({ :fixnum => Fixnum }, { :fixnum => 1 })
|
31
|
+
assert_schema({ :fixnum => Fixnum }, { :fixnum => (2 ** 31 - 1) })
|
32
|
+
assert_schema({ :fixnum => Fixnum }, { :fixnum => -(2 ** 31) })
|
33
|
+
end
|
34
|
+
|
35
|
+
test "fixnum too big for int" do
|
36
|
+
assert_raises RangeError, :message => /too big for int/ do
|
37
|
+
assert_schema({ :fixnum => Fixnum }, { :fixnum => (2 ** 31) })
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test "float" do
|
42
|
+
assert_schema({ :float => Float }, { :float => 1.0 })
|
43
|
+
assert_schema({ :float => Float }, { :float => 1.0 / 3 })
|
44
|
+
end
|
45
|
+
|
46
|
+
test "nested hash" do
|
47
|
+
assert_schema(
|
48
|
+
{ :sub => { :key => String } },
|
49
|
+
{ :sub => { :key => "value" } }
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "array" do
|
55
|
+
test "nil is unknown" do
|
56
|
+
assert_raises ArgumentError, :message => /wrapper for null.*not found/ do
|
57
|
+
assert_schema({ :null => [nil] }, { :null => "foo" })
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
test "string" do
|
62
|
+
assert_schema({ :string => [String] }, { :string => ["foo", "bar"] })
|
63
|
+
assert_schema({ :string => [String] }, { :string => ["foo", "üöäÜÖÄß"] })
|
64
|
+
assert_schema({ :string => [String] }, { :string => ["foo", 23] }) # strange?!
|
65
|
+
end
|
66
|
+
|
67
|
+
test "bool" do
|
68
|
+
assert_schema({ :bool => [Boolean] }, { :bool => [true, false] })
|
69
|
+
assert_schema({ :bool => [Boolean] }, { :bool => [true, 23] }) # strange?
|
70
|
+
end
|
71
|
+
|
72
|
+
test "fixnum" do
|
73
|
+
assert_schema({ :fixnum => [Fixnum] }, { :fixnum => [1, 2] })
|
74
|
+
assert_schema({ :fixnum => [Fixnum] }, { :fixnum => [(2 ** 31 - 1), -(2 ** 31)] })
|
75
|
+
end
|
76
|
+
|
77
|
+
test "float" do
|
78
|
+
assert_schema({ :float => [Float] }, { :float => [1.0, 1.0 / 3] })
|
79
|
+
end
|
80
|
+
|
81
|
+
test "nested hash" do
|
82
|
+
assert_schema(
|
83
|
+
{ :sub => [ { :key => String } ] },
|
84
|
+
{ :sub => [ { :key => "foo" }, { :key => "bar" }] }
|
85
|
+
)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def assert_schema(schema, input, output=input)
|
92
|
+
object = Rubyfox::SFSObject::Schema.to_sfs(schema, input)
|
93
|
+
hash = Rubyfox::SFSObject::Schema.to_hash(schema, object)
|
94
|
+
assert_equal output, hash
|
95
|
+
end
|
96
|
+
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: rubyfox-sfsobject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: universal-java-1.6
|
7
7
|
authors:
|
8
8
|
- Peter Suschlik
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
@@ -63,13 +63,15 @@ files:
|
|
63
63
|
- Rakefile
|
64
64
|
- lib/rubyfox/sfsobject.rb
|
65
65
|
- lib/rubyfox/sfsobject/bulk.rb
|
66
|
-
- lib/rubyfox/sfsobject/
|
66
|
+
- lib/rubyfox/sfsobject/core_ext.rb
|
67
67
|
- lib/rubyfox/sfsobject/java.rb
|
68
|
+
- lib/rubyfox/sfsobject/schema.rb
|
68
69
|
- lib/rubyfox/sfsobject/version.rb
|
69
70
|
- rubyfox-sfsobject.gemspec
|
70
71
|
- test/helper.rb
|
71
72
|
- test/rubyfox/sfsobject/bulk_test.rb
|
72
|
-
- test/rubyfox/sfsobject/
|
73
|
+
- test/rubyfox/sfsobject/core_ext.rb
|
74
|
+
- test/rubyfox/sfsobject/schema_test.rb
|
73
75
|
homepage: https://github.com/neopoly/rubyfox-sfsobject
|
74
76
|
licenses: []
|
75
77
|
post_install_message:
|
@@ -105,4 +107,5 @@ summary: ''
|
|
105
107
|
test_files:
|
106
108
|
- test/helper.rb
|
107
109
|
- test/rubyfox/sfsobject/bulk_test.rb
|
108
|
-
- test/rubyfox/sfsobject/
|
110
|
+
- test/rubyfox/sfsobject/core_ext.rb
|
111
|
+
- test/rubyfox/sfsobject/schema_test.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# Extends core object with some conversion methods.
|
2
|
-
|
3
|
-
require 'rubyfox/sfsobject/bulk'
|
4
|
-
|
5
|
-
class Hash
|
6
|
-
def to_sfs
|
7
|
-
Rubyfox::SFSObject::Bulk.to_sfs(self)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class Rubyfox::SFSObject::Java::SFSObject
|
12
|
-
def to_hash
|
13
|
-
Rubyfox::SFSObject::Bulk.to_hash(self)
|
14
|
-
end
|
15
|
-
end
|