Class: Mutx::Tasks::Custom::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/mutx/tasks/custom/param.rb

Constant Summary collapse

VALID_typeS =
["text","select_list","json"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ Param

Returns a new instance of Param.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mutx/tasks/custom/param.rb', line 10

def initialize definition
  Mutx::Support::Log.debug "definition in new => #{definition}"
  id = definition["_id"] || Mutx::Database::MongoConnector.generate_id
  @id       = id
  @name     = definition["name"]
  @type     = definition["type"]
  @options  = definition["options"] || []
  # @required = definition["required"]=="on" || false
  @required = definition["required"]
  @value    = definition["value"]
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/mutx/tasks/custom/param.rb', line 6

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/mutx/tasks/custom/param.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/mutx/tasks/custom/param.rb', line 6

def options
  @options
end

#requiredObject (readonly)

Returns the value of attribute required.



6
7
8
# File 'lib/mutx/tasks/custom/param.rb', line 6

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/mutx/tasks/custom/param.rb', line 6

def type
  @type
end

Class Method Details

.delete(param_id) ⇒ Object



137
138
139
# File 'lib/mutx/tasks/custom/param.rb', line 137

def self.delete param_id
  Mutx::Database::MongoConnector.delete_custom_param param_id
end

.delete_this(data) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/mutx/tasks/custom/param.rb', line 78

def self.delete_this data
  if self.delete data["_id"]
    {action:"delete", success:true, message:"Custom Param #{data["name"]} deleted"}
  else
    {action:"delete", success:false, message:"Could not delete custom param #{data["name"]}"}
  end
end

.get(custom_id) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/mutx/tasks/custom/param.rb', line 22

def self.get custom_id
  data = Mutx::Database::MongoConnector.get_custom_param(custom_id)
  Mutx::Support::Log.debug "#{data}"
  raise "Could not get Custom Param CUSTOM-ID[#{custom_id}]" if data.nil?
  data["value"] = data["value"].to_json if data["type"]=="json"
  Mutx::Support::Log.debug "En self.get antes de hacer self.new(#{data})"
  self.new data
end

.insert!(definition) ⇒ Object



127
128
129
130
# File 'lib/mutx/tasks/custom/param.rb', line 127

def self.insert! definition
  definition.delete("action")
  Mutx::Database::MongoConnector.insert_custom_param(self.new(definition).structure)
end

.typesObject



54
55
56
# File 'lib/mutx/tasks/custom/param.rb', line 54

def self.types
  VALID_typeS
end

.update!(definition) ⇒ Object



132
133
134
135
# File 'lib/mutx/tasks/custom/param.rb', line 132

def self.update! definition
  definition.delete("action")
  Mutx::Database::MongoConnector.update_custom_param(self.new(definition).structure)
end

.validate(definition) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mutx/tasks/custom/param.rb', line 86

def self.validate definition
  errors = []
  if definition["action"] == "edit"

    errors << self.validate_name_with_id(definition)
  else
    errors << self.validate_name(definition)
  end
  errors << self.validate_type(definition["type"])
  errors << self.validate_options(definition["options"])
  errors << self.validate_value_for_type(definition)
  errors.compact
end

.validate_and_create(definition) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/mutx/tasks/custom/param.rb', line 58

def self.validate_and_create definition
  errors = validate(definition)
  return { success:false, message:errors.join(" ")} unless errors.empty?
  if insert!(definition)
    {action:"create", success:true, message:"Custom Param #{definition["name"]} created"}
  else
    {action:"create", success:false, message:"Custom Param #{definition["name"]} could not be created"}
  end
end

.validate_and_update(definition) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/mutx/tasks/custom/param.rb', line 68

def self.validate_and_update definition
  errors = self.validate(definition)
  return { success:false, message:errors.join(" ")} unless errors.empty?
  if self.update! definition
    {action:"edit", success:true, message:"Custom Param #{definition["name"]} updated"}
  else
    {action:"edit", success:false, message:"Custom Param #{definition["name"]} could not be updated"}
  end
end

.validate_json_format(definition) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/mutx/tasks/custom/param.rb', line 109

def self.validate_json_format definition
  Mutx::Support::Log.debug "Value is a => #{definition['value'].class} => #{definition["value"]}"
  begin
    value = JSON.parse(definition["value"])
    Mutx::Support::Log.debug "NOW Value is a => #{value.class} => #{value}"
    nil
  rescue
    return "Invalid json format"
  end
end

.validate_name(definition) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/mutx/tasks/custom/param.rb', line 141

def self.validate_name definition
  return "Custom param name not povided" if definition["name"].nil?
  if (param_id = Mutx::Database::MongoConnector.param_id_for_name(definition["name"]))
    return "A custom param with the name #{definition['name']} already exists (#{param_id})"
  end
  self.validate_special_chars_for_name definition
end

.validate_name_with_id(definition) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/mutx/tasks/custom/param.rb', line 149

def self.validate_name_with_id definition
  db_id = Mutx::Database::MongoConnector.param_id_for_name(definition["name"]).to_i
  unless db_id.zero?
    return "There is another custom param with the name (#{definition['name']})" if db_id != definition["_id"]
  end
  self.validate_special_chars_for_name definition
end

.validate_options(options) ⇒ Object



166
167
# File 'lib/mutx/tasks/custom/param.rb', line 166

def self.validate_options options
end

.validate_required_for_select(definition) ⇒ Object



120
121
122
123
124
125
# File 'lib/mutx/tasks/custom/param.rb', line 120

def self.validate_required_for_select definition
  # cannot define as required select list with no values on options
  if definition["required"]
     return "At least one value on options field must be defined for required Select List custom parameter." if definition["options"].size.zero?
  end
end

.validate_special_chars_for_name(definition) ⇒ Object



157
158
159
# File 'lib/mutx/tasks/custom/param.rb', line 157

def self.validate_special_chars_for_name definition
  return "Only word chars are allowed in name definition" if definition["name"].gsub(" ","") =~ /\W/
end

.validate_type(type) ⇒ Object



161
162
163
164
# File 'lib/mutx/tasks/custom/param.rb', line 161

def self.validate_type type
  return "You have to select type of custom param" if type == "Select..."
  "Invalid type '#{type}'" unless VALID_typeS.include? type.downcase
end

.validate_value_for_type(definition) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/mutx/tasks/custom/param.rb', line 100

def self.validate_value_for_type definition
  case definition["type"]
  when "select_list"
    self.validate_required_for_select definition
  when "json"
    self.validate_json_format definition
  end
end

Instance Method Details

#get_valueObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mutx/tasks/custom/param.rb', line 42

def get_value
  if self.type == "json"
    begin
      value = JSON.parse(@value)
    rescue
      @value
    end
  else
    @value
  end
end

#structureObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/mutx/tasks/custom/param.rb', line 31

def structure
  {
    "_id" => id,
    "name" => name,
    "type" => type,
    "options" => options,
    "required" => required,
    "value" => get_value
  }
end