Module: Jsonize::Collection
- Defined in:
- lib/jsonize.rb
Instance Method Summary collapse
- #as_pure_json(context = {}) ⇒ Object
- #jsonize(context = {}) ⇒ Object
- #recontext(context, key) ⇒ Object
Instance Method Details
#as_pure_json(context = {}) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/jsonize.rb', line 198 def as_pure_json context = {} case self when Hash self.map do |key, value_in| new_context = recontext(context, key) value = value_in.respond_to?(:as_pure_json) ? value_in.as_pure_json(new_context) : value_in.as_json(new_context) [key.to_s, value] end.to_h when Array self.map.with_index do |value_in, index| new_context = recontext(context, index.to_i) value_in.respond_to?(:as_pure_json) ? value_in.as_pure_json(new_context) : value_in.as_json(new_context) end else as_json(context) end end |
#jsonize(context = {}) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/jsonize.rb', line 177 def jsonize context = {} method = case self when Hash :redisize_hash when Array :redisize_array end send(method) do as_pure_json(context) end end |
#recontext(context, key) ⇒ Object
191 192 193 194 195 196 |
# File 'lib/jsonize.rb', line 191 def recontext context, key context_only = [context[:only]].flatten(1).reduce(nil) { |r, x| r || (x.is_a?(Hash) ? x[key] : r) } context_except = [context[:except]].flatten(1).reduce(nil) { |r, x| r || (x.is_a?(Hash) ? x[key] : r) } context.merge(only: context_only, except: context_except) end |