koala 1.8.0 → 1.9.0rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/Gemfile +5 -9
- data/changelog.md +22 -3
- data/koala.gemspec +0 -2
- data/lib/koala.rb +5 -0
- data/lib/koala/api.rb +12 -2
- data/lib/koala/api/graph_api.rb +4 -1
- data/lib/koala/version.rb +1 -1
- data/readme.md +17 -4
- data/spec/cases/api_spec.rb +81 -24
- data/spec/cases/error_spec.rb +16 -16
- data/spec/cases/graph_api_batch_spec.rb +103 -103
- data/spec/cases/graph_api_spec.rb +33 -10
- data/spec/cases/graph_collection_spec.rb +35 -35
- data/spec/cases/http_service_spec.rb +92 -92
- data/spec/cases/koala_spec.rb +9 -9
- data/spec/cases/legacy_spec.rb +22 -22
- data/spec/cases/multipart_request_spec.rb +20 -21
- data/spec/cases/oauth_spec.rb +125 -125
- data/spec/cases/realtime_updates_spec.rb +44 -44
- data/spec/cases/test_users_spec.rb +58 -58
- data/spec/cases/uploadable_io_spec.rb +36 -36
- data/spec/cases/utils_spec.rb +11 -11
- data/spec/spec_helper.rb +0 -19
- data/spec/support/custom_matchers.rb +3 -3
- data/spec/support/graph_api_shared_examples.rb +117 -114
- data/spec/support/koala_test.rb +3 -8
- data/spec/support/rest_api_shared_examples.rb +18 -19
- data/spec/support/uploadable_io_shared_examples.rb +10 -10
- metadata +20 -50
- data/spec/support/ordered_hash.rb +0 -201
data/spec/support/koala_test.rb
CHANGED
@@ -55,17 +55,12 @@ module KoalaTest
|
|
55
55
|
RSpec.configure do |config|
|
56
56
|
config.before :each do
|
57
57
|
@token = KoalaTest.oauth_token
|
58
|
-
Koala::Utils.
|
58
|
+
allow(Koala::Utils).to receive(:deprecate) # never fire deprecation warnings
|
59
|
+
# Clean up Koala config
|
60
|
+
Koala.reset_config
|
59
61
|
end
|
60
62
|
|
61
63
|
config.after :each do
|
62
|
-
# Clean up Koala config
|
63
|
-
Koala.configure do |config|
|
64
|
-
Koala::HTTPService::DEFAULT_SERVERS.each_pair do |k, v|
|
65
|
-
config.send("#{k}=", v)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
64
|
# if we're working with a real user, clean up any objects posted to Facebook
|
70
65
|
# no need to do so for test users, since they get deleted at the end
|
71
66
|
if @temporary_object_id && KoalaTest.real_user?
|
@@ -3,7 +3,7 @@ shared_examples_for "Koala RestAPI" do
|
|
3
3
|
describe "when making a rest request" do
|
4
4
|
it "uses the proper path" do
|
5
5
|
method = double('methodName')
|
6
|
-
@api.
|
6
|
+
expect(@api).to receive(:api).with(
|
7
7
|
"method/#{method}",
|
8
8
|
anything,
|
9
9
|
anything,
|
@@ -14,7 +14,7 @@ shared_examples_for "Koala RestAPI" do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "always uses the rest api" do
|
17
|
-
@api.
|
17
|
+
expect(@api).to receive(:api).with(
|
18
18
|
anything,
|
19
19
|
anything,
|
20
20
|
anything,
|
@@ -27,7 +27,7 @@ shared_examples_for "Koala RestAPI" do
|
|
27
27
|
it "sets the read_only option to true if the method is listed in the read-only list" do
|
28
28
|
method = Koala::Facebook::RestAPI::READ_ONLY_METHODS.first
|
29
29
|
|
30
|
-
@api.
|
30
|
+
expect(@api).to receive(:api).with(
|
31
31
|
anything,
|
32
32
|
anything,
|
33
33
|
anything,
|
@@ -40,7 +40,7 @@ shared_examples_for "Koala RestAPI" do
|
|
40
40
|
it "sets the read_only option to false if the method is not inthe read-only list" do
|
41
41
|
method = "I'm not a read-only method"
|
42
42
|
|
43
|
-
@api.
|
43
|
+
expect(@api).to receive(:api).with(
|
44
44
|
anything,
|
45
45
|
anything,
|
46
46
|
anything,
|
@@ -54,7 +54,7 @@ shared_examples_for "Koala RestAPI" do
|
|
54
54
|
it "takes an optional hash of arguments" do
|
55
55
|
args = {:arg1 => 'arg1'}
|
56
56
|
|
57
|
-
@api.
|
57
|
+
expect(@api).to receive(:api).with(
|
58
58
|
anything,
|
59
59
|
hash_including(args),
|
60
60
|
anything,
|
@@ -65,7 +65,7 @@ shared_examples_for "Koala RestAPI" do
|
|
65
65
|
end
|
66
66
|
|
67
67
|
it "always asks for JSON" do
|
68
|
-
@api.
|
68
|
+
expect(@api).to receive(:api).with(
|
69
69
|
anything,
|
70
70
|
hash_including('format' => 'json'),
|
71
71
|
anything,
|
@@ -78,7 +78,7 @@ shared_examples_for "Koala RestAPI" do
|
|
78
78
|
it "passes any options provided to the API" do
|
79
79
|
options = {:a => 2}
|
80
80
|
|
81
|
-
@api.
|
81
|
+
expect(@api).to receive(:api).with(
|
82
82
|
anything,
|
83
83
|
hash_including('format' => 'json'),
|
84
84
|
anything,
|
@@ -89,7 +89,7 @@ shared_examples_for "Koala RestAPI" do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
it "uses get by default" do
|
92
|
-
@api.
|
92
|
+
expect(@api).to receive(:api).with(
|
93
93
|
anything,
|
94
94
|
anything,
|
95
95
|
"get",
|
@@ -101,7 +101,7 @@ shared_examples_for "Koala RestAPI" do
|
|
101
101
|
|
102
102
|
it "allows you to specify other http methods as the last argument" do
|
103
103
|
method = 'bar'
|
104
|
-
@api.
|
104
|
+
expect(@api).to receive(:api).with(
|
105
105
|
anything,
|
106
106
|
anything,
|
107
107
|
method,
|
@@ -112,8 +112,8 @@ shared_examples_for "Koala RestAPI" do
|
|
112
112
|
end
|
113
113
|
|
114
114
|
it "throws an APIError if the status code >= 400" do
|
115
|
-
Koala.
|
116
|
-
|
115
|
+
allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(500, '{"error_code": "An error occurred!"}', {}))
|
116
|
+
expect { @api.rest_call(KoalaTest.user1, {}) }.to raise_exception(Koala::Facebook::APIError)
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
@@ -126,29 +126,29 @@ shared_examples_for "Koala RestAPI with an access token" do
|
|
126
126
|
describe "#set_app_properties" do
|
127
127
|
it "sends Facebook the properties JSON-encoded as :properties" do
|
128
128
|
props = {:a => 2, :c => [1, 2, "d"]}
|
129
|
-
@api.
|
129
|
+
expect(@api).to receive(:rest_call).with(anything, hash_including(:properties => MultiJson.dump(props)), anything, anything)
|
130
130
|
@api.set_app_properties(props)
|
131
131
|
end
|
132
132
|
|
133
133
|
it "calls the admin.setAppProperties method" do
|
134
|
-
@api.
|
134
|
+
expect(@api).to receive(:rest_call).with("admin.setAppProperties", anything, anything, anything)
|
135
135
|
@api.set_app_properties({})
|
136
136
|
end
|
137
137
|
|
138
138
|
it "includes any other provided arguments" do
|
139
139
|
args = {:c => 3, :d => "a"}
|
140
|
-
@api.
|
140
|
+
expect(@api).to receive(:rest_call).with(anything, hash_including(args), anything, anything)
|
141
141
|
@api.set_app_properties({:a => 2}, args)
|
142
142
|
end
|
143
143
|
|
144
144
|
it "includes any http_options provided" do
|
145
145
|
opts = {:c => 3, :d => "a"}
|
146
|
-
@api.
|
146
|
+
expect(@api).to receive(:rest_call).with(anything, anything, opts, anything)
|
147
147
|
@api.set_app_properties({}, {}, opts)
|
148
148
|
end
|
149
149
|
|
150
150
|
it "makes a POST" do
|
151
|
-
@api.
|
151
|
+
expect(@api).to receive(:rest_call).with(anything, anything, anything, "post")
|
152
152
|
@api.set_app_properties({})
|
153
153
|
end
|
154
154
|
|
@@ -156,14 +156,13 @@ shared_examples_for "Koala RestAPI with an access token" do
|
|
156
156
|
oauth = Koala::Facebook::OAuth.new(KoalaTest.app_id, KoalaTest.secret)
|
157
157
|
app_token = oauth.get_app_access_token
|
158
158
|
@app_api = Koala::Facebook::API.new(app_token)
|
159
|
-
@app_api.set_app_properties(KoalaTest.app_properties).
|
159
|
+
expect(@app_api.set_app_properties(KoalaTest.app_properties)).to be_truthy
|
160
160
|
end
|
161
161
|
end
|
162
162
|
end
|
163
163
|
|
164
|
-
|
165
164
|
shared_examples_for "Koala RestAPI without an access token" do
|
166
165
|
it "can't use set_app_properties" do
|
167
|
-
|
166
|
+
expect { @api.set_app_properties(:desktop => 0) }.to raise_error(Koala::Facebook::AuthenticationError)
|
168
167
|
end
|
169
168
|
end
|
@@ -8,21 +8,21 @@ shared_examples_for "MIME::Types can't return results" do
|
|
8
8
|
it "should properly get content types for #{extension} using basic analysis" do
|
9
9
|
path = "filename.#{extension}"
|
10
10
|
if @koala_io_params[0].is_a?(File)
|
11
|
-
@koala_io_params[0].
|
11
|
+
allow(@koala_io_params[0]).to receive(:path).and_return(path)
|
12
12
|
else
|
13
13
|
@koala_io_params[0] = path
|
14
14
|
end
|
15
|
-
Koala::UploadableIO.new(*@koala_io_params).content_type.
|
15
|
+
expect(Koala::UploadableIO.new(*@koala_io_params).content_type).to eq(mime_type)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should get content types for #{extension} using basic analysis with file names with more than one dot" do
|
19
19
|
path = "file.name.#{extension}"
|
20
20
|
if @koala_io_params[0].is_a?(File)
|
21
|
-
@koala_io_params[0].
|
21
|
+
allow(@koala_io_params[0]).to receive(:path).and_return(path)
|
22
22
|
else
|
23
23
|
@koala_io_params[0] = path
|
24
24
|
end
|
25
|
-
Koala::UploadableIO.new(*@koala_io_params).content_type.
|
25
|
+
expect(Koala::UploadableIO.new(*@koala_io_params).content_type).to eq(mime_type)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -30,14 +30,14 @@ shared_examples_for "MIME::Types can't return results" do
|
|
30
30
|
before :each do
|
31
31
|
path = "badfile.badextension"
|
32
32
|
if @koala_io_params[0].is_a?(File)
|
33
|
-
@koala_io_params[0].
|
33
|
+
allow(@koala_io_params[0]).to receive(:path).and_return(path)
|
34
34
|
else
|
35
35
|
@koala_io_params[0] = path
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should throw an exception" do
|
40
|
-
|
40
|
+
expect { Koala::UploadableIO.new(*@koala_io_params) }.to raise_exception(Koala::KoalaError)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -46,15 +46,15 @@ shared_examples_for "determining a mime type" do
|
|
46
46
|
describe "if MIME::Types is available" do
|
47
47
|
it "should return an UploadIO with MIME::Types-determined type if the type exists" do
|
48
48
|
type_result = ["type"]
|
49
|
-
Koala::MIME::Types.
|
50
|
-
Koala::UploadableIO.new(*@koala_io_params).content_type.
|
49
|
+
allow(Koala::MIME::Types).to receive(:type_for).and_return(type_result)
|
50
|
+
expect(Koala::UploadableIO.new(*@koala_io_params).content_type).to eq(type_result.first)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe "if MIME::Types is unavailable" do
|
55
55
|
before :each do
|
56
56
|
# fake that MIME::Types doesn't exist
|
57
|
-
Koala::MIME::Types.
|
57
|
+
allow(Koala::MIME::Types).to receive(:type_for).and_raise(NameError)
|
58
58
|
end
|
59
59
|
it_should_behave_like "MIME::Types can't return results"
|
60
60
|
end
|
@@ -62,7 +62,7 @@ shared_examples_for "determining a mime type" do
|
|
62
62
|
describe "if MIME::Types can't find the result" do
|
63
63
|
before :each do
|
64
64
|
# fake that MIME::Types doesn't exist
|
65
|
-
Koala::MIME::Types.
|
65
|
+
allow(Koala::MIME::Types).to receive(:type_for).and_return([])
|
66
66
|
end
|
67
67
|
|
68
68
|
it_should_behave_like "MIME::Types can't return results"
|
metadata
CHANGED
@@ -1,83 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: koala
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Koppel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01
|
11
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: faraday
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: addressable
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rspec
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - '>='
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rake
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - '>='
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - '>='
|
52
|
+
- - ">="
|
81
53
|
- !ruby/object:Gem::Version
|
82
54
|
version: '0'
|
83
55
|
description: Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write
|
@@ -92,11 +64,11 @@ extra_rdoc_files:
|
|
92
64
|
- readme.md
|
93
65
|
- changelog.md
|
94
66
|
files:
|
95
|
-
- .autotest
|
96
|
-
- .gitignore
|
97
|
-
- .rspec
|
98
|
-
- .travis.yml
|
99
|
-
- .yardopts
|
67
|
+
- ".autotest"
|
68
|
+
- ".gitignore"
|
69
|
+
- ".rspec"
|
70
|
+
- ".travis.yml"
|
71
|
+
- ".yardopts"
|
100
72
|
- Gemfile
|
101
73
|
- Guardfile
|
102
74
|
- LICENSE
|
@@ -148,7 +120,6 @@ files:
|
|
148
120
|
- spec/support/graph_api_shared_examples.rb
|
149
121
|
- spec/support/koala_test.rb
|
150
122
|
- spec/support/mock_http_service.rb
|
151
|
-
- spec/support/ordered_hash.rb
|
152
123
|
- spec/support/rest_api_shared_examples.rb
|
153
124
|
- spec/support/uploadable_io_shared_examples.rb
|
154
125
|
homepage: http://github.com/arsduo/koala
|
@@ -156,25 +127,25 @@ licenses: []
|
|
156
127
|
metadata: {}
|
157
128
|
post_install_message:
|
158
129
|
rdoc_options:
|
159
|
-
- --line-numbers
|
160
|
-
- --inline-source
|
161
|
-
- --title
|
130
|
+
- "--line-numbers"
|
131
|
+
- "--inline-source"
|
132
|
+
- "--title"
|
162
133
|
- Koala
|
163
134
|
require_paths:
|
164
135
|
- lib
|
165
136
|
required_ruby_version: !ruby/object:Gem::Requirement
|
166
137
|
requirements:
|
167
|
-
- -
|
138
|
+
- - ">="
|
168
139
|
- !ruby/object:Gem::Version
|
169
140
|
version: '0'
|
170
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
142
|
requirements:
|
172
|
-
- -
|
143
|
+
- - ">"
|
173
144
|
- !ruby/object:Gem::Version
|
174
|
-
version:
|
145
|
+
version: 1.3.1
|
175
146
|
requirements: []
|
176
147
|
rubyforge_project:
|
177
|
-
rubygems_version: 2.
|
148
|
+
rubygems_version: 2.2.1
|
178
149
|
signing_key:
|
179
150
|
specification_version: 4
|
180
151
|
summary: A lightweight, flexible library for Facebook with support for the Graph API,
|
@@ -204,7 +175,6 @@ test_files:
|
|
204
175
|
- spec/support/graph_api_shared_examples.rb
|
205
176
|
- spec/support/koala_test.rb
|
206
177
|
- spec/support/mock_http_service.rb
|
207
|
-
- spec/support/ordered_hash.rb
|
208
178
|
- spec/support/rest_api_shared_examples.rb
|
209
179
|
- spec/support/uploadable_io_shared_examples.rb
|
210
180
|
has_rdoc:
|
@@ -1,201 +0,0 @@
|
|
1
|
-
module KoalaTest
|
2
|
-
# directly taken from Rails 3.1's OrderedHash
|
3
|
-
# see https://github.com/rails/rails/blob/master/activesupport/lib/active_support/ordered_hash.rb
|
4
|
-
|
5
|
-
# The order of iteration over hashes in Ruby 1.8 is undefined. For example, you do not know the
|
6
|
-
# order in which +keys+ will return keys, or +each+ yield pairs. <tt>ActiveSupport::OrderedHash</tt>
|
7
|
-
# implements a hash that preserves insertion order, as in Ruby 1.9:
|
8
|
-
#
|
9
|
-
# oh = ActiveSupport::OrderedHash.new
|
10
|
-
# oh[:a] = 1
|
11
|
-
# oh[:b] = 2
|
12
|
-
# oh.keys # => [:a, :b], this order is guaranteed
|
13
|
-
#
|
14
|
-
# <tt>ActiveSupport::OrderedHash</tt> is namespaced to prevent conflicts with other implementations.
|
15
|
-
class OrderedHash < ::Hash #:nodoc:
|
16
|
-
def to_yaml_type
|
17
|
-
"!tag:yaml.org,2002:omap"
|
18
|
-
end
|
19
|
-
|
20
|
-
def encode_with(coder)
|
21
|
-
coder.represent_seq '!omap', map { |k,v| { k => v } }
|
22
|
-
end
|
23
|
-
|
24
|
-
def to_yaml(opts = {})
|
25
|
-
if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
|
26
|
-
return super
|
27
|
-
end
|
28
|
-
|
29
|
-
YAML.quick_emit(self, opts) do |out|
|
30
|
-
out.seq(taguri) do |seq|
|
31
|
-
each do |k, v|
|
32
|
-
seq.add(k => v)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def nested_under_indifferent_access
|
39
|
-
self
|
40
|
-
end
|
41
|
-
|
42
|
-
# Hash is ordered in Ruby 1.9!
|
43
|
-
if RUBY_VERSION < '1.9'
|
44
|
-
|
45
|
-
# In MRI the Hash class is core and written in C. In particular, methods are
|
46
|
-
# programmed with explicit C function calls and polymorphism is not honored.
|
47
|
-
#
|
48
|
-
# For example, []= is crucial in this implementation to maintain the @keys
|
49
|
-
# array but hash.c invokes rb_hash_aset() originally. This prevents method
|
50
|
-
# reuse through inheritance and forces us to reimplement stuff.
|
51
|
-
#
|
52
|
-
# For instance, we cannot use the inherited #merge! because albeit the algorithm
|
53
|
-
# itself would work, our []= is not being called at all by the C code.
|
54
|
-
|
55
|
-
def initialize(*args, &block)
|
56
|
-
super
|
57
|
-
@keys = []
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.[](*args)
|
61
|
-
ordered_hash = new
|
62
|
-
|
63
|
-
if (args.length == 1 && args.first.is_a?(Array))
|
64
|
-
args.first.each do |key_value_pair|
|
65
|
-
next unless (key_value_pair.is_a?(Array))
|
66
|
-
ordered_hash[key_value_pair[0]] = key_value_pair[1]
|
67
|
-
end
|
68
|
-
|
69
|
-
return ordered_hash
|
70
|
-
end
|
71
|
-
|
72
|
-
unless (args.size % 2 == 0)
|
73
|
-
raise ArgumentError.new("odd number of arguments for Hash")
|
74
|
-
end
|
75
|
-
|
76
|
-
args.each_with_index do |val, ind|
|
77
|
-
next if (ind % 2 != 0)
|
78
|
-
ordered_hash[val] = args[ind + 1]
|
79
|
-
end
|
80
|
-
|
81
|
-
ordered_hash
|
82
|
-
end
|
83
|
-
|
84
|
-
def initialize_copy(other)
|
85
|
-
super
|
86
|
-
# make a deep copy of keys
|
87
|
-
@keys = other.keys
|
88
|
-
end
|
89
|
-
|
90
|
-
def []=(key, value)
|
91
|
-
@keys << key unless has_key?(key)
|
92
|
-
super
|
93
|
-
end
|
94
|
-
|
95
|
-
def delete(key)
|
96
|
-
if has_key? key
|
97
|
-
index = @keys.index(key)
|
98
|
-
@keys.delete_at index
|
99
|
-
end
|
100
|
-
super
|
101
|
-
end
|
102
|
-
|
103
|
-
def delete_if
|
104
|
-
super
|
105
|
-
sync_keys!
|
106
|
-
self
|
107
|
-
end
|
108
|
-
|
109
|
-
def reject!
|
110
|
-
super
|
111
|
-
sync_keys!
|
112
|
-
self
|
113
|
-
end
|
114
|
-
|
115
|
-
def reject(&block)
|
116
|
-
dup.reject!(&block)
|
117
|
-
end
|
118
|
-
|
119
|
-
def keys
|
120
|
-
@keys.dup
|
121
|
-
end
|
122
|
-
|
123
|
-
def values
|
124
|
-
@keys.collect { |key| self[key] }
|
125
|
-
end
|
126
|
-
|
127
|
-
def to_hash
|
128
|
-
self
|
129
|
-
end
|
130
|
-
|
131
|
-
def to_a
|
132
|
-
@keys.map { |key| [ key, self[key] ] }
|
133
|
-
end
|
134
|
-
|
135
|
-
def each_key
|
136
|
-
return to_enum(:each_key) unless block_given?
|
137
|
-
@keys.each { |key| yield key }
|
138
|
-
self
|
139
|
-
end
|
140
|
-
|
141
|
-
def each_value
|
142
|
-
return to_enum(:each_value) unless block_given?
|
143
|
-
@keys.each { |key| yield self[key]}
|
144
|
-
self
|
145
|
-
end
|
146
|
-
|
147
|
-
def each
|
148
|
-
return to_enum(:each) unless block_given?
|
149
|
-
@keys.each {|key| yield [key, self[key]]}
|
150
|
-
self
|
151
|
-
end
|
152
|
-
|
153
|
-
alias_method :each_pair, :each
|
154
|
-
|
155
|
-
alias_method :select, :find_all
|
156
|
-
|
157
|
-
def clear
|
158
|
-
super
|
159
|
-
@keys.clear
|
160
|
-
self
|
161
|
-
end
|
162
|
-
|
163
|
-
def shift
|
164
|
-
k = @keys.first
|
165
|
-
v = delete(k)
|
166
|
-
[k, v]
|
167
|
-
end
|
168
|
-
|
169
|
-
def merge!(other_hash)
|
170
|
-
if block_given?
|
171
|
-
other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v }
|
172
|
-
else
|
173
|
-
other_hash.each { |k, v| self[k] = v }
|
174
|
-
end
|
175
|
-
self
|
176
|
-
end
|
177
|
-
|
178
|
-
alias_method :update, :merge!
|
179
|
-
|
180
|
-
def merge(other_hash, &block)
|
181
|
-
dup.merge!(other_hash, &block)
|
182
|
-
end
|
183
|
-
|
184
|
-
# When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
|
185
|
-
def replace(other)
|
186
|
-
super
|
187
|
-
@keys = other.keys
|
188
|
-
self
|
189
|
-
end
|
190
|
-
|
191
|
-
def invert
|
192
|
-
OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}]
|
193
|
-
end
|
194
|
-
|
195
|
-
private
|
196
|
-
def sync_keys!
|
197
|
-
@keys.delete_if {|k| !has_key?(k)}
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|