Class: BeerDb::Model::BeerSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/beerdb/serializers/beer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beer) ⇒ BeerSerializer

Returns a new instance of BeerSerializer.

[View source]

7
8
9
# File 'lib/beerdb/serializers/beer.rb', line 7

def initialize( beer )
  @beer = beer
end

Instance Attribute Details

#beerObject (readonly)

Returns the value of attribute beer.


11
12
13
# File 'lib/beerdb/serializers/beer.rb', line 11

def beer
  @beer
end

Instance Method Details

#as_jsonObject

[View source]

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/beerdb/serializers/beer.rb', line 13

def as_json
  ## note: as_json returns record as a hash
  ##   note: NOT yet converted with to_json or JSON.pretty_generate etc.

  brewery = {}
  if beer.brewery.present?
    brewery = { key: beer.brewery.key,
                title: beer.brewery.title }
  end

  tags = []
  if beer.tags.present?
    beer.tags.each { |tag| tags << tag.key }
  end

  country = {
    key:   beer.country.key,
    title: beer.country.title
  }

  data = { key:      beer.key,
           title:    beer.title,
           synonyms: beer.synonyms,
           abv:      beer.abv,
           srm:      beer.srm,
           og:       beer.og,
           tags:     tags,
           brewery: brewery,
           country: country }

  data
end