Class: StateGate::Type
- Inherits:
-
ActiveModel::Type::String
- Object
- ActiveModel::Type::String
- StateGate::Type
- Defined in:
- lib/state_gate/type.rb
Overview
Description
ActiveRecord::Type to cast a model attribute as a StateGate, mapping to a string database column.
Ensures that any string written to, or read from, the database is a valid state
, otherwise it raises an exception.
This class is has an internal API for ActiveRecord and is not intended for public use.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Returns TRUE if the other class is equal, otherewise FALSE.
-
#assert_valid_value(value) ⇒ Object
Raise an exception unless the value is both serializable and a legitimate state.
-
#cast(value) ⇒ Object
ensure the value is a legitimate state and return a downcased string.
-
#deserialize(value) ⇒ Object
Convert a nil DB value to the default state.
-
#hash ⇒ Object
Returns a unique hash value.
-
#serializable?(value) ⇒ Boolean
Return TRUE if the value is serializable, otherwise FASLE.
-
#serialize(value) ⇒ Object
Return a downcased String of the given value, providing it is a legitimate state.
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Returns TRUE if the other class is equal, otherewise FALSE.
Equality matches on Class, name and states(in the given order)
86 87 88 89 90 91 92 93 |
# File 'lib/state_gate/type.rb', line 86 def ==(other) # :nodoc: return false unless self.class == other.class return false unless klass == other.send(:klass) return false unless name == other.send(:name) return false unless states == other.send(:states) true end |
#assert_valid_value(value) ⇒ Object
Raise an exception unless the value is both serializable and a legitimate state
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/state_gate/type.rb', line 66 def assert_valid_value(value) # :nodoc: return if serializable?(value) && states.include?(value.to_s.downcase.remove(/^force_/)) case value when NilClass fail ArgumentError, "'nil' is not a valid state for #{@klass}##{@name}." when Symbol fail ArgumentError, ":#{value} is not a valid state for #{@klass}##{@name}." else fail ArgumentError, "'#{value&.to_s}' is not a valid state for #{@klass}##{@name}." end end |
#cast(value) ⇒ Object
ensure the value is a legitimate state and return a downcased string
22 23 24 25 |
# File 'lib/state_gate/type.rb', line 22 def cast(value) # :nodoc: assert_valid_value(value) cast_value(value) end |
#deserialize(value) ⇒ Object
Convert a nil DB value to the default state.
55 56 57 58 59 |
# File 'lib/state_gate/type.rb', line 55 def deserialize(value) # :nodoc: return value if value klass.constantize.stateables[name].default_state end |
#hash ⇒ Object
Returns a unique hash value
101 102 103 |
# File 'lib/state_gate/type.rb', line 101 def hash # :nodoc: [self.class, klass, name, states].hash end |
#serializable?(value) ⇒ Boolean
Return TRUE if the value is serializable, otherwise FASLE.
a value is serializable if it can be coerced to a String
34 35 36 37 38 |
# File 'lib/state_gate/type.rb', line 34 def serializable?(value) # :nodoc: value.to_s rescue NoMethodError false end |
#serialize(value) ⇒ Object
Return a downcased String of the given value, providing it is a legitimate state.
45 46 47 48 |
# File 'lib/state_gate/type.rb', line 45 def serialize(value) # :nodoc: assert_valid_value(value) value.to_s.downcase.remove(/^force_/) end |