arrthorizer 0.1.2 → 0.1.3

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.
@@ -41,6 +41,18 @@ module Arrthorizer
41
41
  class Context < OpenStruct
42
42
  ConversionError = Class.new(Arrthorizer::ArrthorizerException)
43
43
 
44
+ def self.build(attrs = {})
45
+ if attrs.is_a? Context
46
+ attrs
47
+ else
48
+ new(attrs)
49
+ end
50
+ end
51
+
52
+ def initialize(attrs = {})
53
+ super(attrs.to_hash || attrs.marshal_dump)
54
+ end
55
+
44
56
  def merge(hash)
45
57
  self.class.new(to_hash.merge(hash))
46
58
  end
@@ -56,9 +68,7 @@ module Arrthorizer
56
68
 
57
69
  module_function
58
70
  def Context(contents)
59
- return contents if contents.is_a? Context
60
-
61
- return Context.new(contents.to_hash)
71
+ Context.build(contents)
62
72
  rescue NoMethodError
63
73
  raise Arrthorizer::Context::ConversionError, "Can't convert #{contents} to an Arrthorizer::Context"
64
74
  end
@@ -1,3 +1,3 @@
1
1
  module Arrthorizer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+
3
+ describe Arrthorizer::Context do
4
+ describe :initialize do
5
+ context "when a Hash is provided" do
6
+ let(:attributes) { { a: 2 } }
7
+
8
+ it "copies the keys into the new Context object" do
9
+ context = Arrthorizer::Context.new(attributes)
10
+
11
+ expect(context.a).to eq 2
12
+ end
13
+ end
14
+
15
+ context "when an OpenStruct is provided" do
16
+ let(:attributes) { OpenStruct.new(a: 2) }
17
+
18
+ it "copies the keys into the new Context object" do
19
+ context = Arrthorizer::Context.new(attributes)
20
+
21
+ expect(context.a).to eq 2
22
+ end
23
+ end
24
+
25
+ context "when an Arrthorizer::Context is provided" do
26
+ let(:attributes) { Arrthorizer::Context.new(a: 2) }
27
+
28
+ it "copies the keys into the new Context object" do
29
+ context = Arrthorizer::Context.new(attributes)
30
+
31
+ expect(context.a).to eq 2
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "DSL-style initialization" do
37
+ context "when a Hash is provided" do
38
+ let(:attributes) { { a: 2 } }
39
+
40
+ it "copies the keys into the new Context object" do
41
+ context = Arrthorizer::Context(attributes)
42
+
43
+ expect(context.a).to eq 2
44
+ end
45
+ end
46
+
47
+ context "when an OpenStruct is provided" do
48
+ let(:attributes) { OpenStruct.new(a: 2) }
49
+
50
+ it "copies the keys into the new Context object" do
51
+ context = Arrthorizer::Context(attributes)
52
+
53
+ expect(context.a).to eq 2
54
+ end
55
+ end
56
+
57
+ context "when an Arrthorizer::Context is provided" do
58
+ let(:attributes) { Arrthorizer::Context(a: 2) }
59
+
60
+ it "copies the keys into the new Context object" do
61
+ context = Arrthorizer::Context(attributes)
62
+
63
+ expect(context.a).to eq 2
64
+ end
65
+ end
66
+ end
67
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arrthorizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-03-12 00:00:00.000000000 Z
13
+ date: 2014-03-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -126,6 +126,7 @@ files:
126
126
  - lib/generators/test_unit/context_role/templates/role_test.rb
127
127
  - spec/arrthorizer_exception/inner_spec.rb
128
128
  - spec/context/equals_spec.rb
129
+ - spec/context/initialize_spec.rb
129
130
  - spec/context/merge_spec.rb
130
131
  - spec/context_builder/build_spec.rb
131
132
  - spec/context_role/to_key_spec.rb
@@ -196,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
196
197
  version: '0'
197
198
  segments:
198
199
  - 0
199
- hash: -2335547421098657015
200
+ hash: -4378932366296225598
200
201
  required_rubygems_version: !ruby/object:Gem::Requirement
201
202
  none: false
202
203
  requirements:
@@ -205,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
206
  version: '0'
206
207
  segments:
207
208
  - 0
208
- hash: -2335547421098657015
209
+ hash: -4378932366296225598
209
210
  requirements: []
210
211
  rubyforge_project:
211
212
  rubygems_version: 1.8.24
@@ -215,6 +216,7 @@ summary: Contextual authorization for your Rails (3+) application
215
216
  test_files:
216
217
  - spec/arrthorizer_exception/inner_spec.rb
217
218
  - spec/context/equals_spec.rb
219
+ - spec/context/initialize_spec.rb
218
220
  - spec/context/merge_spec.rb
219
221
  - spec/context_builder/build_spec.rb
220
222
  - spec/context_role/to_key_spec.rb