Class: Dugway::Theme
- Inherits:
-
Object
- Object
- Dugway::Theme
- Defined in:
- lib/dugway/theme.rb
Constant Summary collapse
- REQUIRED_FILES =
%w( cart.html contact.html home.html layout.html maintenance.html product.html products.html screenshot.jpg settings.json theme.css theme.js )
- THEME_COLOR_ATTRIBUTE_MAPPINGS =
YAML.load_file( File.join(__dir__, 'config', 'theme_color_attribute_mappings.yml') ).freeze
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #build_file(name) ⇒ Object
- #customization ⇒ Object
- #file_content(name) ⇒ Object
- #files ⇒ Object
- #font_files ⇒ Object
- #fonts ⇒ Object
- #image_files ⇒ Object
- #image_sets ⇒ Object
- #images ⇒ Object
-
#initialize(overridden_customization = {}) ⇒ Theme
constructor
A new instance of Theme.
- #layout ⇒ Object
- #name ⇒ Object
- #settings ⇒ Object
- #valid?(validate_colors: true, validate_layout_attributes: true) ⇒ Boolean
- #validate_required_color_settings ⇒ Object
-
#validate_required_layout_attributes ⇒ Object
Validate that the Layout file has expected attributes for: - data-bc-page-type on the body tag - one data-bc-hook=“header” and one data-bc-hook=“footer” somewhere.
- #version ⇒ Object
Constructor Details
#initialize(overridden_customization = {}) ⇒ Theme
Returns a new instance of Theme.
18 19 20 |
# File 'lib/dugway/theme.rb', line 18 def initialize(overridden_customization={}) @overridden_customization = overridden_customization.stringify_keys end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/dugway/theme.rb', line 16 def errors @errors end |
Instance Method Details
#build_file(name) ⇒ Object
75 76 77 78 |
# File 'lib/dugway/theme.rb', line 75 def build_file(name) @building = true file_content(name) end |
#customization ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/dugway/theme.rb', line 42 def customization Hash.new.tap do |customization| %w( fonts colors options images image_sets ).each do |type| customization.update(customization_for_type(type)) end customization.update(@overridden_customization) end end |
#file_content(name) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dugway/theme.rb', line 60 def file_content(name) case name when 'theme.js' if @building Terser.new.compile(sprockets[name].to_s) else sprockets[name].to_s end when 'theme.css' sprockets[name].to_s else read_source_file(name) end end |
#files ⇒ Object
80 81 82 |
# File 'lib/dugway/theme.rb', line 80 def files REQUIRED_FILES + image_files + font_files end |
#font_files ⇒ Object
90 91 92 93 94 |
# File 'lib/dugway/theme.rb', line 90 def font_files Dir.glob(File.join(source_dir, 'fonts', '**', '*.{eot,ttf,otf,woff,svg}')).map do |i| i.gsub(source_dir, '')[1..-1] end end |
#fonts ⇒ Object
30 31 32 |
# File 'lib/dugway/theme.rb', line 30 def fonts customization_for_type('fonts') end |
#image_files ⇒ Object
84 85 86 87 88 |
# File 'lib/dugway/theme.rb', line 84 def image_files Dir.glob(File.join(source_dir, 'images', '**', '*.{png,jpg,jpeg,gif,ico,svg}')).map do |i| i.gsub(source_dir, '')[1..-1] end end |
#image_sets ⇒ Object
38 39 40 |
# File 'lib/dugway/theme.rb', line 38 def image_sets customization_for_type('image_sets') end |
#images ⇒ Object
34 35 36 |
# File 'lib/dugway/theme.rb', line 34 def images customization_for_type('images') end |
#layout ⇒ Object
22 23 24 |
# File 'lib/dugway/theme.rb', line 22 def layout read_source_file('layout.html') end |
#name ⇒ Object
52 53 54 |
# File 'lib/dugway/theme.rb', line 52 def name settings['name'] end |
#settings ⇒ Object
26 27 28 |
# File 'lib/dugway/theme.rb', line 26 def settings JSON.parse(read_source_file('settings.json')) end |
#valid?(validate_colors: true, validate_layout_attributes: true) ⇒ Boolean
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dugway/theme.rb', line 96 def valid?(validate_colors: true, validate_layout_attributes: true) @errors = [] REQUIRED_FILES.each do |file| @errors << "Missing source/#{ file }" if read_source_file(file).nil? end @errors << 'Missing theme name in source/settings.json' if name.blank? @errors << 'Invalid theme version in source/settings.json (ex: 1.0.3)' unless !!(version =~ /\d+\.\d+\.\d+/) if settings['preset_styles'] validate_preview if settings['preset_styles']['presets'] settings['preset_styles']['presets'].each do |preset| validate_preset_styles(preset) validate_style_references(preset) end else @errors << "Missing presets" end end validate_required_color_settings if validate_colors validate_required_layout_attributes if validate_layout_attributes @errors.empty? end |
#validate_required_color_settings ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/dugway/theme.rb', line 124 def validate_required_color_settings required_colors_attribute_names = THEME_COLOR_ATTRIBUTE_MAPPINGS['required_color_attributes'] theme_colors = settings['colors'].map { |c| c['variable'] } mappings = THEME_COLOR_ATTRIBUTE_MAPPINGS[name] || {} missing_colors = required_colors_attribute_names.reject do |color| theme_colors.include?(color) || (mappings.key?(color) && (mappings[color].nil? || theme_colors.include?(mappings[color]))) end unless missing_colors.empty? @errors << "Missing required color settings: #{missing_colors.join(', ')}" end end |
#validate_required_layout_attributes ⇒ Object
Validate that the Layout file has expected attributes for:
-
data-bc-page-type on the body tag
-
one data-bc-hook=“header” and one data-bc-hook=“footer” somewhere
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/dugway/theme.rb', line 143 def validate_required_layout_attributes layout_content = read_source_file('layout.html') unless layout_content =~ /<body.*?\bdata-bc-page-type\b/ @errors << "layout.html missing `data-bc-page-type` attribute on body tag" end header_hooks = layout_content.scan(/data-bc-hook=(?:"|')header(?:"|')/).size = layout_content.scan(/data-bc-hook=(?:"|')footer(?:"|')/).size @errors << "layout.html must have exactly one `data-bc-hook=\"header\"`" if header_hooks != 1 @errors << "layout.html must have exactly one `data-bc-hook=\"footer\"`" if != 1 end |
#version ⇒ Object
56 57 58 |
# File 'lib/dugway/theme.rb', line 56 def version settings['version'] end |