Class: ThemeCheck::PaginationSize
- Inherits:
-
LiquidCheck
- Object
- Check
- LiquidCheck
- ThemeCheck::PaginationSize
- Defined in:
- lib/theme_check/checks/pagination_size.rb
Constant Summary
Constants inherited from Check
Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#min_size ⇒ Object
readonly
Returns the value of attribute min_size.
Attributes inherited from Check
#ignored_patterns, #offenses, #options, #theme
Instance Method Summary collapse
- #after_document(_node) ⇒ Object
-
#get_value(size) ⇒ Object
We’ll work with either an explicit value, or the default value of the section setting.
-
#initialize(min_size: 1, max_size: 50) ⇒ PaginationSize
constructor
A new instance of PaginationSize.
- #on_document(_node) ⇒ Object
- #on_paginate(node) ⇒ Object
- #on_schema(node) ⇒ Object
-
#size_is_a_section_setting?(size) ⇒ Boolean
Section settings look like: #<Liquid::VariableLookup:0x00007fd699c50c48 @name=“section”, @lookups=[“settings”, “products_per_page”], @command_flags=0>.
Methods included from ChecksTracking
Methods included from ParsingHelpers
Methods inherited from Check
#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_theme?
Methods included from JsonHelpers
#format_json_parse_error, #pretty_json
Constructor Details
#initialize(min_size: 1, max_size: 50) ⇒ PaginationSize
Returns a new instance of PaginationSize.
11 12 13 14 |
# File 'lib/theme_check/checks/pagination_size.rb', line 11 def initialize(min_size: 1, max_size: 50) @min_size = min_size @max_size = max_size end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
9 10 11 |
# File 'lib/theme_check/checks/pagination_size.rb', line 9 def max_size @max_size end |
#min_size ⇒ Object (readonly)
Returns the value of attribute min_size.
8 9 10 |
# File 'lib/theme_check/checks/pagination_size.rb', line 8 def min_size @min_size end |
Instance Method Details
#after_document(_node) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/theme_check/checks/pagination_size.rb', line 54 def after_document(_node) @paginations.each_pair do |size, nodes| # Validate presence of default section setting. if size_is_a_section_setting?(size) && !get_setting_default_value(size) nodes.each { |node| add_offense("Default pagination size should be defined in the section settings", node: node) } end # Validate if size is within range. next unless (numerical_size = get_value(size)) if numerical_size > @max_size || numerical_size < @min_size || !numerical_size.is_a?(Integer) nodes.each { |node| add_offense("Pagination size must be a positive integer between #{@min_size} and #{@max_size}", node: node) } end end end |
#get_value(size) ⇒ Object
We’ll work with either an explicit value, or the default value of the section setting.
49 50 51 52 |
# File 'lib/theme_check/checks/pagination_size.rb', line 49 def get_value(size) return size if size.is_a?(Numeric) return get_setting_default_value(size) if size_is_a_section_setting?(size) end |
#on_document(_node) ⇒ Object
16 17 18 19 |
# File 'lib/theme_check/checks/pagination_size.rb', line 16 def on_document(_node) @paginations = {} @schema_settings = {} end |
#on_paginate(node) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/theme_check/checks/pagination_size.rb', line 21 def on_paginate(node) size = node.value.page_size unless @paginations.key?(size) @paginations[size] = [] end @paginations[size].push(node) end |
#on_schema(node) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/theme_check/checks/pagination_size.rb', line 29 def on_schema(node) schema = node.inner_json return if schema.nil? if (settings = schema["settings"]) @schema_settings = settings end end |
#size_is_a_section_setting?(size) ⇒ Boolean
Section settings look like:
#<Liquid::VariableLookup:0x00007fd699c50c48 @name="section", @lookups=["settings", "products_per_page"], @command_flags=0>
41 42 43 44 45 |
# File 'lib/theme_check/checks/pagination_size.rb', line 41 def size_is_a_section_setting?(size) size.is_a?(Liquid::VariableLookup) && size.name == 'section' && size.lookups.first == 'settings' end |