Module: BreezyTemplate::SearchExtension

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

Instance Method Summary collapse

Instance Method Details

#_filter_by_path(search_path) ⇒ Object



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

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

#_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



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

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



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

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!(key, value = BLANK, *args) ⇒ Object



57
58
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
# File 'lib/breezy_template/search_extension.rb', line 57

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_track = args.dup
          super(key, value, *without_track)
        else
          ::Kernel.raise 'This should not happen'
        end
      end

      @search_path = original_search_path
    end

    return _blank
  else
    super
  end
end