pragma-decorator 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e305095eddad1a3fdb573ece93d351b61732bcd
4
- data.tar.gz: bcc5c1edaa9d26b41b648ddccadd9c5fe78748b4
3
+ metadata.gz: 4ff5939aba8c33bfd6bce0dfe784392ec89b0905
4
+ data.tar.gz: 24b7825e2c07df12ef69089d7ee854a1bcbcc408
5
5
  SHA512:
6
- metadata.gz: 8f05deb8a7a6b5142b11916d630b7a91471887f92528beaa742bf1dc408efbbdbef8ba8ddf0426f4ae50d6c95990b12a06b25f2b4a77a5cd7b4b7592ef3c430b
7
- data.tar.gz: 1893e791b53c6f2ed32f8f652844a54f0336c19924d02c52d3036f2ea70383e308be45be19fd75bb3702d977cb6bcdb34c2f7bf2eeaf98abe65852c4175fca34
6
+ metadata.gz: cf9c5241c0107a425dbe86bd802f46ac25c698b219ace26187aa1b5a744ecf18fbbf230ea090f59799964e1c7c632980e2488f9c779a5bae25880f5f8d45ffb1
7
+ data.tar.gz: 3e6a9cfbfaca8868c8f2f39a9be3978f2e5319c4f7b2e5ac7357413bc2372d83ca6450e0bf800e98e98fe1ab66b71005784c54fe111ce159e041823f89fbb0be
data/.rubocop.yml CHANGED
@@ -53,7 +53,7 @@ Style/BracesAroundHashParameters:
53
53
  EnforcedStyle: context_dependent
54
54
 
55
55
  Lint/EndAlignment:
56
- AlignWith: variable
56
+ EnforcedStyleAlignWith: variable
57
57
  AutoCorrect: true
58
58
 
59
59
  Style/AndOr:
@@ -82,3 +82,6 @@ Metrics/PerceivedComplexity:
82
82
 
83
83
  Metrics/CyclomaticComplexity:
84
84
  Enabled: false
85
+
86
+ Metrics/BlockLength:
87
+ Enabled: false
@@ -86,6 +86,6 @@ Here's a list of options accepted when defining an association:
86
86
  Name | Type | Default | Meaning
87
87
  ---- | ---- | ------- | -------
88
88
  `expandable` | Boolean | `false` | Whether this association is expandable by consumers. Attempting to expand a non-expandable association will raise a `UnexpandableError`.
89
- `decorator` | Class | - | If provided, decorates the expanded object with this decorator. Otherwise, simply calls `#to_hash` on the object to get a representable hash.
89
+ `decorator` | Class|Proc | - | If provided, decorates the expanded object with this decorator. Otherwise, simply calls `#to_hash` on the object to get a representable hash. If the option is callable, will call it and pass the associated object - a decorator class should be returned, or `nil` to skip decoration.
90
90
  `render_nil` | Boolean | `false` | Whether the property should be rendered at all when it is `nil`.
91
91
  `exec_context` | Symbol | `:decorated` | Whether to call the getter on the decorator (`:decorator`) or the decorated object (`:decorated`).
@@ -68,8 +68,10 @@ module Pragma
68
68
  }
69
69
  }
70
70
 
71
- if reflection.options[:decorator]
72
- reflection.options[:decorator].new(associated_object).to_hash(options)
71
+ decorator_klass = compute_decorator
72
+
73
+ if decorator_klass
74
+ decorator_klass.new(associated_object).to_hash(options)
73
75
  else
74
76
  associated_object.as_json(options)
75
77
  end
@@ -106,6 +108,14 @@ module Pragma
106
108
  end
107
109
  end
108
110
  end
111
+
112
+ def compute_decorator
113
+ if reflection.options[:decorator].respond_to?(:call)
114
+ reflection.options[:decorator].call(associated_object)
115
+ else
116
+ reflection.options[:decorator]
117
+ end
118
+ end
109
119
  end
110
120
  end
111
121
  end
@@ -23,7 +23,8 @@ module Pragma
23
23
  # @param options [Hash] additional options
24
24
  #
25
25
  # @option options [Boolean] :expandable (`false`) whether the association is expandable
26
- # @option options [Class] :decorator the decorator to use for the associated object
26
+ # @option options [Class|Proc] :decorator the decorator to use for the associated object
27
+ # or a callable that will return the decorator class (or +nil+ to skip decoration)
27
28
  # @option options [Boolean] :render_nil (`true`) whether to render a +nil+ association
28
29
  # @option options [Symbol] :exec_context (`decorated`) whether to call the getter on the
29
30
  # decorator (+decorator+) or the decorated object (+decorated+)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Pragma
3
3
  module Decorator
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1.0'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pragma-decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Desantis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-16 00:00:00.000000000 Z
11
+ date: 2017-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roar