Class: FsrsRuby::TypeConverter
- Inherits:
-
Object
- Object
- FsrsRuby::TypeConverter
- Defined in:
- lib/fsrs_ruby/type_converter.rb
Overview
Type conversion utilities for normalizing inputs
Class Method Summary collapse
-
.card(card_input) ⇒ Card
Normalize Card object.
-
.rating(value) ⇒ Integer
Convert string or integer to Rating constant.
-
.state(value) ⇒ Integer
Convert string or integer to State constant.
-
.time(value) ⇒ Time
Convert various time formats to Time object.
Class Method Details
.card(card_input) ⇒ Card
Normalize Card object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fsrs_ruby/type_converter.rb', line 55 def self.card(card_input) return card_input if card_input.is_a?(Card) Card.new( due: time(card_input[:due]), stability: card_input[:stability] || 0.0, difficulty: card_input[:difficulty] || 0.0, elapsed_days: card_input[:elapsed_days] || 0, scheduled_days: card_input[:scheduled_days] || 0, learning_steps: card_input[:learning_steps] || 0, reps: card_input[:reps] || 0, lapses: card_input[:lapses] || 0, state: state(card_input[:state] || State::NEW), last_review: card_input[:last_review] ? time(card_input[:last_review]) : nil ) end |
.rating(value) ⇒ Integer
Convert string or integer to Rating constant
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fsrs_ruby/type_converter.rb', line 40 def self.(value) case value when Integer raise ArgumentError, "Invalid rating: #{value}" unless Rating.valid?(value) value when String, Symbol Rating.from_string(value.to_s) else raise ArgumentError, "Invalid rating: #{value}" end end |
.state(value) ⇒ Integer
Convert string or integer to State constant
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fsrs_ruby/type_converter.rb', line 25 def self.state(value) case value when Integer raise ArgumentError, "Invalid state: #{value}" unless State.valid?(value) value when String, Symbol State.from_string(value.to_s) else raise ArgumentError, "Invalid state: #{value}" end end |
.time(value) ⇒ Time
Convert various time formats to Time object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fsrs_ruby/type_converter.rb', line 9 def self.time(value) case value when Time value when Integer Time.at(value) when String Time.parse(value) else raise ArgumentError, "Invalid time: #{value}" end end |