Class: Saphyr::Fields::FieldBase

Inherits:
Object
  • Object
show all
Includes:
Asserts::BaseAssert, Asserts::ErrorConstants, Asserts::NumericAssert, Asserts::SizeAssert, Asserts::StringAssert
Defined in:
lib/saphyr/fields/field_base.rb

Constant Summary collapse

PREFIX =
Note:

You must Override this class constants in your field type class

Prefix use to format error code

'base'
DEFAULT_OPT_VALUES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Every field type has the :required and :nullable options.

{required: true, nullable: false, default: :_none_}.freeze
DEFAULT_OPTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

DEFAULT_OPT_VALUES.keys.freeze
EXPECTED_TYPES =
Note:

Overriding this class constant is mandatory.

List of expected classes for the field. It can be an array of class names, or a single class name. EX: [TrueClass, FalseClass] or Integer.

nil
AUTHORIZED_OPTIONS =
Note:

Override this class constant if you want to use this feature.

List of authorized options.

[]
REQUIRED_OPTIONS =
Note:

Override this class constant if you want to use this feature.

List of required options.

[]
REQUIRED_ONE_OF_OPTIONS =
Note:

Override this class constant if you want to use this feature.

Require one and only of the listed options.

[]
EXCLUSIVE_OPTIONS =
Note:

Override this class constant if you want to use this feature.

Definition of exclusive options

[]
NOT_EQUALS_OPTIONS =
Note:

Override this class constant if you want to use this feature.

List of options where value must not be equals to another option. (ex: min == max)

[]
NOT_SUP_OPTIONS =
Note:

Override this class constant if you want to use this feature.

List of options where value must not be superior to another option. (ex: lt > gt)

[]
NOT_SUP_OR_EQUALS_OPTIONS =
Note:

Override this class constant if you want to use this feature.

List of options where value must not be superior or equals to another option. (ex: lt >= gt)

[]

Constants included from Asserts::ErrorConstants

Asserts::ErrorConstants::ERR_BAD_FORMAT, Asserts::ErrorConstants::ERR_EQ, Asserts::ErrorConstants::ERR_GT, Asserts::ErrorConstants::ERR_GTE, Asserts::ErrorConstants::ERR_IN, Asserts::ErrorConstants::ERR_LEN, Asserts::ErrorConstants::ERR_LT, Asserts::ErrorConstants::ERR_LTE, Asserts::ErrorConstants::ERR_MAX, Asserts::ErrorConstants::ERR_MIN, Asserts::ErrorConstants::ERR_NOT_EMPTY, Asserts::ErrorConstants::ERR_NOT_NULLABLE, Asserts::ErrorConstants::ERR_REGEXP, Asserts::ErrorConstants::ERR_SIZE_EQ, Asserts::ErrorConstants::ERR_SIZE_LEN, Asserts::ErrorConstants::ERR_SIZE_MAX, Asserts::ErrorConstants::ERR_SIZE_MIN, Asserts::ErrorConstants::ERR_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Asserts::StringAssert

#assert_string_regexp

Methods included from Asserts::NumericAssert

#assert_numeric_gt, #assert_numeric_gte, #assert_numeric_lt, #assert_numeric_lte

Methods included from Asserts::SizeAssert

#assert_size_len, #assert_size_max, #assert_size_min

Methods included from Asserts::BaseAssert

#assert_boolean, #assert_class, #assert_eq, #assert_in, #assert_not_empty

Constructor Details

#initialize(opts = {}) ⇒ FieldBase

Returns a new instance of FieldBase.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/saphyr/fields/field_base.rb', line 63

def initialize(opts={})
  if expected_types.nil?
    raise Saphyr::Error.new "The 'EXPECTED_TYPES' constant must be defined"
  end
  if opts.key? :required
    unless assert_boolean opts[:required]
      raise Saphyr::Error.new "Option ':required' must be a Boolean"
    end
  end
  if opts.key? :nullable
    unless assert_boolean opts[:nullable]
      raise Saphyr::Error.new "Option ':nullable' must be a Boolean"
    end
  end
  if opts.key? :default
    unless assert_class expected_types, opts[:default], []
      raise Saphyr::Error.new "Option ':default' bad type. Expecting: '#{self.class::EXPECTED_TYPES.to_s}', got: '#{opts[:default].class.name}'"
    end
  end

  if authorized_options.size == 0
    opts.keys.each do |opt|
      unless [:required, :nullable, :default].include? opt
        raise Saphyr::Error.new "Options are not allowed for this field type: '#{opt}'"
      end
    end
  else
    opts.keys.each do |opt|
      next if opt == :required or opt == :nullable or opt == :default
      unless authorized_options.include? opt
        raise Saphyr::Error.new "Unauthorized option: #{opt}"
      end
    end
  end

  required_options.each do |opt|
    unless opts.include? opt
      raise Saphyr::Error.new "Missing required option: '#{opt}'"
    end
  end

  unless required_one_of_options.size == 0
    status, selected = false, nil
    required_one_of_options.each do |opt|
      if opts.include? opt
        if status
          raise Saphyr::Error.new "You can't provide both options at same time: '#{selected}' and '#{opt}'"
        end
        status = true
        selected = opt
      end
    end
    unless status
      raise Saphyr::Error.new "You must provide one of the following options: '#{required_one_of_options.to_s}'"
    end
  end

  exclusive_options.each do |data|
    opt, excluded = data
    if opts.include? opt
      if excluded.first == :_all_
        excluded = authorized_options - [opt]
      end
      unless opts.keys.intersection(excluded).size == 0
        raise Saphyr::Error.new "You can't use #{excluded.to_s} options, if you use #{opt.to_s} options, if you use : :#{opt}"
      end
    end
  end

  not_equals_options.each do |data|
    opt1, opt2 = data
    if opts.include? opt1 and opts.include? opt2
      if opts[opt1] == opts[opt2]
        raise Saphyr::Error.new "Option '#{opt1} cannot be > to '#{opt2}'"
      end
    end
  end

  not_sup_options.each do |data|
    opt1, opt2 = data
    if opts.include? opt1 and opts.include? opt2
      if opts[opt1] > opts[opt2]
        raise Saphyr::Error.new "Option '#{opt1} cannot be > to '#{opt2}'"
      end
    end
  end

  not_sup_or_equals_options.each do |data|
    opt1, opt2 = data
    if opts.include? opt1 and opts.include? opt2
      if opts[opt1] >= opts[opt2]
        raise Saphyr::Error.new "Option '#{opt1} cannot be >= to '#{opt2}'"
      end
    end
  end

  @opts = DEFAULT_OPT_VALUES.merge opts
