Module: Lycra::Search::Enumerable
- Defined in:
- lib/lycra/search/enumerable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #apply_transformers(result) ⇒ Object
- #as_records! ⇒ Object
- #as_records? ⇒ Boolean
- #as_results! ⇒ Object
- #as_results? ⇒ Boolean
- #decorate! ⇒ Object (also: #decorated!)
- #decorate? ⇒ Boolean
- #decorate_record(record, decorator = nil) ⇒ Object
- #decorate_result(result, decorator = nil) ⇒ Object
- #decorated_with_hit(decorator = nil, &block) ⇒ Object
- #each(&block) ⇒ Object
- #each_decorated_records(decorator = nil, &block) ⇒ Object (also: #decorated_records, #each_decorated_record)
- #each_decorated_results(decorator = nil, &block) ⇒ Object (also: #decorated_results, #each_decorated_result)
-
#enumerable_method ⇒ Object
Enumeration ####.
- #map(&block) ⇒ Object
- #map_decorated_records(decorator = nil, &block) ⇒ Object
- #map_decorated_results(decorator = nil, &block) ⇒ Object
- #records_with_hit(&block) ⇒ Object
- #result_type ⇒ Object
- #to_ary ⇒ Object (also: #to_a)
-
#transform(&block) ⇒ Object
Decoration & Transformations ####.
- #transformers ⇒ Object
- #undecorate! ⇒ Object (also: #undecorated!)
- #undecorate? ⇒ Boolean
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/lycra/search/enumerable.rb', line 4 def self.included(base) base.send :delegate, :count, :size, :results, :records, :total_count, to: :response base.send :delegate, :first, to: :to_a base.send :alias_method, :total, :total_count end |
Instance Method Details
#apply_transformers(result) ⇒ Object
103 104 105 106 107 |
# File 'lib/lycra/search/enumerable.rb', line 103 def apply_transformers(result) transformers.each do |transformer| transformer.call(result) end end |
#as_records! ⇒ Object
20 21 22 23 |
# File 'lib/lycra/search/enumerable.rb', line 20 def as_records! @result_type = :records self end |
#as_records? ⇒ Boolean
29 30 31 |
# File 'lib/lycra/search/enumerable.rb', line 29 def as_records? @result_type == :records end |
#as_results! ⇒ Object
15 16 17 18 |
# File 'lib/lycra/search/enumerable.rb', line 15 def as_results! @result_type = :results self end |
#as_results? ⇒ Boolean
25 26 27 |
# File 'lib/lycra/search/enumerable.rb', line 25 def as_results? @result_type != :records end |
#decorate! ⇒ Object Also known as: decorated!
33 34 35 36 |
# File 'lib/lycra/search/enumerable.rb', line 33 def decorate! @decorated = true self end |
#decorate? ⇒ Boolean
45 46 47 |
# File 'lib/lycra/search/enumerable.rb', line 45 def decorate? @decorated == true end |
#decorate_record(record, decorator = nil) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/lycra/search/enumerable.rb', line 165 def decorate_record(record, decorator=nil) if decorator decorator.decorate(record) else record.decorate end end |
#decorate_result(result, decorator = nil) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/lycra/search/enumerable.rb', line 133 def decorate_result(result, decorator=nil) if decorator decorator.decorate(result) else SearchDecorator.decorate(result) end end |
#decorated_with_hit(decorator = nil, &block) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/lycra/search/enumerable.rb', line 182 def decorated_with_hit(decorator=nil, &block) @decorated_with_hit ||= records.map_with_hit do |record,hit| decorated = begin if decorator Hashie::Mash.new(record: decorator.decorate(record), hit: hit) else Hashie::Mash.new(record: record.decorate, hit: hit) end end apply_transformers(decorated) yield(decorated) if block_given? decorated end end |
#each(&block) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lycra/search/enumerable.rb', line 70 def each(&block) if decorate? send("each_#{enumerable_method}", &block) else send(enumerable_method).each do |result| apply_transformers(result) yield(result) if block_given? end end end |
#each_decorated_records(decorator = nil, &block) ⇒ Object Also known as: decorated_records, each_decorated_record
141 142 143 144 |
# File 'lib/lycra/search/enumerable.rb', line 141 def each_decorated_records(decorator=nil, &block) map_decorated_records(decorator, &block) @decorated_records end |
#each_decorated_results(decorator = nil, &block) ⇒ Object Also known as: decorated_results, each_decorated_result
109 110 111 112 |
# File 'lib/lycra/search/enumerable.rb', line 109 def each_decorated_results(decorator=nil, &block) map_decorated_results(decorator, &block) @decorated_results end |
#enumerable_method ⇒ Object
Enumeration ####
55 56 57 |
# File 'lib/lycra/search/enumerable.rb', line 55 def enumerable_method "#{'decorated_' if decorate?}#{result_type}".to_sym end |
#map(&block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lycra/search/enumerable.rb', line 81 def map(&block) if decorate? send("map_#{enumerable_method}", &block) else send(enumerable_method).map do |result| apply_transformers(result) yield(result) if block_given? end end end |
#map_decorated_records(decorator = nil, &block) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/lycra/search/enumerable.rb', line 148 def map_decorated_records(decorator=nil, &block) mapped = [] if @decorated_records mapped = @decorated_records.map(&block) else @decorated_records ||= records.map do |record| decorated = decorate_record(record, decorator) apply_transformers(decorated) mapped << (block_given? ? yield(decorated) : decorated) decorated end end mapped end |
#map_decorated_results(decorator = nil, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/lycra/search/enumerable.rb', line 116 def map_decorated_results(decorator=nil, &block) mapped = [] if @decorated_results mapped = @decorated_results.map(&block) else @decorated_results ||= results.map do |result| decorated = decorate_result(result, decorator) apply_transformers(decorated) mapped << (block_given? ? yield(decorated) : decorated) decorated end end mapped end |
#records_with_hit(&block) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/lycra/search/enumerable.rb', line 173 def records_with_hit(&block) @records_with_hit ||= records.map_with_hit do |record,hit| mash = Hashie::Mash.new(record: record, hit: hit) apply_transformers(mash) yield(mash) if block_given? mash end end |
#result_type ⇒ Object
11 12 13 |
# File 'lib/lycra/search/enumerable.rb', line 11 def result_type @result_type ||= :results end |
#to_ary ⇒ Object Also known as: to_a
59 60 61 62 63 64 65 66 67 |
# File 'lib/lycra/search/enumerable.rb', line 59 def to_ary if decorate? send(enumerable_method) else send(enumerable_method).each do |result| apply_transformers(result) end.to_a end end |
#transform(&block) ⇒ Object
Decoration & Transformations ####
94 95 96 97 |
# File 'lib/lycra/search/enumerable.rb', line 94 def transform(&block) (@transformers ||= []) << block self end |
#transformers ⇒ Object
99 100 101 |
# File 'lib/lycra/search/enumerable.rb', line 99 def transformers @transformers ||= [] end |
#undecorate! ⇒ Object Also known as: undecorated!
39 40 41 42 |
# File 'lib/lycra/search/enumerable.rb', line 39 def undecorate! @decorated = false self end |
#undecorate? ⇒ Boolean
49 50 51 |
# File 'lib/lycra/search/enumerable.rb', line 49 def undecorate? @decorated != true end |