Class: Kaya::Suites::Custom::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/kaya/suites/custom/params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_params_definition) ⇒ Params

It receives plain string with the definitions of each custom param

Text fields examples

      'a_text_field_param'                                   -> will be interpreted as a text field accepting any value
      'required_text_param:*'                                -> will be interpreted as a text field with a required value (* indicates required field)
      'with_a_default_value:foobar'                          -> will be interpreted as a text field with a default value
      'required_and_with_default:foobar:*'                   -> will be interpreted as a text field with a default value and as a required field

Select list examples
      'a_select_list:(value1|value2|value3)'                 -> will be interpreted as a select list with listed values. First is used as default value
      'none_default_select_list:(none|value1|value2|value3)' -> will be interpreted as a select list with listed values. First is used as default value and none indicates as empty value

Parameters:

  • custom (String)

    secion: a_text_field_param, required_text_param:*, with_a_default_value:foobar, required_and_with_default:foobar:* , a_select_list:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kaya/suites/custom/params.rb', line 27

def initialize custom_params_definition
  if custom_params_definition
    plain_params = custom_params_definition.gsub(/\s+,/,",").gsub(/\,\s+/,",").split(",")
    @params = plain_params.map do |param|
      send(type_of(param), param)
    end
  else
    @params = {}
  end
  @params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/kaya/suites/custom/params.rb', line 6

def params
  @params
end

Instance Method Details

#default_text_field(param) ⇒ Hash

Returns the structure of a text field with a default value

Parameters:

  • the (String)

    plain structure of a custom param

Returns:

  • (Hash)

    a hash with the required structure



124
125
126
127
128
129
130
131
132
133
# File 'lib/kaya/suites/custom/params.rb', line 124

def default_text_field param
  name, value = param.split ":"
  {
    "name" => name,
    "type" => "text",
    "options" => value,
    "required" => false,
    "value" => nil
  }
end

#required_default_text_field(param) ⇒ Hash

Returns the structure of a text field with a default value and required

Parameters:

  • the (String)

    plain structure of a custom param

Returns:

  • (Hash)

    a hash with the required structure



138
139
140
141
142
143
144
145
146
147
# File 'lib/kaya/suites/custom/params.rb', line 138

def required_default_text_field param
  name, value= param.split(":")
  {
    "name" => name,
    "type" => "text",
    "options" => value,
    "required" => true,
    "value" => nil
  }
end

#required_text_field(param) ⇒ Hash

Returns the structure of a required text field

Parameters:

  • the (String)

    plain structure of a custom param

Returns:

  • (Hash)

    a hash with the required structure



111
112
113
114
115
116
117
118
119
# File 'lib/kaya/suites/custom/params.rb', line 111

def required_text_field param
  {
    "name" => param.split(":").first,
    "type" => "text",
    "options" => nil,
    "required" => true,
    "value" => nil
  }
end

#select_list(param) ⇒ Hash

Returns the structure of a select list element

Parameters:

  • the (String)

    plain structure of a custom param

Returns:

  • (Hash)

    a hash with the required structure



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kaya/suites/custom/params.rb', line 73

def select_list param
  plus = Hash.new
  name, values = param.split(":")
  values.gsub!(/\(|\)/, "")
  values = values.split("|").map do |val|
    if val.include? "*"
      h = val.split("*")
      plus[h[0]] = h[1]
      val = h[0]
    end
    val
  end
  {
    "name" => name,
    "type" => "select_list",
    "options" => values,
    "plus_options" => plus,
    "required" => nil,
    "value" => nil
  }
end

#single_text_field(param) ⇒ Hash

Returns the structure of a single text field

Parameters:

  • the (String)

    plain structure of a custom param

Returns:

  • (Hash)

    a hash with the required structure



98
99
100
101
102
103
104
105
106
# File 'lib/kaya/suites/custom/params.rb', line 98

def single_text_field param
  {
    "name" => param.split.first,
    "type" => "text",
    "options" => nil,
    "required" => nil,
    "value" => nil
  }
end

#type_of(entire_param) ⇒ Object

Returns the type of the parameter according to its structure



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kaya/suites/custom/params.rb', line 41

def type_of entire_param
  parts = entire_param.split(":")

  if parts.last =~ /^\(.*\)$/ # It is a select list

    "select_list"

  else # It is a text field

    case parts.size

    when 1
      "single_text_field"

    when 2
      return "required_text_field" if parts.last == "*"
      "default_text_field"

    when 3
      if parts.last == "*"
        "required_default_text_field"
      else
        raise "Malformed custom param"
      end
    end
  end

end