Class: Terrazine::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/terrazine/presenter.rb

Overview

convinient for API presenter

Class Method Summary collapse

Class Method Details

.present(result, options) ⇒ Object

TODO: delete fields



6
7
8
9
10
11
12
13
14
# File 'lib/terrazine/presenter.rb', line 6

def present(result, options)
  if options[:array] || result.count > 1
    return [] if result.count.zero?
    result.map { |i| present_row i, options[:structure] }
  else
    return nil if result.count.zero?
    present_row result, options[:structure]
  end
end

.present_row(row, structure) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/terrazine/presenter.rb', line 16

def present_row(row, structure)
  hash = row.to_h
  if structure.present?
    structure.each do |k, v|
      hash[k] = present_value(row, v)
    end
  end
  hash.compact
end

.present_value(row, modifier) ⇒ Object

TODO!!!



27
28
29
30
31
32
33
34
35
36
# File 'lib/terrazine/presenter.rb', line 27

def present_value(row, modifier)
  case modifier
  when Result
    modifier.present
  when Proc
    present_value row, modifier.call(row)
  else
    modifier
  end
end