composable_decorator 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/lib/composable_decorator.rb +4 -7
- data/lib/composable_decorator/dsl.rb +54 -0
- data/lib/composable_decorator/models.rb +53 -0
- data/lib/composable_decorator/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab7452f0819960368e842bb6ebcfeaa1419a058d
|
4
|
+
data.tar.gz: 7bca3b3cdb974c0f5498b82300bd5d519e2d661c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e8a40c6d12b76bc7cd62de3618c9e3e4b9ab182b184688acb8e1eef32f45bb5ac55d52ea5e3dc83e164ee9f885b3ab53f142e55b08da303018dcd1fb92f4092
|
7
|
+
data.tar.gz: 1cb8a7c80b335881b971ca5c89e0cf5cdab3756daf25779ab37dc6488b700c7fb0c9e2127382b3dfae602e1432a492de9c7b7916b770659585adeb646d469714
|
data/README.md
CHANGED
data/lib/composable_decorator.rb
CHANGED
@@ -1,17 +1,14 @@
|
|
1
1
|
require_relative "./composable_decorator/version"
|
2
|
-
require_relative './composable_decorator/
|
3
|
-
require_relative './composable_decorator/
|
2
|
+
require_relative './composable_decorator/dsl'
|
3
|
+
require_relative './composable_decorator/models'
|
4
4
|
|
5
5
|
module ComposableDecorator
|
6
|
-
def self.included(mod)
|
7
|
-
mod.extend ClassMethods
|
8
|
-
mod.include InstanceMethods
|
9
|
-
end
|
10
6
|
end
|
11
7
|
|
12
8
|
module ActiveRecord
|
13
9
|
class Base
|
14
|
-
|
10
|
+
extend ComposableDecorator::DSL
|
11
|
+
include ComposableDecorator::Models
|
15
12
|
end
|
16
13
|
end
|
17
14
|
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ComposableDecorator
|
2
|
+
module DSL
|
3
|
+
# # declares the order of decorators applied to an instance
|
4
|
+
# # that calls the #decorate method
|
5
|
+
#
|
6
|
+
# class AdminUser
|
7
|
+
# decorate_with UserDecorator, AdminDecorator
|
8
|
+
#
|
9
|
+
# # The above code results in #decorate first decorating the instance
|
10
|
+
# # with the UserDecorator, then the AdminDecorator
|
11
|
+
#
|
12
|
+
# @Param +decorators+ is an <Array> of classes
|
13
|
+
def decorate_with(*decorators)
|
14
|
+
define_method(:decorators) { decorators }
|
15
|
+
end
|
16
|
+
|
17
|
+
# # delegates all of the decorated methods for each association.
|
18
|
+
#
|
19
|
+
# module PostDecorator
|
20
|
+
# def full_name
|
21
|
+
# "#{name} {created_at}"
|
22
|
+
#
|
23
|
+
# class Post
|
24
|
+
# decorate_with PostDecorator
|
25
|
+
#
|
26
|
+
# class Author
|
27
|
+
# has_many :posts
|
28
|
+
# delegate_decorated_to :posts
|
29
|
+
#
|
30
|
+
# # The above code allows you to call:
|
31
|
+
#
|
32
|
+
# author.decorate
|
33
|
+
# author.post_full_name
|
34
|
+
#
|
35
|
+
# # Like Rails's #delegate method, you can choose to allow_nil
|
36
|
+
# # or to prefix the delegated method. Both default to true.
|
37
|
+
#
|
38
|
+
# @Param +associations+ is an <Array> of symbols
|
39
|
+
def delegate_decorated_to(*associations, prefix: true, allow_nil: true, handle_nil_with: '')
|
40
|
+
define_delegate_decorated_methods(
|
41
|
+
associations: associations,
|
42
|
+
prefix: prefix,
|
43
|
+
allow_nil: allow_nil,
|
44
|
+
handle_nil_with: handle_nil_with)
|
45
|
+
end
|
46
|
+
|
47
|
+
def define_delegate_decorated_methods(associations: [], prefix: true, allow_nil: true, handle_nil_with: '')
|
48
|
+
define_method(:delegate_decorated_associations) { associations }
|
49
|
+
define_method(:delegate_decorated_prefix) { prefix }
|
50
|
+
define_method(:delegate_decorated_allow_nil) { allow_nil }
|
51
|
+
define_method(:delegate_decorated_handle_nil_with) { handle_nil_with }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module ComposableDecorator
|
2
|
+
module Models
|
3
|
+
def self.included(base)
|
4
|
+
base.extend DSL
|
5
|
+
|
6
|
+
base.define_delegate_decorated_methods
|
7
|
+
end
|
8
|
+
|
9
|
+
def decorate
|
10
|
+
add_decorators
|
11
|
+
decorate_associations
|
12
|
+
delegate_decorated_association_methods
|
13
|
+
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
private def add_decorators
|
18
|
+
decorators.each do |decorator|
|
19
|
+
extend(decorator)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private def decorate_associations
|
24
|
+
method_delegate_decorated_associations.each do |assoc|
|
25
|
+
public_send(assoc).try(:decorate)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private def delegate_decorated_association_methods
|
30
|
+
class_delegate_decorated_associations.each do |klass|
|
31
|
+
methods = class_decorated_methods(klass)
|
32
|
+
|
33
|
+
self.class.delegate(
|
34
|
+
*methods,
|
35
|
+
to: klass.to_s.underscore,
|
36
|
+
prefix: delegate_decorated_prefix,
|
37
|
+
allow_nil: delegate_decorated_allow_nil)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private def class_decorated_methods(klass)
|
42
|
+
klass.new.decorators.map(&:instance_methods).flatten
|
43
|
+
end
|
44
|
+
|
45
|
+
private def class_delegate_decorated_associations
|
46
|
+
delegate_decorated_associations.map { |a| a.to_s.camelize.constantize }
|
47
|
+
end
|
48
|
+
|
49
|
+
private def method_delegate_decorated_associations
|
50
|
+
delegate_decorated_associations
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composable_decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Steel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,8 @@ files:
|
|
71
71
|
- bin/setup
|
72
72
|
- composable_decorator.gemspec
|
73
73
|
- lib/composable_decorator.rb
|
74
|
+
- lib/composable_decorator/dsl.rb
|
75
|
+
- lib/composable_decorator/models.rb
|
74
76
|
- lib/composable_decorator/version.rb
|
75
77
|
homepage: https://www.github.com/adsteel/composable_decorator
|
76
78
|
licenses:
|
@@ -92,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
94
|
version: '0'
|
93
95
|
requirements: []
|
94
96
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.4.8
|
96
98
|
signing_key:
|
97
99
|
specification_version: 4
|
98
100
|
summary: A simple, composable decorator pattern for Ruby.
|