lite-decorator 1.3.1 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +23 -7
- data/lib/lite/decorator/base.rb +3 -7
- data/lib/lite/decorator/{record.rb → delegator.rb} +4 -10
- data/lib/lite/decorator/inference.rb +19 -0
- data/lib/lite/decorator/version.rb +1 -1
- data/lib/lite/decorator.rb +2 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86273c0729ff8bedae37cb2b0c05a502e368fab6aba0a9e32c639acddb9335ab
|
4
|
+
data.tar.gz: c06a5e0ee30f5f8f84a76ac39a6cb028ce4955cbb86d51e14671b608a0deb9c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a049d6bd1d9a19fa4c34e233525e4bd40cb621470c4a52ff1758d886c3c3ac5e8b004e5e9abfdb231e6655074fdb7b406f3465e04ea7dd0fcde6c04388074b5a
|
7
|
+
data.tar.gz: 1243a68c3306d38f3c828fa75ec1fea227597934b9e80238ba7cab476e8d98dc843b08efa709d22a8753a7bf8864a923a786a23b1d7554d1bd10721560e0eccf
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [2.0.1] - 2024-09-23
|
10
|
+
### Changed
|
11
|
+
- Rename `Infered` to `Inference` and `Record` to `Delegator`
|
12
|
+
|
13
|
+
## [2.0.0] - 2024-09-22
|
14
|
+
### Changed
|
15
|
+
- Split inference from lazy delegation
|
16
|
+
|
9
17
|
## [1.3.1] - 2024-09-20
|
10
18
|
### Changed
|
11
19
|
- Memoize record decorator class
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -26,7 +26,6 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
* [Setup](#setup)
|
28
28
|
* [Usage](#usage)
|
29
|
-
* [ActiveRecord](#active_record)
|
30
29
|
|
31
30
|
## Setup
|
32
31
|
|
@@ -59,7 +58,7 @@ end
|
|
59
58
|
## Usage
|
60
59
|
|
61
60
|
To access the decorator you need to pass the object to the decorator class.
|
62
|
-
PORO's
|
61
|
+
PORO's and ActiveRecord objects can be decorated with this method.
|
63
62
|
|
64
63
|
#### Instance
|
65
64
|
```ruby
|
@@ -75,16 +74,33 @@ collection = UserDecorator.new(users)
|
|
75
74
|
collection.map(&:full_name) #=> ["John Doe", "Jane Poe"]
|
76
75
|
```
|
77
76
|
|
78
|
-
|
77
|
+
#### Infered decoration
|
79
78
|
|
80
|
-
Including the
|
81
|
-
|
82
|
-
|
79
|
+
Including the `Inference` module will setup a `decorator_class` and `decorator` method
|
80
|
+
that you can access via your PORO and ActiveRecord objects.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class User
|
84
|
+
include Lite::Decorator::Inference
|
85
|
+
end
|
86
|
+
|
87
|
+
user = User.first
|
88
|
+
user.decorator.full_name
|
89
|
+
```
|
90
|
+
|
91
|
+
#### Decorator delegation
|
92
|
+
|
93
|
+
Including the `Delegator` module will use method missing to delegate decorator methods as
|
94
|
+
if it lived on the PORO or ActiveRecord object itself. If neither the class instance and the
|
95
|
+
decorator contain the method, a `NoMethodError` just like normal.
|
83
96
|
|
84
97
|
```ruby
|
85
98
|
class User < ActiveRecord::Base
|
86
|
-
include Lite::Decorator::
|
99
|
+
include Lite::Decorator::Delegator
|
87
100
|
end
|
101
|
+
|
102
|
+
user = User.first
|
103
|
+
user.full_name
|
88
104
|
```
|
89
105
|
|
90
106
|
## Development
|
data/lib/lite/decorator/base.rb
CHANGED
@@ -6,14 +6,10 @@ module Lite
|
|
6
6
|
module Decorator
|
7
7
|
class Base < SimpleDelegator
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
def decorate(object)
|
12
|
-
return new(object) unless object.respond_to?(:each)
|
13
|
-
|
14
|
-
object.map { |obj| new(obj) }
|
15
|
-
end
|
9
|
+
def self.decorate(object)
|
10
|
+
return new(object) unless object.respond_to?(:each)
|
16
11
|
|
12
|
+
object.map { |obj| new(obj) }
|
17
13
|
end
|
18
14
|
|
19
15
|
def class
|
@@ -2,16 +2,10 @@
|
|
2
2
|
|
3
3
|
module Lite
|
4
4
|
module Decorator
|
5
|
-
module
|
5
|
+
module Delegator
|
6
6
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
@decorator_class = "#{self.class.name}Decorator".safe_constantize
|
11
|
-
end
|
12
|
-
|
13
|
-
def decorator
|
14
|
-
@decorator ||= decorator_class&.new(self)
|
7
|
+
def self.included(base)
|
8
|
+
base.include Inference
|
15
9
|
end
|
16
10
|
|
17
11
|
private
|
@@ -31,7 +25,7 @@ module Lite
|
|
31
25
|
def respond_to_method?(method_name)
|
32
26
|
return respond_to_method_cache[method_name] if respond_to_method_cache.key?(method_name)
|
33
27
|
|
34
|
-
respond_to_method_cache[method_name] = decorator
|
28
|
+
respond_to_method_cache[method_name] = decorator&.public_methods&.include?(method_name)
|
35
29
|
end
|
36
30
|
|
37
31
|
def respond_to_missing?(method_name, include_private = false)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lite
|
4
|
+
module Decorator
|
5
|
+
module Inference
|
6
|
+
|
7
|
+
def decorator_class
|
8
|
+
return @decorator_class if defined?(@decorator_class)
|
9
|
+
|
10
|
+
@decorator_class = "#{self.class.name}Decorator".safe_constantize
|
11
|
+
end
|
12
|
+
|
13
|
+
def decorator
|
14
|
+
@decorator ||= decorator_class&.new(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/lite/decorator.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lite-decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -204,7 +204,8 @@ files:
|
|
204
204
|
- lib/generators/rails/templates/decorator.rb.tt
|
205
205
|
- lib/lite/decorator.rb
|
206
206
|
- lib/lite/decorator/base.rb
|
207
|
-
- lib/lite/decorator/
|
207
|
+
- lib/lite/decorator/delegator.rb
|
208
|
+
- lib/lite/decorator/inference.rb
|
208
209
|
- lib/lite/decorator/version.rb
|
209
210
|
- lite-decorator.gemspec
|
210
211
|
homepage: http://drexed.github.io/lite-decorator
|