rubyfox-sfsobject 0.0.1-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/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +10 -0
- data/lib/rubyfox/sfsobject/bulk.rb +101 -0
- data/lib/rubyfox/sfsobject/extend.rb +15 -0
- data/lib/rubyfox/sfsobject/java.rb +11 -0
- data/lib/rubyfox/sfsobject/version.rb +5 -0
- data/lib/rubyfox/sfsobject.rb +17 -0
- data/rubyfox-sfsobject.gemspec +23 -0
- data/test/helper.rb +18 -0
- data/test/rubyfox/sfsobject/bulk_test.rb +92 -0
- data/test/rubyfox/sfsobject/extend_test.rb +9 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm jruby@rubyfox-sfsobject --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Rubyfox::SFSObject
|
2
|
+
|
3
|
+
Converts between SmartFox's SFSObjects and Ruby Hashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rubyfox-sfsobject'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rubyfox-sfsobject
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'rubyfox/sfsobject/bulk'
|
22
|
+
sfs_object = Rubyfox::SFSObject::Bulk.to_sfs({ :hello => "world" })
|
23
|
+
# => SFSObject ready to use in SmartFox
|
24
|
+
hash = Rubyfox::SFSObject::Bulk.to.hash(sfs_object)
|
25
|
+
# => { :hello => "world" }
|
26
|
+
|
27
|
+
|
28
|
+
You can extend Hash and SFSObject with method shortcuts:
|
29
|
+
|
30
|
+
require 'rubyfox/sfsobject/extend'
|
31
|
+
sfs_object = { :hello => "world" }.to_sfs
|
32
|
+
# => SFSObject
|
33
|
+
sfs_object.to_hash
|
34
|
+
# { :hello => "world" }
|
35
|
+
|
36
|
+
## Caveats
|
37
|
+
|
38
|
+
*Note* that all hash keys will be converted to symbols.
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'rubyfox/sfsobject/java'
|
2
|
+
|
3
|
+
module Rubyfox
|
4
|
+
module SFSObject
|
5
|
+
module Bulk
|
6
|
+
extend self
|
7
|
+
|
8
|
+
TO_SFS = {
|
9
|
+
NilClass => proc { |o, k, _| o.putNull(k) },
|
10
|
+
String => :putUtfString,
|
11
|
+
TrueClass => :putBool,
|
12
|
+
FalseClass => :putBool,
|
13
|
+
Fixnum => :putInt,
|
14
|
+
Float => :putDouble,
|
15
|
+
Hash => proc { |o, k, v| o.putSFSObject(k, Rubyfox::SFSObject::Bulk.to_sfs(v)) },
|
16
|
+
[String] => :putUtfStringArray,
|
17
|
+
[TrueClass] => :putBoolArray,
|
18
|
+
[FalseClass] => :putBoolArray,
|
19
|
+
[Fixnum] => proc do |o, k, v|
|
20
|
+
collection = Java::ArrayList.new
|
21
|
+
v.each { |e| collection.add(e.to_java(:int)) }
|
22
|
+
o.putIntArray(k, collection)
|
23
|
+
end,
|
24
|
+
[Float] => :putDoubleArray,
|
25
|
+
[Hash] => proc do |o, k, v|
|
26
|
+
ary = Java::SFSArray.new
|
27
|
+
v.each { |e| ary.addSFSObject(Rubyfox::SFSObject::Bulk.to_sfs(e)) }
|
28
|
+
o.putSFSArray(k, ary)
|
29
|
+
end
|
30
|
+
}
|
31
|
+
|
32
|
+
TO_HASH = {
|
33
|
+
"NULL" => proc { |h, k, v| h[k.to_sym] = nil },
|
34
|
+
"UTF_STRING" => :getUtfString,
|
35
|
+
"BOOL" => :getBool,
|
36
|
+
"INT" => :getInt,
|
37
|
+
"DOUBLE" => :getDouble,
|
38
|
+
"UTF_STRING_ARRAY" => :getUtfStringArray,
|
39
|
+
"BOOL_ARRAY" => :getBoolArray,
|
40
|
+
#"INT_ARRAY" => :getIntArray,
|
41
|
+
"INT_ARRAY" => proc { |h, k, v| h[k.to_sym] = v.object.to_a },
|
42
|
+
"LONG_ARRAY" => :getLongArray,
|
43
|
+
"DOUBLE_ARRAY" => :getDoubleArray,
|
44
|
+
"SFS_OBJECT" => proc { |h, k, v| h[k.to_sym] = Rubyfox::SFSObject::Bulk.to_hash(v.object) },
|
45
|
+
"SFS_ARRAY" => proc do |h, k, v|
|
46
|
+
h[k.to_sym] = v.object.iterator.map { |e| Rubyfox::SFSObject::Bulk.to_hash(e.object) }
|
47
|
+
end
|
48
|
+
}
|
49
|
+
|
50
|
+
# hash -> object
|
51
|
+
def to_sfs(hash={}, schema=nil)
|
52
|
+
object = Java::SFSObject.new_instance
|
53
|
+
hash.each do |key, value|
|
54
|
+
if wrapper_method = _wrapper(value)
|
55
|
+
case wrapper_method
|
56
|
+
when Symbol
|
57
|
+
object.send(wrapper_method, key, value)
|
58
|
+
else
|
59
|
+
wrapper_method.call(object, key, value)
|
60
|
+
end
|
61
|
+
else
|
62
|
+
raise ArgumentError, "wrapper for #{key}=#{value} (#{value.class}) not found"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
object
|
66
|
+
end
|
67
|
+
|
68
|
+
def _wrapper(value)
|
69
|
+
case value
|
70
|
+
when Array
|
71
|
+
TO_SFS[[value.first.class]]
|
72
|
+
else
|
73
|
+
TO_SFS[value.class]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# object -> hash
|
78
|
+
def to_hash(object)
|
79
|
+
hash = {}
|
80
|
+
object.keys.each do |key|
|
81
|
+
value = object.get(key)
|
82
|
+
if wrapper_method = _unwrapper(value)
|
83
|
+
case wrapper_method
|
84
|
+
when Symbol
|
85
|
+
hash[key.to_sym] = object.send(wrapper_method, key)
|
86
|
+
else
|
87
|
+
wrapper_method.call(hash, key, value)
|
88
|
+
end
|
89
|
+
else
|
90
|
+
raise ArgumentError, "unwrapper for #{key}=#{value.object.inspect} (#{value.type_id}) not found"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
hash
|
94
|
+
end
|
95
|
+
|
96
|
+
def _unwrapper(value)
|
97
|
+
TO_HASH[value.type_id.to_s]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,15 @@
|
|
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
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module Rubyfox
|
4
|
+
module SFSObject
|
5
|
+
def self.boot!(sf_dir)
|
6
|
+
unless $LOAD_PATH.include?(sf_dir)
|
7
|
+
path = Pathname.new(sf_dir).join("*.jar")
|
8
|
+
jars = Dir[path].to_a
|
9
|
+
unless jars.empty?
|
10
|
+
jars.each { |jar| require jar }
|
11
|
+
else
|
12
|
+
raise LoadError, "No jars found in #{path}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rubyfox/sfsobject/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rubyfox-sfsobject"
|
8
|
+
gem.version = Rubyfox::SFSObject::VERSION
|
9
|
+
gem.authors = ["Peter Suschlik"]
|
10
|
+
gem.email = ["ps@neopoly.de"]
|
11
|
+
gem.description = %q{Map SFSObject into Ruby Hash}
|
12
|
+
gem.summary = %q{}
|
13
|
+
gem.homepage = "https://github.com/neopoly/rubyfox-sfsobject"
|
14
|
+
gem.platform = Gem::Platform::CURRENT
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/).reject { |file| file =~ %r{test/vendor} }
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_development_dependency 'minitest'
|
22
|
+
gem.add_development_dependency 'simple_assertions'
|
23
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'simple_assertions'
|
3
|
+
|
4
|
+
require 'rubyfox/sfsobject'
|
5
|
+
|
6
|
+
ENV['SF_DIR'] ||= File.join(File.dirname(__FILE__), 'vendor', 'smartfox')
|
7
|
+
Rubyfox::SFSObject.boot!(ENV['SF_DIR'] + "/lib")
|
8
|
+
|
9
|
+
class RubyfoxCase < MiniTest::Spec
|
10
|
+
include SimpleAssertions::AssertRaises
|
11
|
+
|
12
|
+
class << self
|
13
|
+
alias :setup :before
|
14
|
+
alias :teardown :after
|
15
|
+
alias :context :describe
|
16
|
+
alias :test :it
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
require 'rubyfox/sfsobject/bulk'
|
5
|
+
|
6
|
+
class RubyfoxSFSObjectBulkTest < RubyfoxCase
|
7
|
+
let(:bulk) { Rubyfox::SFSObject::Bulk }
|
8
|
+
|
9
|
+
test "empty" do
|
10
|
+
assert_conversion Hash.new
|
11
|
+
end
|
12
|
+
|
13
|
+
test "converts keys to symbols" do
|
14
|
+
assert_conversion({ "key" => nil }, { :key => nil })
|
15
|
+
end
|
16
|
+
|
17
|
+
context "plain" do
|
18
|
+
test "nil" do
|
19
|
+
assert_conversion :null => nil
|
20
|
+
end
|
21
|
+
|
22
|
+
test "string" do
|
23
|
+
assert_conversion :key => "value"
|
24
|
+
assert_conversion :key => "üöäÜÖÄß"
|
25
|
+
end
|
26
|
+
|
27
|
+
test "boolean" do
|
28
|
+
assert_conversion :true => true, :false => false
|
29
|
+
end
|
30
|
+
|
31
|
+
test "fixnum" do
|
32
|
+
assert_conversion :fixnum => 1
|
33
|
+
assert_conversion :fixnum => (2 ** 31 - 1)
|
34
|
+
assert_conversion :fixnum => -(2 ** 31)
|
35
|
+
end
|
36
|
+
|
37
|
+
test "fixnum too big for int" do
|
38
|
+
assert_raises RangeError, :message => /too big for int/ do
|
39
|
+
assert_conversion :fixnum => (2 ** 31)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
test "cannot handle bignum" do
|
44
|
+
assert_raises ArgumentError, :message => /Bignum/ do
|
45
|
+
assert_conversion :fixnum => (2 ** 63)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
test "float" do
|
50
|
+
assert_conversion :float => 1.0
|
51
|
+
assert_conversion :float => 1.0 / 3
|
52
|
+
end
|
53
|
+
|
54
|
+
test "sub hashes" do
|
55
|
+
assert_conversion :sub => { :key => "value" }
|
56
|
+
assert_conversion :sub => { :deep => { :key => "value" } }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "array" do
|
61
|
+
# TODO nil array?
|
62
|
+
|
63
|
+
test "string" do
|
64
|
+
assert_conversion :string => ["foo", "bar"]
|
65
|
+
assert_conversion :string => ["Föhn", "BÄR"]
|
66
|
+
end
|
67
|
+
|
68
|
+
test "boolean" do
|
69
|
+
assert_conversion :bool => [ true, true, false ]
|
70
|
+
end
|
71
|
+
|
72
|
+
test "fixnum" do
|
73
|
+
assert_conversion :fixnum => [ 1, 2, 3 ]
|
74
|
+
end
|
75
|
+
|
76
|
+
test "float" do
|
77
|
+
assert_conversion :float => [ 1.0, 1.0 / 3 ]
|
78
|
+
end
|
79
|
+
|
80
|
+
test "sub hashes" do
|
81
|
+
assert_conversion :sub => [{ :key => "value" }]
|
82
|
+
assert_conversion :sub => [{ :deep => [{ :key => "value" }] }]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def assert_conversion(input, output=input)
|
89
|
+
object = bulk.to_sfs(input)
|
90
|
+
assert_equal output, bulk.to_hash(object)
|
91
|
+
end
|
92
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyfox-sfsobject
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: universal-java-1.6
|
7
|
+
authors:
|
8
|
+
- Peter Suschlik
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ! '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: !binary |-
|
21
|
+
MA==
|
22
|
+
none: false
|
23
|
+
requirement: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ! '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: !binary |-
|
28
|
+
MA==
|
29
|
+
none: false
|
30
|
+
prerelease: false
|
31
|
+
type: :development
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: simple_assertions
|
34
|
+
version_requirements: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: !binary |-
|
39
|
+
MA==
|
40
|
+
none: false
|
41
|
+
requirement: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: !binary |-
|
46
|
+
MA==
|
47
|
+
none: false
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
description: Map SFSObject into Ruby Hash
|
51
|
+
email:
|
52
|
+
- ps@neopoly.de
|
53
|
+
executables: []
|
54
|
+
extensions: []
|
55
|
+
extra_rdoc_files: []
|
56
|
+
files:
|
57
|
+
- .gitignore
|
58
|
+
- .rvmrc
|
59
|
+
- .travis.yml
|
60
|
+
- Gemfile
|
61
|
+
- LICENSE.txt
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- lib/rubyfox/sfsobject.rb
|
65
|
+
- lib/rubyfox/sfsobject/bulk.rb
|
66
|
+
- lib/rubyfox/sfsobject/extend.rb
|
67
|
+
- lib/rubyfox/sfsobject/java.rb
|
68
|
+
- lib/rubyfox/sfsobject/version.rb
|
69
|
+
- rubyfox-sfsobject.gemspec
|
70
|
+
- test/helper.rb
|
71
|
+
- test/rubyfox/sfsobject/bulk_test.rb
|
72
|
+
- test/rubyfox/sfsobject/extend_test.rb
|
73
|
+
homepage: https://github.com/neopoly/rubyfox-sfsobject
|
74
|
+
licenses: []
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: 2
|
86
|
+
version: !binary |-
|
87
|
+
MA==
|
88
|
+
none: false
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: 2
|
96
|
+
version: !binary |-
|
97
|
+
MA==
|
98
|
+
none: false
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: ''
|
105
|
+
test_files:
|
106
|
+
- test/helper.rb
|
107
|
+
- test/rubyfox/sfsobject/bulk_test.rb
|
108
|
+
- test/rubyfox/sfsobject/extend_test.rb
|