lab42_diggy_methods 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/lib/lab42/diggy_methods/version.rb +2 -1
- data/lib/lab42/diggy_methods.rb +4 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c7c506c170e95600888b44e102b565f4c6fa3fe52bf7dcd34c935d204e9980a
|
4
|
+
data.tar.gz: df9c49145cd0780be53b8e1fbc4c568d53064cb35200839d3f80baace035b0b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f316245eed051450bd831d7b378376ba4e9fbd515b4efad4b670aeed2949a98de239d2ec2d81db5067272c446b067a86ab805b90779cd73afc412ec9539b93a1
|
7
|
+
data.tar.gz: c7965c692a9ce203a079482a939b6f3f6ff16a60fcc2307fd4fcfeb40224a5a20c42c4c687f64c6e1f6375e5bff5ebe07ae995111e660e08fd255095c3d2fd3d
|
data/README.md
CHANGED
@@ -60,6 +60,25 @@ But if we pass an array
|
|
60
60
|
expect{ Diggy([:a]) }.to raise_error(ArgumentError)
|
61
61
|
```
|
62
62
|
|
63
|
+
### Context: Using in ERB
|
64
|
+
|
65
|
+
In order to take advantage of this syntax we want to pass the binding of a `Diggy` object to `ERB`
|
66
|
+
|
67
|
+
Given an `ERB` template
|
68
|
+
```ruby
|
69
|
+
require 'erb'
|
70
|
+
let(:template_text) { "<%= data.person.name %>" }
|
71
|
+
```
|
72
|
+
|
73
|
+
And a `Lab42::DiggyMethod` instance
|
74
|
+
```ruby
|
75
|
+
let(:data) { Diggy(data: {person: {name: "YHS"}}) }
|
76
|
+
```
|
77
|
+
|
78
|
+
Then we can pass the binding to the template
|
79
|
+
```ruby
|
80
|
+
expect(ERB.new(template_text).result(data.__binding__)).to eq("YHS")
|
81
|
+
```
|
63
82
|
# LICENSE
|
64
83
|
|
65
84
|
Copyright 2022 Robert Dober robert.dober@gmail.com
|
data/lib/lab42/diggy_methods.rb
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
require_relative 'diggy_methods/kernel'
|
4
4
|
module Lab42
|
5
5
|
class DiggyMethods
|
6
|
-
|
6
|
+
|
7
|
+
# Expose for ERB
|
8
|
+
def __binding__; binding end
|
7
9
|
|
8
10
|
# In case data has key :data (often the case)
|
9
11
|
def __data__; @data end
|
@@ -36,7 +38,7 @@ module Lab42
|
|
36
38
|
end
|
37
39
|
|
38
40
|
def method_missing(name)
|
39
|
-
found = data.fetch(name)
|
41
|
+
found = @data.fetch(name)
|
40
42
|
if found.respond_to?(:to_h)
|
41
43
|
self.class.new(**found)
|
42
44
|
else
|