Files
redux-scraper/app/models/global_state.rb
Dylan Knutson 0a651bab9d Enhance strict typing and refactor models for improved type safety
- Updated various Ruby files to enforce strict typing with Sorbet, including controllers, jobs, and models.
- Refactored method signatures across multiple classes to ensure better type checking and documentation.
- Introduced `requires_ancestor` in several modules to enforce class hierarchy requirements.
- Enhanced the `DbSampler` and `Scraper` classes with type signatures for better clarity and maintainability.
- Added new methods in the `Domain::Fa::User` class for scan management, improving functionality and type safety.

These changes aim to enhance the overall stability and maintainability of the codebase.
2025-01-01 23:14:27 +00:00

58 lines
1.3 KiB
Ruby

# typed: strict
class GlobalState < ReduxApplicationRecord
validates :key, presence: true, uniqueness: true
validates :value, presence: true
validates :value_type, presence: true
enum :value_type,
{ string: 0, counter: 1, duration: 2, password: 3 },
prefix: :value_type
validate :validate_value_format
sig { params(key: String).returns(T.nilable(String)) }
def self.get(key)
find_by(key: key)&.value
end
sig { params(key: String, value: String).returns(String) }
def self.set(key, value)
record = find_or_initialize_by(key: key)
record.value = value
record.save!
value
end
sig { returns(T.nilable(String)) }
def display_value
case value_type&.to_sym
when :password
"••••••••"
else
value
end
end
private
sig { void }
def validate_value_format
value_type = self.value_type || return
value = self.value || return
case value_type.to_sym
when :counter
unless value.match?(/\A-?\d+\z/)
errors.add(:value, "must be a valid integer for counter type")
end
when :duration
unless value.match?(/\A\d+[smhd]\z/)
errors.add(
:value,
"must be a valid duration (e.g., '30s', '5m', '2h', '1d')",
)
end
end
end
end