decoration 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/decoration.rb +66 -0
  3. metadata +87 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f0ec6301b730223f1618206014a28d37aa313225
4
+ data.tar.gz: 3cb8cfc3a59d2f3815cd246a493a3e65a0a2d7fe
5
+ SHA512:
6
+ metadata.gz: f4df7dceb2499ec60f361f2532700b5b5a3879eec6beef8ae16c4aee4cff285c297f11a1865176ec2e57ef5f1c80ade6fb4e845f2e3e74ee4b3c6af188b30667
7
+ data.tar.gz: 6edfddfcbee05e464a48de5aca2e689e4e9a78f85cc0e57c201f8f39a3bb3051f989eebed0e869c39f86fa2c74756e0c66f230bde7f861e10f9a05df63f28802
@@ -0,0 +1,66 @@
1
+ module Decoration
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ def add_behaviors(behaviors)
8
+ @behaviors = behaviors
9
+ end
10
+
11
+ attr_reader :behaviors
12
+ end
13
+
14
+ def initialize(component)
15
+ @component = component
16
+ check_behaviors!
17
+ define_delegation
18
+ end
19
+
20
+ private
21
+
22
+ def check_behaviors!
23
+ get_decorations.each do |behavior|
24
+ raise "method #{behavior} collides with the same name method of the component decorated" if get_originals.include?(behavior)
25
+ end
26
+ end
27
+
28
+ def get_decorations
29
+ get_behaviors.map { |_, value| value }
30
+ end
31
+
32
+ def define_delegation
33
+ get_originals.each do |method|
34
+ define_delegated_method(method)
35
+ end
36
+ end
37
+
38
+ def define_delegated_method(operation)
39
+ self.class.send(:define_method, operation) do |*args|
40
+ result = execute_original(operation, args)
41
+ decorate(result, behavior_for(operation), args)
42
+ end
43
+ end
44
+
45
+ def decorate(original_result, method, args)
46
+ return original_result unless method
47
+
48
+ send(method, original_result, *args)
49
+ end
50
+
51
+ def execute_original(operation, args)
52
+ @component.send(operation, *args)
53
+ end
54
+
55
+ def behavior_for(operation)
56
+ get_behaviors[operation]
57
+ end
58
+
59
+ def get_originals
60
+ @component.public_methods(false)
61
+ end
62
+
63
+ def get_behaviors
64
+ self.class.behaviors || {}
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: decoration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Flywire tech
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A mixin for make an object a decorator
56
+ email:
57
+ - tech@peertransfer.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/decoration.rb
63
+ homepage: http://github.com/peertransfer/decoration
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.6.4
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: A mixin for make an object a decorator
87
+ test_files: []