Skip to content

Commit 2e69cee

Browse files
committed
wip
1 parent 4ce3953 commit 2e69cee

File tree

6 files changed

+51
-28
lines changed

6 files changed

+51
-28
lines changed

lib/props_template.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ def set!(key, options = {}, &block)
5353
options.delete(:dig)
5454

5555
@builder.set!(key, options, &block)
56-
found_block, found_path, found_options, fragment_path, fragment_context = @builder.found!
56+
found_block, found_path, found_options, fragment_path, fragment_context, found_item = @builder.found!
5757
@found_path = found_path || []
5858
@fragment_context = fragment_context
5959
@builder = prev_builder
6060
@fragment_path = fragment_path
6161

6262
if found_block
63+
@builder.item_context = found_item
6364
set!(key, found_options, &found_block)
6465
end
6566
else

lib/props_template/base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ class InvalidScopeForObjError < StandardError; end
99
class InvalidScopeForChildError < StandardError; end
1010

1111
class Base
12+
attr_accessor :item_context
13+
1214
def initialize(encoder = nil)
1315
@stream = Oj::StringWriter.new(mode: :rails)
1416
@scope = nil
17+
@item_context = nil
1518
end
1619

1720
def set_content!(options = {})
1821
@scope = nil
22+
@item_context = nil
1923
yield
2024
if @scope.nil?
2125
@stream.push_object
@@ -64,6 +68,8 @@ def refine_item_options(item, options)
6468
end
6569

6670
def handle_collection_item(collection, item, index, options)
71+
@item_context = item
72+
6773
set_content!(options) do
6874
yield
6975
end

lib/props_template/base_with_extensions.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def traveled_path!
2929
def set_content!(options = {})
3030
return super if !@em.has_extensions(options)
3131

32-
@em.handle(options) do
32+
@em.handle(options, item_context) do
3333
yield
3434
end
3535
end
@@ -104,16 +104,6 @@ def refine_all_item_options(all_options)
104104
def refine_item_options(item, options)
105105
return options if options.empty?
106106

107-
if (key = options[:key])
108-
val = if item.respond_to? key
109-
item.send(key)
110-
elsif item.is_a? Hash
111-
item[key] || item[key.to_sym]
112-
end
113-
114-
options[:key] = [options[:key], val]
115-
end
116-
117107
@em.refine_options(options, item)
118108
end
119109
end

lib/props_template/extension_manager.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def has_extensions(options)
4444
options[:defer] || options[:cache] || options[:partial] || options[:key]
4545
end
4646

47-
def handle(options)
47+
def handle(options, item_context = nil)
4848
return yield if !has_extensions(options)
4949

5050
if options[:defer] && !@deferment.disabled
@@ -61,9 +61,16 @@ def handle(options)
6161
yield
6262
end
6363

64-
if options[:key]
65-
id, val = options[:key]
66-
base.set!(id, val)
64+
if (key = options[:key]) && item_context
65+
val = if item_context.respond_to? key
66+
item_context.send(key)
67+
elsif item_context.is_a? Hash
68+
item_context[key] || item_context[key.to_sym]
69+
end
70+
end
71+
72+
if key && val
73+
base.set!(key, val)
6774
end
6875
end
6976
end

lib/props_template/searcher.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def initialize(builder, path = [], context = nil)
1212
@traveled_path = []
1313
@partialer = Partialer.new(self, context, builder)
1414
@fragment_name = nil
15+
@found_item = nil
1516
end
1617

1718
def deferred!
@@ -38,7 +39,7 @@ def found!
3839

3940
fragment_context = @fragment_name
4041

41-
[@found_block, @traveled_path, pass_opts, @fragment_path, fragment_context]
42+
[@found_block, @traveled_path, pass_opts, @fragment_path, fragment_context, @found_item]
4243
end
4344

4445
def set_content!(*args)
@@ -97,21 +98,11 @@ def array!(collection = nil, options = {}, &block)
9798

9899
if item
99100
pass_opts = @partialer.refine_options(options, item)
100-
101-
if (key = pass_opts[:key])
102-
val = if item.respond_to? key
103-
item.send(key)
104-
elsif item.is_a? Hash
105-
item[key] || item[key.to_sym]
106-
end
107-
108-
pass_opts[:key] = [pass_opts[:key], val]
109-
end
110-
111101
@traveled_path.push(key_index)
112102

113103
if @depth == @search_path.size - 1
114104
@found_options = pass_opts
105+
@found_item = item
115106
@found_block = proc {
116107
yield item, 0
117108
}

lib/props_template/test.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
array
2+
handle_collection
3+
all_opts = collection.map do |item|
4+
refine_item_options(item, options)
5+
end
6+
creates clone of the opts using
7+
refine_item_options clones original options
8+
then subsequently the ext refine_item_options
9+
gets called first,
10+
11+
refine_all_item_options <- operates on the entire collection
12+
13+
handle_collection_item
14+
set_content options
15+
16+
17+
18+
base with ExtensionManager
19+
refine_all_item_options - overrided with ext manager
20+
- which attahes a _template with partialer
21+
to it AND multifetch and attaches it to cache
22+
as [key, result]
23+
24+
refine_item_options
25+
- transforms key: :id to key = [key, val]
26+
- passes to refine_options
27+
which is then asks the searcher the cache and
28+
deferment to mutate the option

0 commit comments

Comments
 (0)