Class: Expo3::Builder
- Inherits:
-
Object
- Object
- Expo3::Builder
- Defined in:
- lib/expo3.rb
Instance Method Summary collapse
- #augment(another, *keys) ⇒ Object
- #collection(key, anothers, sub_expo = nil, &block) ⇒ Object
- #current_expo ⇒ Object
- #expose(*keys) ⇒ Object
-
#initialize(expo) ⇒ Builder
constructor
A new instance of Builder.
- #inline(hash) ⇒ Object
- #merge_expo(another, sub_expo = nil, &block) ⇒ Object
- #output(obj = nil, **context) ⇒ Object
- #sub_expo(key, another, sub_expo = nil, &block) ⇒ Object
Constructor Details
#initialize(expo) ⇒ Builder
Returns a new instance of Builder.
3 4 5 6 7 |
# File 'lib/expo3.rb', line 3 def initialize(expo) @expo = expo @output = {} @blocks = [] end |
Instance Method Details
#augment(another, *keys) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/expo3.rb', line 19 def augment(another, *keys) return if another.nil? key_map = key_map(keys) output = @output @blocks << Proc.new do |obj, **context| key_map.each do |k,v| output[v] = another.send(k) end end end |
#collection(key, anothers, sub_expo = nil, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/expo3.rb', line 50 def collection(key, anothers, sub_expo=nil, &block) return if anothers.nil? output = @output sub = (sub_expo || Expo3.new).upgrade(&block) @blocks << Proc.new do |obj,**context| out = anothers.map.with_index do |o, i| if sub.expects?(:index) sub.expo(o, **(context.merge(index: i))) else sub.expo(o, **context) end end output[key] = out end sub end |
#current_expo ⇒ Object
71 72 73 |
# File 'lib/expo3.rb', line 71 def current_expo @expo end |
#expose(*keys) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/expo3.rb', line 9 def expose(*keys) key_map = key_map(keys) output = @output @blocks << Proc.new do |obj, **context| key_map.each do |k,v| output[v] = obj.send(k) end end end |
#inline(hash) ⇒ Object
67 68 69 |
# File 'lib/expo3.rb', line 67 def inline(hash) @output.merge!(hash) end |
#merge_expo(another, sub_expo = nil, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/expo3.rb', line 40 def merge_expo(another, sub_expo=nil, &block) return if another.nil? output = @output @blocks << Proc.new do |obj, **context| sub = sub_expo || Expo3.new(&block) out = sub.expo(another,**context) output.merge! out end end |
#output(obj = nil, **context) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/expo3.rb', line 75 def output(obj=nil,**context) @blocks.each do |b| b.call(obj,**context) end @output end |
#sub_expo(key, another, sub_expo = nil, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/expo3.rb', line 30 def sub_expo(key, another, sub_expo=nil, &block) return if another.nil? output = @output @blocks << Proc.new do |obj, **context| sub = sub_expo || Expo3.new(&block) out = sub.expo(another, **context) output[key] = out end end |