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
15
16
# 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)
  @fragment_name = nil
  @found_item = nil
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



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/props_template/searcher.rb', line 79

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_item = item
        @found_block = proc {
          yield item, 0
        }
        return
      end

      @depth += 1
      if pass_opts[:partial]
        fragment_name = Fragment.fragment_name_from_options(pass_opts, item)
        if fragment_name
          @fragment_name = fragment_name
          @fragment_path = @traveled_path.clone
        end
        # 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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/props_template/searcher.rb', line 130

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



18
19
20
# File 'lib/props_template/searcher.rb', line 18

def deferred!
  []
end

#found!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/props_template/searcher.rb', line 26

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

  fragment_name = Fragment.fragment_name_from_options(pass_opts, @found_item)
  if fragment_name
    @fragment_name = fragment_name
    @fragment_path = @traveled_path.clone
  end

  fragment_context = @fragment_name

  [@found_block, @traveled_path, pass_opts, @fragment_path, fragment_context, @found_item]
end

#fragments!Object



22
23
24
# File 'lib/props_template/searcher.rb', line 22

def fragments!
  []
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/props_template/searcher.rb', line 49

def set!(key, options = {}, &block)
  return if @found_block || !block && !options.is_a?(Props::Options)

  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]
      fragment_name = Fragment.fragment_name_from_options(options)
      if fragment_name
        @fragment_name = fragment_name
        @fragment_path = @traveled_path.clone
      end

      @partialer.handle(options)
    else
      yield
    end
    @depth -= 1
  end

  nil
end

#set_content!(*args) ⇒ Object



45
46
47
# File 'lib/props_template/searcher.rb', line 45

def set_content!(*args)
  yield
end