Class: Renshuu::List
- 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.
{ 'kanji' => Kanji, 'vocab' => Word, 'grammar' => Grammar, 'sent' => Sentence }.freeze
Instance Attribute Summary collapse
- #description ⇒ String readonly
- #list_id ⇒ String (also: #id) readonly
- #num_terms ⇒ Integer readonly
- #privacy ⇒ String readonly
- #termtype ⇒ String readonly
- #title ⇒ String readonly
Class Method Summary collapse
-
.list ⇒ Array<Group>
Retrives list groups from the API.
Instance Method Summary collapse
-
#add(item) ⇒ Void
Adds the given item to the list.
-
#contents(page: nil) ⇒ Array<Kanji,Word,Grammar,Sentence>
Retrieves terms contained in the list.
-
#remove(item) ⇒ Void
Removes the given item from the list.
-
#term_class ⇒ Class
Model class to use for this list’s terms.
Instance Attribute Details
#description ⇒ String (readonly)
66 |
# File 'lib/renshuu/models/list.rb', line 66 attribute :description, Types::String |
#list_id ⇒ String (readonly) Also known as: id
53 |
# File 'lib/renshuu/models/list.rb', line 53 attribute :list_id, Types::String |
#num_terms ⇒ Integer (readonly)
70 |
# File 'lib/renshuu/models/list.rb', line 70 attribute :num_terms, Types::Integer |
#privacy ⇒ String (readonly)
74 |
# File 'lib/renshuu/models/list.rb', line 74 attribute :privacy, Types::String |
#termtype ⇒ String (readonly)
58 |
# File 'lib/renshuu/models/list.rb', line 58 attribute :termtype, Types::String |
#title ⇒ String (readonly)
62 |
# File 'lib/renshuu/models/list.rb', line 62 attribute :title, Types::String |
Class Method Details
.list ⇒ Array<Group>
Retrives list groups from the API.
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.
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.
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.
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_class ⇒ Class
Model class to use for this list’s terms.
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 |