responsible 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODBiZDU4NGFlZmYwZWM4YmY5YmQwNzc5MzBkYmIwYzY0YzE2OGU3Mg==
5
+ data.tar.gz: !binary |-
6
+ YjI3MDIxMmY3ZWMwODZiYjM5MjQ1NjI1YWYxZmE1ZTE3OTVkNTUzYw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MWY5YWFiNGE0ZGU4ZjkzYjEwYmUxNDg2ZDQ3OTY1NWQwMTYyNDU5NjU1YzJk
10
+ ZWQzYmRjOGRjNjZmMTg4NTIxZTk3YzI2M2JlNzFhMGQyODU4ZGJlY2Q4ZmMz
11
+ MmNhN2M5N2Y2ZDdiYmI0MzA0M2Y3ZTYwNjcwNTQzZjU2Mjg1NDA=
12
+ data.tar.gz: !binary |-
13
+ NzAxNzA4YWM3YTU4YWMyODgwN2JhOTk2MTJiOWZhZTBkNjVjZDM2MzRjMGU5
14
+ ZGUzNzU0MTA2N2RkZTYxNzUxZjZjNWY3Y2QxYTgwY2UzZWIyZjdlZDdlNDJl
15
+ OGRkNmEwZTlhMzM5MWZiMDMzMTFmZWRmNzU2M2I3ZDk4NWNlOGI=
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ gemspec
2
+
3
+ group :test do
4
+ gem "rspec"
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Reevoo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ responsible
2
+ ===========
3
+
4
+ Generic Response Builder
data/lib/consumer.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Responsible
2
+ class Consumer
3
+ def initialize(*valid_restrictions)
4
+ @valid_restrictions = valid_restrictions
5
+ end
6
+
7
+ def can_see?(restrictions)
8
+ @valid_restrictions.include?(restrictions)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,66 @@
1
+ module Responsible
2
+
3
+ class PropertyNotImplemented < StandardError; end
4
+ class UnknownConfigurationParameter < StandardError; end
5
+
6
+ class Base
7
+
8
+ class << self
9
+ def doc(str=nil)
10
+ @doc ||= []
11
+ @doc << str if str
12
+ @doc
13
+ end
14
+
15
+ def property(name, options={})
16
+ unknown_configuration_params = options.keys - [:delegate, :to, :restrict_to, :doc]
17
+ raise(Responsible::UnknownConfigurationParameter, unknown_configuration_params.join(", ")) if unknown_configuration_params.any?
18
+
19
+ properties[name.to_sym] = options
20
+ delegate_method(name, options[:to]) if options[:delegate]
21
+ end
22
+
23
+ def properties
24
+ @properties ||= {}
25
+ end
26
+
27
+ private
28
+
29
+ def delegate_method(name, to)
30
+ define_method name do
31
+ data.send(to || name)
32
+ end
33
+ end
34
+ end
35
+
36
+ attr_reader :consumer, :data
37
+
38
+ def initialize(consumer, data)
39
+ @consumer, @data = consumer, data
40
+
41
+ undefined_properties = _properties_.keys - methods
42
+
43
+ if undefined_properties.any?
44
+ raise Responsible::PropertyNotImplemented, undefined_properties.join(", ")
45
+ end
46
+ end
47
+
48
+ def as_json(opt={})
49
+ result = {}
50
+
51
+ _properties_.each do |name, options|
52
+ if consumer.can_see?(options[:restrict_to])
53
+ result[name] = send(name)
54
+ end
55
+ end
56
+
57
+ result
58
+ end
59
+
60
+ private
61
+
62
+ def _properties_
63
+ self.class.properties
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,19 @@
1
+ $:.unshift 'lib'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.platform = Gem::Platform::RUBY
5
+ s.name = "responsible"
6
+ s.version = "0.0.1"
7
+ s.date = Time.now.strftime('%Y-%m-%d')
8
+ s.homepage = "https://github.com/reevoo/responsible"
9
+ s.authors = ["dw_henry", "lamp"]
10
+ s.email = "developers@reevoo.com"
11
+ s.summary = "Response builders"
12
+
13
+ s.files = %w[ Gemfile README.md responsible.gemspec LICENSE ]
14
+ s.files += Dir['lib/**/*']
15
+ s.test_files = Dir['spec/**/*']
16
+
17
+ s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'minitest'
19
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe Responsible::Base do
4
+
5
+ context "when a simple property is declare" do
6
+ let(:consumer) { double(:consumer) }
7
+ let(:data) { double(:data) }
8
+
9
+ it "raises an error is the property is not implemented" do
10
+ klass = Class.new(described_class) do
11
+ property :not_implemented
12
+ end
13
+
14
+ expect {
15
+ klass.new(consumer, data)
16
+ }.to raise_error(Responsible::PropertyNotImplemented)
17
+ end
18
+
19
+ it "defines a property if the method exists" do
20
+ klass = Class.new(described_class) do
21
+ property :implemented
22
+
23
+ def implemented
24
+
25
+ end
26
+ end
27
+
28
+ expect {
29
+ klass.new(consumer, data)
30
+ }.to_not raise_error
31
+ end
32
+
33
+ it "will create a delegated method (to the 2nd arg) if delegate: true passed in" do
34
+ klass = Class.new(described_class) do
35
+ property :delegated, delegate: true
36
+ end
37
+
38
+ expect {
39
+ klass.new(consumer, data)
40
+ }.to_not raise_error
41
+
42
+ data = Struct.new(:delegated).new('delegated value')
43
+ klass.new(consumer, data).delegated.should == 'delegated value'
44
+ end
45
+
46
+ it "raises an error if defined with unknown param" do
47
+ expect {
48
+ Class.new(described_class) do
49
+ property :delegated, unknown_param: true
50
+ end
51
+ }.to raise_error(Responsible::UnknownConfigurationParameter)
52
+ end
53
+
54
+ context "when 'to' parameter passed in" do
55
+ it "raises and error if to method is not defined" do
56
+ klass = Class.new(described_class) do
57
+ property :delegated, delegate: true, to: :delegated?
58
+ end
59
+
60
+ data_class = Class.new do
61
+ def delegated?
62
+ true
63
+ end
64
+ end
65
+
66
+ klass.new(consumer, data_class.new).delegated.should be_true
67
+ end
68
+ end
69
+ end
70
+
71
+ describe "#as_json" do
72
+
73
+ it "returns a data structure that includes properties without a role" do
74
+ klass = Class.new(described_class) do
75
+ property :no_role
76
+
77
+ def no_role
78
+ data.no_role
79
+ end
80
+ end
81
+
82
+ consumer = double(:consumer, can_see?: true)
83
+ data = double(:data, no_role: true)
84
+
85
+ expect(klass.new(consumer, data).as_json).to eq(no_role: true)
86
+ end
87
+
88
+ it "returns a data structure that does not include properties that has a role the consumer can't see" do
89
+ klass = Class.new(described_class) do
90
+ property :with_role, restrict_to: :some_role
91
+
92
+ def with_role
93
+ data.with_role
94
+ end
95
+ end
96
+
97
+ consumer = double(:consumer, can_see?: false)
98
+ data = double(:data, with_role: 'besty')
99
+ expect(klass.new(consumer, data).as_json).to eq({})
100
+ end
101
+
102
+ it "returns a data structure that includes properties for multipler roles the consumer can see" do
103
+ klass = Class.new(described_class) do
104
+ property :with_external_role, restrict_to: :external
105
+ property :with_analytics_role, restrict_to: :analytics
106
+ property :with_another_role, restrict_to: :another_role
107
+
108
+ def with_external_role
109
+ data.with_external_role
110
+ end
111
+
112
+ def with_analytics_role
113
+ data.with_analytics_role
114
+ end
115
+
116
+ def with_another_role
117
+ data.with_another_role
118
+ end
119
+ end
120
+
121
+
122
+ data = double(:data, with_external_role: 'external', with_analytics_role: 'analytics', with_another_role: 'another')
123
+ consumer = Responsible::Consumer.new(:external, :analytics)
124
+
125
+ expect(klass.new(consumer, data).as_json).to eq( { with_external_role: 'external', with_analytics_role: 'analytics' } )
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift("./lib")
2
+ require 'responsible'
3
+ require 'consumer'
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: responsible
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - dw_henry
8
+ - lamp
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description:
43
+ email: developers@reevoo.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - README.md
50
+ - responsible.gemspec
51
+ - LICENSE
52
+ - lib/consumer.rb
53
+ - lib/responsible.rb
54
+ - spec/base_spec.rb
55
+ - spec/spec_helper.rb
56
+ homepage: https://github.com/reevoo/responsible
57
+ licenses: []
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.1.11
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Response builders
79
+ test_files:
80
+ - spec/base_spec.rb
81
+ - spec/spec_helper.rb
82
+ has_rdoc: