Class: GoaModelGen::Model

Inherits:
Type
  • Object
show all
Defined in:
lib/goa_model_gen/type.rb

Constant Summary

Constants inherited from Type

Type::TIME_TYPE_PATTERN

Instance Attribute Summary collapse

Attributes inherited from Type

#fields, #name

Instance Method Summary collapse

Methods inherited from Type

#assign_field_type_base, #field_diffs, #field_type_packages, #has_time_field?

Constructor Details

#initialize(name, attrs) ⇒ Model

Returns a new instance of Model.



42
43
44
45
46
47
48
49
50
# File 'lib/goa_model_gen/type.rb', line 42

def initialize(name, attrs)
  super(name, attrs)
  @base = attrs['base']
  @enum_path = attrs['enum']
  @goon = attrs['goon']
  @payload_name = check_blank(attrs['payload']){ store? ? "#{@name}Payload" : @name }
  @media_type_name = check_blank(attrs['media_type']){ @name }
  @conv = attrs['conv'] || 'generate'
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



39
40
41
# File 'lib/goa_model_gen/type.rb', line 39

def base
  @base
end

#convObject (readonly)

Returns the value of attribute conv.



40
41
42
# File 'lib/goa_model_gen/type.rb', line 40

def conv
  @conv
end

#enum_itemsObject (readonly)

Returns the value of attribute enum_items.



35
36
37
# File 'lib/goa_model_gen/type.rb', line 35

def enum_items
  @enum_items
end

#enum_pathObject (readonly)

Returns the value of attribute enum_path.



35
36
37
# File 'lib/goa_model_gen/type.rb', line 35

def enum_path
  @enum_path
end

#goonObject (readonly)

Returns the value of attribute goon.



38
39
40
# File 'lib/goa_model_gen/type.rb', line 38

def goon
  @goon
end

#media_typeObject (readonly)

Returns the value of attribute media_type.



36
37
38
# File 'lib/goa_model_gen/type.rb', line 36

def media_type
  @media_type
end

#media_type_nameObject (readonly)

Returns the value of attribute media_type_name.



37
38
39
# File 'lib/goa_model_gen/type.rb', line 37

def media_type_name
  @media_type_name
end

#payloadObject (readonly)

Returns the value of attribute payload.



36
37
38
# File 'lib/goa_model_gen/type.rb', line 36

def payload
  @payload
end

#payload_nameObject (readonly)

Returns the value of attribute payload_name.



37
38
39
# File 'lib/goa_model_gen/type.rb', line 37

def payload_name
  @payload_name
end

Instance Method Details

#assign_swagger_types(loader) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/goa_model_gen/type.rb', line 99

def assign_swagger_types(loader)
  GoaModelGen.logger.debug "assign_swagger_types for #{name.inspect}"
  # Original Type: VmDisk
  # Swagger type : VmDisk
  # Goa struct   : VMDisk
  # Goa struct name is a little different from others.
  # Use underscore and camelize to regularize it.
  if !fields.empty?
    @payload = loader.load(to_swagger_name(payload_name)) if payload_name
    @media_type = loader.load(media_type_name) if media_type_name
  elsif enum_path
    @enum_items = loader.dig(enum_path)
  end
end

#check_blank(s) ⇒ Object



52
53
54
55
# File 'lib/goa_model_gen/type.rb', line 52

def check_blank(s)
  s.nil? ? (block_given? ? yield : nil) :
    (s.empty? || s == '-') ? nil : s
end

#field_by(name) ⇒ Object



138
139
140
# File 'lib/goa_model_gen/type.rb', line 138

def field_by(name)
  fields.detect{|f| f.name == name}
end

#fields_including_idObject



130
131
132
133
134
135
136
# File 'lib/goa_model_gen/type.rb', line 130

def fields_including_id
  if goon && goon['id_type']
    [id_field] + fields
  else
    fields
  end
end

#gen_converter?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/goa_model_gen/type.rb', line 122

def gen_converter?
  (conv == 'generate') && !fields.empty?
end

#id_definitionObject



74
75
76
# File 'lib/goa_model_gen/type.rb', line 74

def id_definition
  goon && "#{id_name} #{id_golang_type } `datastore:\"-\" goon:\"id\" json:\"#{ id_name.underscore }\"`"
end

#id_fieldObject



126
127
128
# File 'lib/goa_model_gen/type.rb', line 126

def id_field
  @id_field ||= Field.new(id_name, {'type' => goon['id_type']})
end

#id_golang_typeObject



57
58
59
60
61
62
63
# File 'lib/goa_model_gen/type.rb', line 57

def id_golang_type
  if goon && goon['id_type'] == 'UUID'
    return 'string'
  else
    goon && goon['id_type']
  end
end

#id_nameObject



65
66
67
# File 'lib/goa_model_gen/type.rb', line 65

def id_name
  goon && (goon['id_name'] || 'Id')
end

#id_name_varObject



69
70
71
72
# File 'lib/goa_model_gen/type.rb', line 69

def id_name_var
  s = id_name
  s.blank? ? nil : s[0].downcase + s[1..-1]
end

#key_id_methodObject



91
92
93
94
95
96
97
# File 'lib/goa_model_gen/type.rb', line 91

def key_id_method
  case id_golang_type
  when 'int64' then 'IntID'
  when 'string' then 'StringID'
  else 'UnknownTypeID'
  end
end

#media_type_name_for_goObject



118
119
120
# File 'lib/goa_model_gen/type.rb', line 118

def media_type_name_for_go
  Goa.capitalize_join(media_type_name.split('-').map(&:underscore).map{|n| n.split('_')}.flatten)
end

#parentObject



78
79
80
# File 'lib/goa_model_gen/type.rb', line 78

def parent
  goon && goon['parent']
end

#store?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/goa_model_gen/type.rb', line 82

def store?
  !!goon
end

#to_swagger_name(name) ⇒ Object



114
115
116
# File 'lib/goa_model_gen/type.rb', line 114

def to_swagger_name(name)
  name.split('-').map{|n| n.underscore.camelize }.join('-')
end

#use_uuid?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/goa_model_gen/type.rb', line 87

def use_uuid?
  goon && (goon['id_type'] == 'UUID')
end