Class: Jets::Resource::Standardizer

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/resource/standardizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*definition) ⇒ Standardizer

Returns a new instance of Standardizer.



4
5
6
# File 'lib/jets/resource/standardizer.rb', line 4

def initialize(*definition)
  @definition = definition.flatten
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



3
4
5
# File 'lib/jets/resource/standardizer.rb', line 3

def definition
  @definition
end

Instance Method Details

#camelize(attributes) ⇒ Object



12
13
14
# File 'lib/jets/resource/standardizer.rb', line 12

def camelize(attributes)
  Jets::Camelizer.transform(attributes)
end

#standarize(definition) ⇒ Object



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
# File 'lib/jets/resource/standardizer.rb', line 16

def standarize(definition)
  first, second, third, _ = definition
  if definition.size == 1 && first.is_a?(Hash) # long form
    first # pass through
  elsif definition.size == 2 && second.is_a?(Hash) # medium form
    logical_id, attributes = first, second
    attributes.delete(:properties) if attributes[:properties].nil? || attributes[:properties].empty?
    { logical_id => attributes }
  elsif definition.size == 2 && second.is_a?(String) # short form
    logical_id, type = first, second
    { logical_id => {
        type: type
    }}
  elsif definition.size == 3 && (second.is_a?(String) || second.is_a?(NilClass))# short form
    logical_id, type, properties = first, second, third
    template = { logical_id => {
                   type: type
                }}
    attributes = template.values.first
    attributes[:properties] = properties unless properties.empty?
    template
  else # Dont understand this form
    raise "Invalid form provided. definition #{definition.inspect}"
  end
end

#templateObject



8
9
10
# File 'lib/jets/resource/standardizer.rb', line 8

def template
  camelize(standarize(@definition))
end