Class: Lyra::Associations::VirtualRecord
- Inherits:
-
Object
- Object
- Lyra::Associations::VirtualRecord
- Defined in:
- lib/lyra/associations/event_aware.rb
Overview
Virtual record representing a record from events (not in DB)
Provides a read-only interface that looks like an ActiveRecord model but is backed by event data instead of a database row.
Defined Under Namespace
Classes: ReadOnlyRecord
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access attributes.
- #attributes ⇒ Object
-
#initialize(model_class, id, state) ⇒ VirtualRecord
constructor
A new instance of VirtualRecord.
- #method_missing(method, *args) ⇒ Object
- #new_record? ⇒ Boolean
-
#pending_projection? ⇒ Boolean
Indicates this is a virtual record from events.
- #persisted? ⇒ Boolean
- #readonly? ⇒ Boolean
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
- #to_param ⇒ Object
Constructor Details
#initialize(model_class, id, state) ⇒ VirtualRecord
Returns a new instance of VirtualRecord.
164 165 166 167 168 |
# File 'lib/lyra/associations/event_aware.rb', line 164 def initialize(model_class, id, state) @model_class = model_class @id = id @state = state.transform_keys(&:to_sym) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/lyra/associations/event_aware.rb', line 200 def method_missing(method, *args) method_name = method.to_s # Getter if @state.key?(method) @state[method] # Boolean query elsif method_name.end_with?("?") field = method_name.chomp("?").to_sym !!@state[field] # Setter (rejected - read only) elsif method_name.end_with?("=") raise ReadOnlyRecord, "Cannot modify a virtual record" else nil end end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
162 163 164 |
# File 'lib/lyra/associations/event_aware.rb', line 162 def id @id end |
Instance Method Details
#[](key) ⇒ Object
Access attributes
188 189 190 |
# File 'lib/lyra/associations/event_aware.rb', line 188 def [](key) @state[key.to_sym] end |
#attributes ⇒ Object
192 193 194 |
# File 'lib/lyra/associations/event_aware.rb', line 192 def attributes @state.except(:deleted) end |
#new_record? ⇒ Boolean
174 175 176 |
# File 'lib/lyra/associations/event_aware.rb', line 174 def new_record? true end |
#pending_projection? ⇒ Boolean
Indicates this is a virtual record from events
179 180 181 |
# File 'lib/lyra/associations/event_aware.rb', line 179 def pending_projection? true end |
#persisted? ⇒ Boolean
170 171 172 |
# File 'lib/lyra/associations/event_aware.rb', line 170 def persisted? false end |
#readonly? ⇒ Boolean
183 184 185 |
# File 'lib/lyra/associations/event_aware.rb', line 183 def readonly? true end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
218 219 220 |
# File 'lib/lyra/associations/event_aware.rb', line 218 def respond_to_missing?(method, include_private = false) @state.key?(method) || method.to_s.end_with?("?") || super end |
#to_param ⇒ Object
196 197 198 |
# File 'lib/lyra/associations/event_aware.rb', line 196 def to_param id&.to_s end |