Class: Renshuu::List

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

Overview

Model class for lists.

Defined Under Namespace

Classes: Group

Constant Summary collapse

TERMTYPES =

Mapping between list termtypes 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

#descriptionString (readonly)

Returns:

  • (String)


66
# File 'lib/renshuu/models/list.rb', line 66

attribute :description, Types::String

#list_idString (readonly) Also known as: id

Returns:

  • (String)


53
# File 'lib/renshuu/models/list.rb', line 53

attribute :list_id, Types::String

#num_termsInteger (readonly)

Returns:

  • (Integer)


70
# File 'lib/renshuu/models/list.rb', line 70

attribute :num_terms, Types::Integer

#privacyString (readonly)

Returns:

  • (String)


74
# File 'lib/renshuu/models/list.rb', line 74

attribute :privacy, Types::String

#termtypeString (readonly)

Returns:

  • (String)


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

attribute :termtype, Types::String

#titleString (readonly)

Returns:

  • (String)


62
# File 'lib/renshuu/models/list.rb', line 62

attribute :title, Types::String

Class Method Details

.listArray<Group>

Retrives list groups from the API.

Returns:



22
23
24
25
26
27
28
29
# File 'lib/renshuu/models/list.rb', line 22

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

  body.fetch(:termtype_groups).flat_map do |termtype_group|
    termtype = termtype_group.fetch(:termtype)
    termtype_group.fetch(:groups).map { Group.new(termtype:, **_1) }
  end
end

Instance Method Details

#add(item) ⇒ Void

Adds the given item to the list.

Parameters:

Returns:

  • (Void)

Raises:

  • (ArgumentError)

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

See Also:



99
100
101
102
103
104
105
# File 'lib/renshuu/models/list.rb', line 99

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

  item.add_to_list(self)
end

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

Retrieves terms contained in the list.

Parameters:

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

Returns:



82
83
84
85
86
87
# File 'lib/renshuu/models/list.rb', line 82

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

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

#remove(item) ⇒ Void

Removes the given item from the list.

Parameters:

Returns:

  • (Void)

Raises:

  • (ArgumentError)

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

See Also:



117
118
119
120
121
122
123
# File 'lib/renshuu/models/list.rb', line 117

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

  item.remove_from_list(self)
end

#term_classClass

Model class to use for this list’s terms.

Returns:

  • (Class)

Raises:

  • (KeyError)

See Also:

  • TERMTYPES


132
133
134
135
136
# File 'lib/renshuu/models/list.rb', line 132

def term_class
  @term_class ||= TERMTYPES.fetch(termtype) do |key|
    raise KeyError, "Unkown list type `#{key}`"
  end
end