Class: GoonModelGen::Source::Field

Inherits:
Object
  • Object
show all
Includes:
Contextual
Defined in:
lib/goon_model_gen/source/field.rb

Instance Attribute Summary collapse

Attributes included from Contextual

#context

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs) ⇒ Field

Returns a new instance of Field.

Parameters:

  • name (String)
  • attrs (Hash<string,Object>)


17
18
19
20
21
22
23
# File 'lib/goon_model_gen/source/field.rb', line 17

def initialize(name, attrs)
  @name = name
  @type_name = attrs['type']
  @required = attrs['required']
  @unique = attrs['unique']
  @tags = attrs['tags']
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/goon_model_gen/source/field.rb', line 10

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



11
12
13
# File 'lib/goon_model_gen/source/field.rb', line 11

def required
  @required
end

#tagsObject (readonly)

Hash<string,Array> ex. for datastore, validate, json, etc…



13
14
15
# File 'lib/goon_model_gen/source/field.rb', line 13

def tags
  @tags
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



10
11
12
# File 'lib/goon_model_gen/source/field.rb', line 10

def type_name
  @type_name
end

#uniqueObject (readonly)

Returns the value of attribute unique.



12
13
14
# File 'lib/goon_model_gen/source/field.rb', line 12

def unique
  @unique
end

Instance Method Details

#build_tagsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/goon_model_gen/source/field.rb', line 25

def build_tags
  r = {}
  (tags || {}).each do |key, val|
    r[key] = val.is_a?(Array) ? val.dup : [val]
  end
  r['json'] ||= [name.underscore]
  if required
    r['validate'] = ['required'] + (r['validate'] || [])
  else
    r['json'] << 'omitempty'
  end
  return r
end