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 = nil, 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
100
101
102
103
104
105
106
# File 'lib/props_template/searcher.rb', line 63

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

  if collection.nil?
    # Handle child! mode - initialize child_index for searching
    @child_index = nil
    yield
  else
    # Original collection handling
    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
end

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/props_template/searcher.rb', line 108

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

  child_index = @child_index || -1
  child_index += 1

  key_index = @search_path[@depth]
  target_index = key_index.to_i

  if child_index == target_index
    @traveled_path.push(key_index)

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

    @depth += 1
    yield
    @depth -= 1
  end

  @child_index = child_index    
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