Class: Coercible::Coercer::String
- Extended by:
- Configurable
- Defined in:
- lib/project/coercer/string.rb
Overview
Coerce String values
Constant Summary collapse
- TRUE_VALUES =
%w[ 1 on t true y yes ].freeze
- FALSE_VALUES =
%w[ 0 off f false n no ].freeze
- BOOLEAN_MAP =
::Hash[ TRUE_VALUES.product([ true ]) + FALSE_VALUES.product([ false ]) ].freeze
- INTEGER_REGEXP =
/[-+]?(?:[0-9]\d*)/.freeze
- EXPONENT_REGEXP =
/(?:[eE][-+]?\d+)/.freeze
- FRACTIONAL_REGEXP =
/(?:\.\d+)/.freeze
- NUMERIC_REGEXP =
/\A( #{INTEGER_REGEXP}#{FRACTIONAL_REGEXP}?#{EXPONENT_REGEXP}? | #{FRACTIONAL_REGEXP}#{EXPONENT_REGEXP}? )\z/x.freeze
Constants inherited from Object
Object::COERCION_METHOD_REGEXP
Constants included from Options
Constants included from TypeLookup
Instance Attribute Summary collapse
-
#boolean_map ⇒ ::Hash
readonly
private
Return boolean map from the config.
Attributes inherited from Object
Class Method Summary collapse
-
.config ⇒ Configuration
private
Return default configuration for string coercer type.
Instance Method Summary collapse
-
#initialize(coercer = Coercer.new, config = self.class.config) ⇒ undefined
constructor
private
Initialize a new string coercer instance.
-
#to_boolean(value) ⇒ Boolean
Coerce value to TrueClass or FalseClass.
-
#to_constant(value) ⇒ Object
Coerce give value to a constant.
-
#to_date(value) ⇒ Date
Coerce given value to Date.
-
#to_datetime(value) ⇒ DateTime
Coerce given value to DateTime.
-
#to_decimal(value) ⇒ BigDecimal
Coerce value to decimal.
-
#to_float(value) ⇒ Float
Coerce value to float.
-
#to_integer(value) ⇒ Integer
Coerce value to integer.
-
#to_symbol(value) ⇒ Symbol
Coerce give value to a symbol.
-
#to_time(value) ⇒ Time
Coerce given value to Time.
Methods included from Configurable
config, config_name, configuration_class, extended
Methods inherited from Object
#coerced?, #inspect, #to_array, #to_hash, #to_string
Methods included from Options
#accept_options, #accepted_options, extended, #options
Methods included from TypeLookup
Constructor Details
#initialize(coercer = Coercer.new, config = self.class.config) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new string coercer instance
53 54 55 56 |
# File 'lib/project/coercer/string.rb', line 53 def initialize(coercer = Coercer.new, config = self.class.config) super(coercer) @boolean_map = config.boolean_map end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Coercible::Coercer::Object
Instance Attribute Details
#boolean_map ⇒ ::Hash (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return boolean map from the config
42 43 44 |
# File 'lib/project/coercer/string.rb', line 42 def boolean_map @boolean_map end |
Class Method Details
.config ⇒ Configuration
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return default configuration for string coercer type
33 34 35 |
# File 'lib/project/coercer/string.rb', line 33 def self.config super { |config| config.boolean_map = BOOLEAN_MAP } end |
Instance Method Details
#to_boolean(value) ⇒ Boolean
Coerce value to TrueClass or FalseClass
143 144 145 146 147 |
# File 'lib/project/coercer/string.rb', line 143 def to_boolean(value) boolean_map.fetch(value.downcase) { raise_unsupported_coercion(value, __method__) } end |
#to_constant(value) ⇒ Object
Coerce give value to a constant
68 69 70 71 72 |
# File 'lib/project/coercer/string.rb', line 68 def to_constant(value) names = value.split('::') names.shift if names.first.empty? names.inject(::Object) { |*args| constant_lookup(*args) } end |
#to_date(value) ⇒ Date
Coerce given value to Date
112 113 114 |
# File 'lib/project/coercer/string.rb', line 112 def to_date(value) parse_value(::Date, value, __method__) end |
#to_datetime(value) ⇒ DateTime
Coerce given value to DateTime
126 127 128 |
# File 'lib/project/coercer/string.rb', line 126 def to_datetime(value) parse_value(::DateTime, value, __method__) end |
#to_decimal(value) ⇒ BigDecimal
Coerce value to decimal
197 198 199 200 201 |
# File 'lib/project/coercer/string.rb', line 197 def to_decimal(value) to_numeric(value, :to_d) rescue UnsupportedCoercion raise_unsupported_coercion(value, __method__) end |
#to_float(value) ⇒ Float
Coerce value to float
181 182 183 184 185 |
# File 'lib/project/coercer/string.rb', line 181 def to_float(value) to_numeric(value, :to_f) rescue UnsupportedCoercion raise_unsupported_coercion(value, __method__) end |
#to_integer(value) ⇒ Integer
Coerce value to integer
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/project/coercer/string.rb', line 159 def to_integer(value) if value =~ /\A#{INTEGER_REGEXP}\z/ value.to_i else # coerce to a Float first to evaluate scientific notation (if any) # that may change the integer part, then convert to an integer to_float(value).to_i end rescue UnsupportedCoercion raise_unsupported_coercion(value, __method__) end |
#to_symbol(value) ⇒ Symbol
Coerce give value to a symbol
84 85 86 |
# File 'lib/project/coercer/string.rb', line 84 def to_symbol(value) value.to_sym end |
#to_time(value) ⇒ Time
Coerce given value to Time
98 99 100 |
# File 'lib/project/coercer/string.rb', line 98 def to_time(value) parse_value(::Time, value, __method__) end |