Class: Renshuu::Schedule

Inherits:
Model
  • Object
show all
Defined in:
lib/renshuu/models/schedule.rb

Overview

Model class for schedules.

Defined Under Namespace

Classes: NewTermCount, TermCount, TodayCount, UpcomingCount

Constant Summary collapse

BOOKTYPES =

Mapping between schedule booktypes and model classes.

See Also:

{
  'kanji' => Kanji, 'vocab' => Word, 'grammar' => Grammar,
  'sent' => Sentence
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#booktypeString (readonly)

Returns:

  • (String)


54
# File 'lib/renshuu/models/schedule.rb', line 54

attribute :booktype, Types::String

#idString (readonly)

Returns:

  • (String)


46
# File 'lib/renshuu/models/schedule.rb', line 46

attribute :id, Types::String

#is_frozenBoolean (readonly)

Returns:

  • (Boolean)


58
# File 'lib/renshuu/models/schedule.rb', line 58

attribute :is_frozen, Types::Params::Bool

#nameString (readonly)

Returns:

  • (String)


50
# File 'lib/renshuu/models/schedule.rb', line 50

attribute :name, Types::String

#new_termsNewTermCount (readonly)

Returns:



139
# File 'lib/renshuu/models/schedule.rb', line 139

attribute :new_terms, NewTermCount

#termsTermCount (readonly)

Returns:



122
# File 'lib/renshuu/models/schedule.rb', line 122

attribute :terms, TermCount

#todayObject (readonly)

rn [TodayCount]



80
# File 'lib/renshuu/models/schedule.rb', line 80

attribute :today, TodayCount

#upcomingArray<UpcomingCount> (readonly)

Returns:



97
# File 'lib/renshuu/models/schedule.rb', line 97

attribute :upcoming, Types::Array.of(UpcomingCount)

Class Method Details

.get(id) ⇒ Schedule

Retrieves a specific schedule from the API.

Parameters:

  • id (String, Integer)

Returns:

See Also:



37
38
39
40
41
# File 'lib/renshuu/models/schedule.rb', line 37

def self.get(id)
  body = Renshuu.client.query(:get, "v1/schedule/#{id}")

  body.fetch(:schedules).first.then { new(_1) }
end

.listArray<Schedule>

Lists all schedules of the current user.



24
25
26
27
28
# File 'lib/renshuu/models/schedule.rb', line 24

def self.list
  body = Renshuu.client.query(:get, 'v1/schedule')

  body.fetch(:schedules).map { new(_1) }
end

Instance Method Details

#add(item) ⇒ Void

Adds the given item to the schedule.

Parameters:

Returns:

  • (Void)

Raises:

  • (ArgumentError)

    If the type of the item does not match the schedule’s type

See Also:



169
170
171
172
173
174
175
# File 'lib/renshuu/models/schedule.rb', line 169

def add(item)
  unless item.is_a?(term_class)
    raise ArgumentError, "Expected a `#{term_class}`, got a `#{item.class}`"
  end

  item.add_to_schedule(self)
end

#contents(page: nil, group: nil) ⇒ Array<Kanji,Word,Grammar,Sentence>

Retrieves the terms contained in the schedule.

Parameters:

  • page (Integer, nil) (defaults to: nil)
  • group (Symbol, nil) (defaults to: nil)

    One of :all, :blocked, :studied, :notyetstudied, :cannot_study, :review_today, :mastery_1, :mastery_2, :mastery_3, :mastery_4, :mastery_5, :mastery_6, :mastery_7, :mastery_8 or :mastery_9.

Returns:



152
153
154
155
156
157
# File 'lib/renshuu/models/schedule.rb', line 152

def contents(page: nil, group: nil)
  params =  { pg: page, group: }.compact
  body = Renshuu.client.query(:get, "v1/schedule/#{id}/list", params:)

  body.dig(:contents, :terms).map { term_class.new(_1) }
end

#remove(item) ⇒ Void

Removes the given item from the schedule.

Parameters:

Returns:

  • (Void)

Raises:

  • (ArgumentError)

    If the type of the item does not match the schedule’s type

See Also:



187
188
189
190
191
192
193
# File 'lib/renshuu/models/schedule.rb', line 187

def remove(item)
  unless item.is_a?(term_class)
    raise ArgumentError, "Expected a `#{term_class}`, got a `#{item.class}`"
  end

  item.remove_from_schedule(self)
end

#term_classClass

Model class to use for this schedule’s terms.

Returns:

  • (Class)

Raises:

  • (KeyError)

See Also:

  • BOOKTYPES


202
203
204
205
206
# File 'lib/renshuu/models/schedule.rb', line 202

def term_class
  @term_class ||= BOOKTYPES.fetch(booktype) do |key|
    raise KeyError, "Unkown schedule type `#{key}`"
  end
end