Class: Props::Searcher

Inherits:
Object
  • Object
show all
Defined in:
lib/props_template/searcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, path = [], context = nil) ⇒ Searcher

Returns a new instance of Searcher.



5
6
7
8
9
10
11
12
13
14
# File 'lib/props_template/searcher.rb', line 5

def initialize(builder, path = [], context = nil)
  @search_path = path
  @depth = 0
  @context = context
  @found_block = nil
  @found_options = nil
  @builder = builder
  @traveled_path = []
  @partialer = Partialer.new(self, context, builder)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



3
4
5
# File 'lib/props_template/searcher.rb', line 3

def builder
  @builder
end

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/props_template/searcher.rb', line 3

def context
  @context
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



3
4
5
# File 'lib/props_template/searcher.rb', line 3

def fragments
  @fragments
end

#traveled_pathObject (readonly)

Returns the value of attribute traveled_path.



3
4
5
# File 'lib/props_template/searcher.rb', line 3

def traveled_path
  @traveled_path
end

Instance Method Details

#array!(collection, options = {}, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/props_template/searcher.rb', line 63

def array!(collection, options = {}, &block)
  return if @found_block

  key_index = @search_path[@depth]
  id_name, id_val = key_index.to_s.split("=")

  if id_val
    id_val = id_val.to_i
    item = collection.member_by(id_name, id_val)
  else
    index = id_name.to_i
    item = collection.member_at(index)
  end

  if item
    pass_opts = @partialer.refine_options(options, item)
    @traveled_path.push(key_index)

    if @depth == @search_path.size - 1
      @found_options = pass_opts
      @found_block = proc {
        yield item, 0
      }
      return
    end

    @depth += 1
    if pass_opts[:partial]
      # todo: what happens when cached: true is passed?
      # would there be any problems with not using the collection_rende?
      @partialer.handle(pass_opts)
    else
      yield item, 0
    end
    @depth -= 1
  end
end

#deferred!Object



16
17
18
# File 'lib/props_template/searcher.rb', line 16

def deferred!
  []
end

#found!Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/props_template/searcher.rb', line 24

def found!
  pass_opts = @found_options.clone || {}
  pass_opts.delete(:defer)
  traveled_path = @traveled_path[1..] || []
  if !traveled_path.empty?
    pass_opts[:path_suffix] = traveled_path
  end

  [@found_block, pass_opts]
end

#fragments!Object



20
21
22
# File 'lib/props_template/searcher.rb', line 20

def fragments!
  []
end

#set!(key, options = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/props_template/searcher.rb', line 39

def set!(key, options = {}, &block)
  return if @found_block || !block

  if @search_path[@depth] == key.to_s
    @traveled_path.push(key)

    if @depth == @search_path.size - 1
      @found_options = options
      @found_block = block
      return
    end

    @depth += 1
    if options[:partial]
      @partialer.handle(options)
    else
      yield
    end
    @depth -= 1
  end

  nil
end

#set_block_content!(*args) ⇒ Object



35
36
37
# File 'lib/props_template/searcher.rb', line 35

def set_block_content!(*args)
  yield
end