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.



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

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.



5
6
7
# File 'lib/props_template/searcher.rb', line 5

def builder
  @builder
end

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/props_template/searcher.rb', line 5

def context
  @context
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



5
6
7
# File 'lib/props_template/searcher.rb', line 5

def fragments
  @fragments
end

#traveled_pathObject (readonly)

Returns the value of attribute traveled_path.



5
6
7
# File 'lib/props_template/searcher.rb', line 5

def traveled_path
  @traveled_path
end

Instance Method Details

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



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
# File 'lib/props_template/searcher.rb', line 68

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.new {
        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



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

def deferred!
  []
end

#found!Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/props_template/searcher.rb', line 29

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

  [@found_block, pass_opts]
end

#fragment_digest!Object



26
27
# File 'lib/props_template/searcher.rb', line 26

def fragment_digest!
end

#fragments!Object



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

def fragments!
  []
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/props_template/searcher.rb', line 44

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

  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



40
41
42
# File 'lib/props_template/searcher.rb', line 40

def set_block_content!(*args)
  yield
end