Class: Saphyr::Fields::FieldBase
- Inherits:
-
Object
- Object
- Saphyr::Fields::FieldBase
- Includes:
- Asserts::BaseAssert, Asserts::ErrorConstants, Asserts::NumericAssert, Asserts::SizeAssert, Asserts::StringAssert
- Defined in:
- lib/saphyr/fields/field_base.rb
Direct Known Subclasses
ArrayField, B64Field, BooleanField, DateTimeField, EmailField, FloatField, IntegerField, IpField, IsoCountryField, IsoLangField, SchemaField, StringField, UriField, UrlField
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
:requiredand:nullableoptions. {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
-
#opts ⇒ Object
readonly
A hash containing the options of the field.
Instance Method Summary collapse
-
#authorized_options ⇒ Array
Get the
AUTHORIZED_OPTIONSoptions. - #default? ⇒ Boolean
-
#err(code) ⇒ String
Format the error code with the field prefix.
-
#exclusive_options ⇒ Array
Get the
EXCLUSIVE_OPTIONSoptions. -
#expected_types ⇒ Array
Get the
EXPECTED_TYPESoptions. -
#initialize(opts = {}) ⇒ FieldBase
constructor
A new instance of FieldBase.
-
#not_equals_options ⇒ Array
Get the
NOT_EQUALS_OPTIONSoptions. -
#not_sup_options ⇒ Array
Get the
NOT_SUP_OPTIONSoptions. -
#not_sup_or_equals_options ⇒ Array
Get the
NOT_SUP_OR_EQUALS_OPTIONSoptions. -
#nullable? ⇒ Boolean
Is the field nullable?.
-
#prefix ⇒ String
Get the
PREFIXsetting. -
#required? ⇒ Boolean
Is the field required?.
-
#required_one_of_options ⇒ Array
Get the
REQUIRED_ONE_OF_OPTIONSoptions. -
#required_options ⇒ Array
Get the
REQUIRED_OPTIONSoptions. -
#validate(ctx, name, value) ⇒ Object
Check if the field value is valid.
Methods included from Asserts::StringAssert
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 .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 .include? opt raise Saphyr::Error.new "Unauthorized option: #{opt}" end end end .each do |opt| unless opts.include? opt raise Saphyr::Error.new "Missing required option: '#{opt}'" end end unless .size == 0 status, selected = false, nil .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 .each do |data| opt, excluded = data if opts.include? opt if excluded.first == :_all_ excluded = - [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 .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 .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 .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
#opts ⇒ Object (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_options ⇒ Array
Get the AUTHORIZED_OPTIONS options
180 181 182 |
# File 'lib/saphyr/fields/field_base.rb', line 180 def self.class::AUTHORIZED_OPTIONS end |
#default? ⇒ 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.
225 226 227 |
# File 'lib/saphyr/fields/field_base.rb', line 225 def err(code) prefix + ':' + code end |
#exclusive_options ⇒ Array
Get the EXCLUSIVE_OPTIONS options
198 199 200 |
# File 'lib/saphyr/fields/field_base.rb', line 198 def self.class::EXCLUSIVE_OPTIONS end |
#expected_types ⇒ Array
Get the EXPECTED_TYPES options
174 175 176 |
# File 'lib/saphyr/fields/field_base.rb', line 174 def expected_types self.class::EXPECTED_TYPES end |
#not_equals_options ⇒ Array
Get the NOT_EQUALS_OPTIONS options
204 205 206 |
# File 'lib/saphyr/fields/field_base.rb', line 204 def self.class::NOT_EQUALS_OPTIONS end |
#not_sup_options ⇒ Array
Get the NOT_SUP_OPTIONS options
210 211 212 |
# File 'lib/saphyr/fields/field_base.rb', line 210 def self.class::NOT_SUP_OPTIONS end |
#not_sup_or_equals_options ⇒ Array
Get the NOT_SUP_OR_EQUALS_OPTIONS options
216 217 218 |
# File 'lib/saphyr/fields/field_base.rb', line 216 def self.class::NOT_SUP_OR_EQUALS_OPTIONS end |
#nullable? ⇒ Boolean
Is the field nullable?
235 236 237 |
# File 'lib/saphyr/fields/field_base.rb', line 235 def nullable? @opts[:nullable] end |
#prefix ⇒ String
Get the PREFIX setting.
166 167 168 |
# File 'lib/saphyr/fields/field_base.rb', line 166 def prefix self.class::PREFIX end |
#required? ⇒ Boolean
Is the field required?
230 231 232 |
# File 'lib/saphyr/fields/field_base.rb', line 230 def required? @opts[:required] end |
#required_one_of_options ⇒ Array
Get the REQUIRED_ONE_OF_OPTIONS options
192 193 194 |
# File 'lib/saphyr/fields/field_base.rb', line 192 def self.class::REQUIRED_ONE_OF_OPTIONS end |
#required_options ⇒ Array
Get the REQUIRED_OPTIONS options
186 187 188 |
# File 'lib/saphyr/fields/field_base.rb', line 186 def self.class::REQUIRED_OPTIONS end |
#validate(ctx, name, value) ⇒ Object
Check if the field value is valid.
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 |