File: serializing.rb

package info (click to toggle)
ruby-grape-entity 0.7.1-1~bpo9%2B1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 440 kB
  • sloc: ruby: 3,037; makefile: 6; sh: 4
file content (100 lines) | stat: -rw-r--r-- 2,313 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'grape-entity'
require 'benchmark'

module Models
  class School
    attr_reader :classrooms
    def initialize
      @classrooms = []
    end
  end

  class ClassRoom
    attr_reader :students
    attr_accessor :teacher
    def initialize(opts = {})
      @teacher = opts[:teacher]
      @students = []
    end
  end

  class Person
    attr_accessor :name
    def initialize(opts = {})
      @name = opts[:name]
    end
  end

  class Teacher < Models::Person
    attr_accessor :tenure
    def initialize(opts = {})
      super(opts)
      @tenure = opts[:tenure]
    end
  end

  class Student < Models::Person
    attr_reader :grade
    def initialize(opts = {})
      super(opts)
      @grade = opts[:grade]
    end
  end
end

module Entities
  class School < Grape::Entity
    expose :classrooms, using: 'Entities::ClassRoom'
  end

  class ClassRoom < Grape::Entity
    expose :teacher, using: 'Entities::Teacher'
    expose :students, using: 'Entities::Student'
    expose :size do |model, _opts|
      model.students.count
    end
  end

  class Person < Grape::Entity
    expose :name
  end

  class Student < Entities::Person
    expose :grade
    expose :failing do |model, _opts|
      model.grade == 'F'
    end
  end

  class Teacher < Entities::Person
    expose :tenure
  end
end

teacher1 = Models::Teacher.new(name: 'John Smith', tenure: 2)
classroom1 = Models::ClassRoom.new(teacher: teacher1)
classroom1.students << Models::Student.new(name: 'Bobby', grade: 'A')
classroom1.students << Models::Student.new(name: 'Billy', grade: 'B')

teacher2 = Models::Teacher.new(name: 'Lisa Barns')
classroom2 = Models::ClassRoom.new(teacher: teacher2, tenure: 15)
classroom2.students << Models::Student.new(name: 'Eric', grade: 'A')
classroom2.students << Models::Student.new(name: 'Eddie', grade: 'C')
classroom2.students << Models::Student.new(name: 'Arnie', grade: 'C')
classroom2.students << Models::Student.new(name: 'Alvin', grade: 'F')
school = Models::School.new
school.classrooms << classroom1
school.classrooms << classroom2

iters = 5000

Benchmark.bm do |bm|
  bm.report('serializing') do
    iters.times do
      Entities::School.represent(school, serializable: true)
    end
  end
end