Class: BeerDb::Model::BrewerySerializer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(brewery) ⇒ BrewerySerializer

Returns a new instance of BrewerySerializer.



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

def initialize( brewery )
  @brewery = brewery
end

Instance Attribute Details

#breweryObject (readonly)

Returns the value of attribute brewery.



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

def brewery
  @brewery
end

Instance Method Details

#as_jsonObject



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/brewery.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.

  beers = []
  brewery.beers.each do |b|
    beers << { key: b.key, title: b.title }
  end

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

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

  data = { key:      brewery.key,
           title:    brewery.title,
           synonyms: brewery.synonyms,
           since:    brewery.since,
           address:  brewery.address,
           web:      brewery.web,
           prod:     brewery.prod,  # (estimated) annual production in hl e.g. 2_000 hl
           tags:     tags,
           beers:    beers,
           country:  country }

  data
end