Module: ReeEnum::DSL::ClassMethods

Defined in:
lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb

Instance Method Summary collapse

Instance Method Details

#enum(name, &proc) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb', line 19

def enum(name, &proc)
  dsl = Ree::ObjectDsl.new(
    Ree.container.packages_facade, name, self, :object
  )

  dsl.instance_exec(&proc) if block_given?

  dsl.tags(["object", "enum"])

  klass = dsl.object.klass
  klass.send(:include, ReeEnum::Enumerable)
  klass.setup_enum(dsl.object.name)
  Ree.container.compile(dsl.package, name)
end

#register_as_mapper_typeObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb', line 64

def register_as_mapper_type
  register_as_swagger_type

  mapper_factory = ReeMapper.get_mapper_factory(
    Object.const_get(self.name.split('::').first)
  )

  mapper_factory.register_type(
    self.get_enum_name, type_for_mapper
  )
end

#register_as_swagger_typeObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb', line 50

def register_as_swagger_type
  swagger_type_registrator = ReeSwagger::RegisterType.new

  [:casters, :serializers].each do |kind|
    swagger_type_registrator.call(
      kind,
      type_for_mapper.class,
      ->(type, _build_schema) {
        type.enum.swagger_definition
      }
    )
  end
end

#swagger_definitionObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb', line 76

def swagger_definition
  return @swagger_definition if defined? @swagger_definition

  value_type = get_values.value_type

  type = if value_type == String
    "string"
  elsif value_type == Integer
    "integer"
  else
    raise NotImplementedError, "value_type #{value_type} is not supported"
  end

  @swagger_definition = {
    type: type,
    enum: get_values.each.map(&:value)
  }
end

#type_for_mapperObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb', line 34

def type_for_mapper
  return @type_for_mapper if defined? @type_for_mapper

  value_type = get_values.value_type

  klass = if value_type == String
    StringValueEnumMapper
  elsif value_type == Integer
    IntegerValueEnumMapper
  else
    raise NotImplementedError, "value_type #{value_type} is not supported"
  end

  @type_for_mapper = klass.new(self)
end