Commit a848bff
committed
Add
`attr_evaluated` can be used to define an attribute who's return value
is evaluated/computed at runtime.
It defines a normal writer method using `attr_writer` which can take
either a static value or a proc/lambda.
If the latter, the reader method will evaluate the proc/lambda when
called and return its value. This allows for defining dynamic runtime
behaviour for specific record attributes.
A static value can still be assigned like other non-evaluated
attributes, and that value will always be returned.
E.g.
```ruby
class MyClass
include StaticAssociation
attr_accessor :toggle
attr_evaluated :status
record(id: 1) do
self.toggle = true
self.status = -> { toggle ? "on" : "off" }
end
record(id: 2) do
self.toggle = true
self.status = "static value"
end
end
record1 = MyClass.find(1)
record1.status # => "on"
record2 = MyClass.find(2)
record2.status # => "static value"
```
As with `attr_accessor` you can pass multiple attribute names to a
single `attr_evaluated` call.
```ruby
attr_evaluated :my_attr_1, :my_attr_2
```attr_evaluated to StaticAssociation objects1 parent 07a9b5b commit a848bff
2 files changed
+62
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
85 | 101 | | |
86 | 102 | | |
87 | 103 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
78 | 124 | | |
79 | 125 | | |
80 | 126 | | |
| |||
0 commit comments