Class: RiceBubble::Attributes::String
- Defined in:
- lib/rice_bubble/attributes/string.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Instance Method Summary collapse
- #call(value, path: '') ⇒ Object
- #description ⇒ Object
-
#initialize(min: nil, max: nil, format: nil) ⇒ String
constructor
A new instance of String.
- #valid?(value) ⇒ Boolean
- #valid_types ⇒ Object
Methods inherited from Base
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
#format ⇒ Object (readonly)
Returns the value of attribute format.
4 5 6 |
# File 'lib/rice_bubble/attributes/string.rb', line 4 def format @format end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
4 5 6 |
# File 'lib/rice_bubble/attributes/string.rb', line 4 def max @max end |
#min ⇒ Object (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 |
#description ⇒ Object
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
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_types ⇒ Object
13 14 15 |
# File 'lib/rice_bubble/attributes/string.rb', line 13 def valid_types [::String, Symbol] end |