Class: Etna::Clients::Magma::AttributeActionsConverter

Inherits:
ConverterBase
  • Object
show all
Defined in:
lib/etna/clients/magma/workflows/json_converters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConverterBase

convert_attribute_user_json_to_magma_json, convert_model_user_json_to_magma_json, convert_project_user_json_to_magma_json, #prettify

Constructor Details

#initialize(actions) ⇒ AttributeActionsConverter

Returns a new instance of AttributeActionsConverter.



46
47
48
# File 'lib/etna/clients/magma/workflows/json_converters.rb', line 46

def initialize(actions)
  @actions = JSON.parse(actions.to_json, symbolize_names: true)
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



45
46
47
# File 'lib/etna/clients/magma/workflows/json_converters.rb', line 45

def actions
  @actions
end

Instance Method Details

#camelize(action_name) ⇒ Object



50
51
52
# File 'lib/etna/clients/magma/workflows/json_converters.rb', line 50

def camelize(action_name)
  action_name.split('_').map(&:capitalize).join('')
end

#clazz_name(action_name) ⇒ Object



54
55
56
# File 'lib/etna/clients/magma/workflows/json_converters.rb', line 54

def clazz_name(action_name)
  "Etna::Clients::Magma::#{camelize(action_name)}Action"
end

#convertObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/etna/clients/magma/workflows/json_converters.rb', line 58

def convert
  actions.map do |action_json|
    # We use desc and attribute_type to be consistent
    #   with the other JSON actions...but the
    #   Magma Model takes type and description.
    if action_json[:action_name] == 'add_attribute'
      action_json[:type] = action_json.delete(:attribute_type)
      action_json[:description] = action_json.delete(:desc)
    elsif action_json[:action_name] == 'add_link'
      action_json[:links].first[:type] = action_json[:links].first.delete(:attribute_type)
      action_json[:links].last[:type] = action_json[:links].last.delete(:attribute_type)
    end

    clazz = Object.const_get(clazz_name(action_json[:action_name]))
    clazz.new(**action_json)
  rescue ArgumentError => e
    modified_message = "Exception while parsing #{action_json}.\n" + e.message
    raise ArgumentError.new(modified_message)
  end
end