lite-decorator 1.2.1 → 1.3.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
  SHA256:
3
- metadata.gz: e0026f563698dc67b2405c7314b71e62e6505f839e3834bd512ab6b40d0815b8
4
- data.tar.gz: 34ca2dda26a84bd2e59b69d87d7e46fd697ef908b4b68a44b20c4ca67c97d876
3
+ metadata.gz: 1fbe8cde8843bd22d6181ad4aeaebd5fe3e74eab12a970c3862a6615e4e761a1
4
+ data.tar.gz: 1304f8031585e3500a704bdbb4091816a8966177325c094e24dfa1f02f7654ed
5
5
  SHA512:
6
- metadata.gz: eaaa7dad4f695a770c416d0387afc33432e0f79915cd7c5c832ae1e06c3e41d5a3e4d9b8e371971a5cd17a31a1f59a164ff0090308dbe8e0fe8cfc4c0227b273
7
- data.tar.gz: dbc3256f11d9184ff53d5476d7f3f69880dea7aeeaa768450ff526d94953f1563df2b32f7a09d263c3e6a793d81b9bb4a003256b315097a0b1cc6da8e8cc3ab5
6
+ metadata.gz: 9207d3e6a931bc694d25030ae219d88efb8ec99798623a6b0c36ae20f00131034515be2102075b216c2077bee890772cbd00cb46bfe3c556efab3d393c497c4e
7
+ data.tar.gz: d43cfa5cb11fd7987476229ec6c95b332c75f4c288ff1343cb2ade16e2214e93b91f914d5c790ca7c5c6510f28b4534b32b99bc7c784532c70a71a51f8c205ad
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.3.0] - 2024-09-20
10
+ ### Added
11
+ - Record lazy load module
12
+
9
13
  ## [1.2.1] - 2024-09-20
10
14
  ### Changed
11
15
  - Bump dependencies
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-decorator (1.2.1)
4
+ lite-decorator (1.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
26
26
 
27
27
  * [Setup](#setup)
28
28
  * [Usage](#usage)
29
+ * [ActiveRecord](#active_record)
29
30
 
30
31
  ## Setup
31
32
 
@@ -58,6 +59,7 @@ end
58
59
  ## Usage
59
60
 
60
61
  To access the decorator you need to pass the object to the decorator class.
62
+ PORO's just like active record objects can be decorated as well in this method.
61
63
 
62
64
  #### Instance
63
65
  ```ruby
@@ -73,6 +75,18 @@ collection = UserDecorator.new(users)
73
75
  collection.map(&:full_name) #=> ["John Doe", "Jane Poe"]
74
76
  ```
75
77
 
78
+ ## ActiveRecord
79
+
80
+ Including the record module will automatically lazy delegate methods to decorators
81
+ if available. If neither the class instance and the decorator contain the method,
82
+ a `NoMethodError` just like normal.
83
+
84
+ ```ruby
85
+ class User < ActiveRecord::Base
86
+ include Lite::Decorator::Record
87
+ end
88
+ ```
89
+
76
90
  ## Development
77
91
 
78
92
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Decorator
5
+ module Record
6
+
7
+ def decorator_class
8
+ "#{self.class.name}Decorator".safe_constantize
9
+ end
10
+
11
+ def decorator
12
+ @decorator ||= decorator_class&.new(self)
13
+ end
14
+
15
+ private
16
+
17
+ def method_missing(method_name, *args, **kwargs, &)
18
+ if respond_to_method?(method_name)
19
+ decorator.send(method_name, *args, **kwargs, &)
20
+ else
21
+ super
22
+ end
23
+ end
24
+
25
+ def respond_to_method_cache
26
+ @respond_to_method_cache ||= {}
27
+ end
28
+
29
+ def respond_to_method?(method_name)
30
+ return respond_to_method_cache[method_name] if respond_to_method_cache.key?(method_name)
31
+
32
+ respond_to_method_cache[method_name] = decorator.present? && decorator.public_methods.include?(method_name)
33
+ end
34
+
35
+ def respond_to_missing?(method_name, include_private = false)
36
+ respond_to_method?(method_name) || super
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Decorator
5
5
 
6
- VERSION = "1.2.1"
6
+ VERSION = "1.3.0"
7
7
 
8
8
  end
9
9
  end
@@ -4,3 +4,4 @@ require "generators/rails/decorator_generator" if defined?(Rails::Generators)
4
4
 
5
5
  require "lite/decorator/version"
6
6
  require "lite/decorator/base"
7
+ require "lite/decorator/record"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
@@ -204,6 +204,7 @@ 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/record.rb
207
208
  - lib/lite/decorator/version.rb
208
209
  - lite-decorator.gemspec
209
210
  homepage: http://drexed.github.io/lite-decorator