Module: BreezyTemplate::SearchExtension

Included in:
BreezyTemplate
Defined in:
lib/breezy_template/search_extension.rb

Instance Method Summary collapse

Instance Method Details

#_found!Object



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

def _found!
  if !@search_path.nil? && @found.nil?
    ::Kernel.raise NotFoundError.build(@search_path)
  end

  found = @found
  @found = nil
  @search_path = nil
  found
end

#_mapping_element(element, options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/breezy_template/search_extension.rb', line 25

def _mapping_element(element, options)
  if @search_path && !@search_path.empty?
    original_search_path = @search_path
    @search_path = original_search_path[1..-1]
    if @search_path.size == 0
      @found = super
    else
      yield element
    end

    @search_path = original_search_path
  else
    super
  end
end

#_prepare_collection_for_map(collection) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/breezy_template/search_extension.rb', line 41

def _prepare_collection_for_map(collection)
  if @search_path && !@search_path.empty?
    id_name, id_val = @search_path.first.split('=')

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

    found ? [found] : []
  else
    super
  end
end

#_set_search_path_once(search_path) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/breezy_template/search_extension.rb', line 16

def _set_search_path_once(search_path)
  return if @search_path

  if search_path.is_a? ::String
    return _set_search_path_once(search_path.split('.'))
  end
  @search_path = search_path
end

#set!(key, value = BLANK, *args) ⇒ Object



59
60
61
62
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
# File 'lib/breezy_template/search_extension.rb', line 59

def set!(key, value = BLANK, *args)
  return if @found
  options = args.first || {}

  if @search_path && !@search_path.empty?
    if key.to_s == @search_path.first
      original_search_path = @search_path
      @search_path = original_search_path[1..-1]
      if @search_path.size == 0
        @found = super
      else
        if ::Kernel.block_given?
          yield self
        elsif _partial_options?(options)
          without = options.dup
          without.delete(:cache)

          super(key, value, without)
        else
          ::Kernel.raise LeafTraversalError.build(key, value, options, @search_path)
        end
      end

      @search_path = original_search_path
    end

    return _blank
  else
    super
  end
end