Class: RiceBubble::Attributes::String

Inherits:
Base
  • Object
show all
Defined in:
lib/rice_bubble/attributes/string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#fetch, #optional

Constructor Details

#initialize(min: nil, max: nil, format: nil) ⇒ String

Returns a new instance of String.



6
7
8
9
10
11
# File 'lib/rice_bubble/attributes/string.rb', line 6

def initialize(min: nil, max: nil, format: nil, &)
  super(&)
  @min = min
  @max = max
  @format = format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'lib/rice_bubble/attributes/string.rb', line 4

def format
  @format
end

#maxObject (readonly)

Returns the value of attribute max.



4
5
6
# File 'lib/rice_bubble/attributes/string.rb', line 4

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



4
5
6
# File 'lib/rice_bubble/attributes/string.rb', line 4

def min
  @min
end

Instance Method Details

#call(value, path: '') ⇒ Object



24
25
26
# File 'lib/rice_bubble/attributes/string.rb', line 24

def call(value, path: '')
  super.to_s
end

#descriptionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rice_bubble/attributes/string.rb', line 28

def description
  result = super

  if min && max
    result = "#{result} between #{min} and #{max} characters long"
  elsif min
    result = "#{result} of at least #{min} characters"
  elsif max
    result = "#{result} of up to #{max} characters"
  end

  result = "#{result} matching #{format.inspect}" if format

  result
end

#valid?(value) ⇒ Boolean

Returns:



17
18
19
20
21
22
# File 'lib/rice_bubble/attributes/string.rb', line 17

def valid?(value)
  super &&
    !min&.send(:>, value.to_s.length) &&
    !max&.send(:<, value.to_s.length) &&
    (!format || format.match?(value.to_s))
end

#valid_typesObject



13
14
15
# File 'lib/rice_bubble/attributes/string.rb', line 13

def valid_types
  [::String, Symbol]
end