@@ -74,38 +74,90 @@ def field(name)
74
74
fields << name
75
75
attr_accessor name
76
76
77
- # Every field gets a fast writer method for immediately persisting
78
- fast_writer ! name
77
+ # Every field gets a fast attribute method for immediately persisting
78
+ fast_attribute ! name
79
79
end
80
80
81
- # Defines a writer method with a bang (!) suffix for a given attribute name.
81
+ # Defines a fast attribute method with a bang (!) suffix for a given
82
+ # attribute name. Fast attribute methods are used to immediately read or
83
+ # write attribute values from/to Redis. Calling a fast attribute method
84
+ # has no effect on any of the object's other attributes and does not
85
+ # trigger a call to update the object's expiration time.
82
86
#
83
87
# The dynamically defined method performs the following:
84
- # - Checks if the correct number of arguments is provided (exactly one).
88
+ # - Acts as both a reader and a writer method.
89
+ # - When called without arguments, retrieves the current value from Redis.
90
+ # - When called with an argument, persists the value to Redis immediately.
91
+ # - Checks if the correct number of arguments is provided (zero or one).
85
92
# - Converts the provided value to a format suitable for Redis storage.
86
- # - Uses the existing accessor method to set the attribute value.
87
- # - Persists the value to Redis immediately using the hset command.
88
- # - Includes custom error handling to raise an ArgumentError if the wrong number of arguments is given.
89
- # - Raises a custom error message if an exception occurs during the execution of the method.
90
- #
91
- # @param [Symbol, String] name the name of the attribute for which the writer method is defined.
92
- # @raise [ArgumentError] if the wrong number of arguments is provided.
93
- # @raise [RuntimeError] if an exception occurs during the execution of the method.
94
- #
95
- def fast_writer! ( name )
93
+ # - Uses the existing accessor method to set the attribute value when
94
+ # writing.
95
+ # - Persists the value to Redis immediately using the hset command when
96
+ # writing.
97
+ # - Includes custom error handling to raise an ArgumentError if the wrong
98
+ # number of arguments is given.
99
+ # - Raises a custom error message if an exception occurs during the
100
+ # execution of the method.
101
+ #
102
+ # @param [Symbol, String] name the name of the attribute for which the
103
+ # fast method is defined.
104
+ # @return [Object] the current value of the attribute when called without
105
+ # arguments.
106
+ # @raise [ArgumentError] if more than one argument is provided.
107
+ # @raise [RuntimeError] if an exception occurs during the execution of the
108
+ # method.
109
+ #
110
+ def fast_attribute! ( name = nil )
111
+ # Fast attribute accessor method for the '#{name}' attribute.
112
+ # This method provides immediate read and write access to the attribute
113
+ # in Redis.
114
+ #
115
+ # When called without arguments, it retrieves the current value of the
116
+ # attribute from Redis.
117
+ # When called with an argument, it immediately persists the new value to
118
+ # Redis.
119
+ #
120
+ # @overload #{name}!
121
+ # Retrieves the current value of the attribute from Redis.
122
+ # @return [Object] the current value of the attribute.
123
+ #
124
+ # @overload #{name}!(value)
125
+ # Sets and immediately persists the new value of the attribute to
126
+ # Redis.
127
+ # @param value [Object] the new value to set for the attribute.
128
+ # @return [Object] the newly set value.
129
+ #
130
+ # @raise [ArgumentError] if more than one argument is provided.
131
+ # @raise [RuntimeError] if an exception occurs during the execution of
132
+ # the method.
133
+ #
134
+ # @note This method bypasses any object-level caching and interacts
135
+ # directly with Redis. It does not trigger updates to other attributes
136
+ # or the object's expiration time.
137
+ #
138
+ # @example
139
+ #
140
+ # def #{name}!(*args)
141
+ # # Method implementation
142
+ # end
143
+ #
96
144
define_method :"#{ name } !" do |*args |
97
145
# Check if the correct number of arguments is provided (exactly one).
98
- raise ArgumentError , "wrong number of arguments (given #{ args . size } , expected 1)" if args . size != 1
146
+ raise ArgumentError , "wrong number of arguments (given #{ args . size } , expected 0 or 1)" if args . size > 1
99
147
100
148
val = args . first
101
149
150
+ # If no value is provided to this fast attribute method, make a call
151
+ # to redis to return the current stored value of the hash field.
152
+ return hget name if val . nil?
153
+
102
154
begin
103
155
# Trace the operation if debugging is enabled.
104
156
Familia . trace :FAST_WRITER , redis , "#{ name } : #{ val . inspect } " , caller ( 1 ..1 ) if Familia . debug?
105
157
106
158
# Convert the provided value to a format suitable for Redis storage.
107
159
prepared = to_redis ( val )
108
- Familia . ld "[.fast_writer !] #{ name } val: #{ val . class } prepared: #{ prepared . class } "
160
+ Familia . ld "[.fast_attribute !] #{ name } val: #{ val . class } prepared: #{ prepared . class } "
109
161
110
162
# Use the existing accessor method to set the attribute value.
111
163
send :"#{ name } =" , val
0 commit comments