Class: QiitaMatome::Sort::Sorter

Inherits:
Object
  • Object
show all
Defined in:
lib/sort/sorter.rb

Overview

QiitaMatome::Sort::Sorter

Constant Summary collapse

SORT_PATTERNS =

rubocop:disable LineLength

{
  Consts::CREATED_AT_ASC => { send_method: :sort_asc, sort_key: :created_at },
  Consts::CREATED_AT_DESC => { send_method: :sort_desc, sort_key: :created_at },
  Consts::UPDATED_AT_ASC => { send_method: :sort_asc, sort_key: :updated_at },
  Consts::UPDATED_AT_DESC => { send_method: :sort_desc, sort_key: :updated_at },
  Consts::TITLE_ASC => { send_method: :sort_asc, sort_key: :title },
  Consts::TITLE_DESC => { send_method: :sort_desc, sort_key: :title },
  Consts::STOCK_COUNT_ASC => { send_method: :sort_asc, sort_key: :stock_count },
  Consts::STOCK_COUNT_DESC => { send_method: :sort_desc, sort_key: :stock_count }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(articles, sort_type = Consts::UPDATED_AT_DESC) ⇒ Sorter

rubocop:enable LineLength



27
28
29
30
31
32
33
# File 'lib/sort/sorter.rb', line 27

def initialize(articles, sort_type = Consts::UPDATED_AT_DESC)
  Validators::ArticlesValidator.validate(articles)
  Validators::ArticleValidator.validate(articles)
  Validators::SortTypeValidator.validate(sort_type)
  @articles = articles
  @sort_type = sort_type
end

Instance Attribute Details

#articlesObject (readonly)

Returns the value of attribute articles.



12
13
14
# File 'lib/sort/sorter.rb', line 12

def articles
  @articles
end

#sort_typeObject (readonly)

Returns the value of attribute sort_type.



12
13
14
# File 'lib/sort/sorter.rb', line 12

def sort_type
  @sort_type
end

Instance Method Details

#sortObject



35
36
37
38
# File 'lib/sort/sorter.rb', line 35

def sort
  sort_pattern = SORT_PATTERNS[@sort_type]
  send(sort_pattern[:send_method], sort_pattern[:sort_key])
end