end

Instance Attribute Details

#optsObject (readonly)

A hash containing the options of the field.



16
17
18
# File 'lib/saphyr/fields/field_base.rb', line 16

def opts
  @opts
end

Instance Method Details

#authorized_optionsArray

Get the AUTHORIZED_OPTIONS options

Returns:

  • (Array)


180
181
182
# File 'lib/saphyr/fields/field_base.rb', line 180

def authorized_options
  self.class::AUTHORIZED_OPTIONS
end

#default?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/saphyr/fields/field_base.rb', line 239

def default?
  @opts[:default] != :_none_
end

#err(code) ⇒ String

Format the error code with the field prefix.

Parameters:

  • code (String)

    The error code.

Returns:

  • (String)

    The formatted error code.



225
226
227
# File 'lib/saphyr/fields/field_base.rb', line 225

def err(code)
  prefix + ':' + code
end

#exclusive_optionsArray

Get the EXCLUSIVE_OPTIONS options

Returns:

  • (Array)


198
199
200
# File 'lib/saphyr/fields/field_base.rb', line 198

def exclusive_options
  self.class::EXCLUSIVE_OPTIONS
end

#expected_typesArray

Get the EXPECTED_TYPES options

Returns:

  • (Array)


174
175
176
# File 'lib/saphyr/fields/field_base.rb', line 174

def expected_types
  self.class::EXPECTED_TYPES
end

#not_equals_optionsArray

Get the NOT_EQUALS_OPTIONS options

Returns:

  • (Array)


204
205
206
# File 'lib/saphyr/fields/field_base.rb', line 204

def not_equals_options
  self.class::NOT_EQUALS_OPTIONS
end

#not_sup_optionsArray

Get the NOT_SUP_OPTIONS options

Returns:

  • (Array)


210
211
212
# File 'lib/saphyr/fields/field_base.rb', line 210

def not_sup_options
  self.class::NOT_SUP_OPTIONS
end

#not_sup_or_equals_optionsArray

Get the NOT_SUP_OR_EQUALS_OPTIONS options

Returns:

  • (Array)


216
217
218
# File 'lib/saphyr/fields/field_base.rb', line 216

def not_sup_or_equals_options
  self.class::NOT_SUP_OR_EQUALS_OPTIONS
end

#nullable?Boolean

Is the field nullable?

Returns:

  • (Boolean)


235
236
237
# File 'lib/saphyr/fields/field_base.rb', line 235

def nullable?
  @opts[:nullable]
end

#prefixString

Get the PREFIX setting.

Returns:

  • (String)

    The prefix



166
167
168
# File 'lib/saphyr/fields/field_base.rb', line 166

def prefix
  self.class::PREFIX
end

#required?Boolean

Is the field required?

Returns:

  • (Boolean)


230
231
232
# File 'lib/saphyr/fields/field_base.rb', line 230

def required?
  @opts[:required]
end

#required_one_of_optionsArray

Get the REQUIRED_ONE_OF_OPTIONS options

Returns:

  • (Array)


192
193
194
# File 'lib/saphyr/fields/field_base.rb', line 192

def required_one_of_options
  self.class::REQUIRED_ONE_OF_OPTIONS
end

#required_optionsArray

Get the REQUIRED_OPTIONS options

Returns:

  • (Array)


186
187
188
# File 'lib/saphyr/fields/field_base.rb', line 186

def required_options
  self.class::REQUIRED_OPTIONS
end

#validate(ctx, name, value) ⇒ Object

Check if the field value is valid.

Parameters:

  • ctx (Saphyr::Engine::Context)

    The engine context.

  • name (String)

    The field name.

  • value (String)

    The field value.



249
250
251
252
253
254
255
# File 'lib/saphyr/fields/field_base.rb', line 249

def validate(ctx, name, value)
  # NOTE: Nullable is handle by the engine.
  errors = []
  return errors unless assert_class self.class::EXPECTED_TYPES, value, errors
  do_validate ctx, name, value, errors
  errors
end