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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd64fc6e7e0eb6eb8ade1ec99a2bad446c873674
4
- data.tar.gz: b6def650c067d09960452e9c85d03bf2776905c9
3
+ metadata.gz: ab7452f0819960368e842bb6ebcfeaa1419a058d
4
+ data.tar.gz: 7bca3b3cdb974c0f5498b82300bd5d519e2d661c
5
5
  SHA512:
6
- metadata.gz: 6fef48689f211503b63b9ae06d81d18ee2a42068c3f850f00033792a5bb98c4fc5da3b586a0b633f68e35552a5173000eb720339d9d65c9a4de54985d4dc7fa5
7
- data.tar.gz: ca5b1ddd0df506f203068d2b221a9cfad409153c19e021bae32f962b9960d5dd484b4af7fb212753c6a82a78e10c110c699efa9c8514d479e9bc924446d5e776
6
+ metadata.gz: 9e8a40c6d12b76bc7cd62de3618c9e3e4b9ab182b184688acb8e1eef32f45bb5ac55d52ea5e3dc83e164ee9f885b3ab53f142e55b08da303018dcd1fb92f4092
7
+ data.tar.gz: 1cb8a7c80b335881b971ca5c89e0cf5cdab3756daf25779ab37dc6488b700c7fb0c9e2127382b3dfae602e1432a492de9c7b7916b770659585adeb646d469714
data/README.md CHANGED
@@ -57,7 +57,6 @@ end
57
57
  class User < ActiveRecord::Base
58
58
  extend Name
59
59
  extend PhoneNumber
60
- # more functionality runs here to work with AR relationships
61
60
  end
62
61
 
63
62
  # we can then decorate the model in the controller
@@ -1,17 +1,14 @@
1
1
  require_relative "./composable_decorator/version"
2
- require_relative './composable_decorator/class_methods'
3
- require_relative './composable_decorator/instance_methods'
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
- include ComposableDecorator
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
@@ -1,3 +1,3 @@
1
1
  module ComposableDecorator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  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.0
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-28 00:00:00.000000000 Z
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.5.0
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.