Class: Plum::Theme
- Inherits:
-
Object
- Object
- Plum::Theme
- Defined in:
- app/services/plum/theme.rb
Constant Summary collapse
- SUPPORTED_SETTING_TYPES =
%w[text textarea color boolean select].freeze
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
-
#manifest ⇒ Object
readonly
Returns the value of attribute manifest.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #asset_path(asset_name) ⇒ Object
- #asset_root ⇒ Object
- #author ⇒ Object
- #block_definition(handle) ⇒ Object
- #block_template(handle, variant: :web) ⇒ Object
- #block_template_path(handle, variant: :web) ⇒ Object
- #blocks ⇒ Object
- #category ⇒ Object
- #description ⇒ Object
-
#initialize(root:, manifest: {}) ⇒ Theme
constructor
A new instance of Theme.
- #layout_path(layout_name = "base") ⇒ Object
- #name ⇒ Object
- #parent_handle ⇒ Object
- #screenshot_path ⇒ Object
- #settings_fields ⇒ Object
- #template_path(template_name) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(root:, manifest: {}) ⇒ Theme
Returns a new instance of Theme.
7 8 9 10 11 |
# File 'app/services/plum/theme.rb', line 7 def initialize(root:, manifest: {}) @root = Pathname(root) @manifest = manifest.to_h.deep_stringify_keys @handle = @manifest["handle"].presence || @root.basename.to_s end |
Instance Attribute Details
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
5 6 7 |
# File 'app/services/plum/theme.rb', line 5 def handle @handle end |
#manifest ⇒ Object (readonly)
Returns the value of attribute manifest.
5 6 7 |
# File 'app/services/plum/theme.rb', line 5 def manifest @manifest end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
5 6 7 |
# File 'app/services/plum/theme.rb', line 5 def root @root end |
Instance Method Details
#asset_path(asset_name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/services/plum/theme.rb', line 83 def asset_path(asset_name) normalized_name = ThemeAssetPath.normalize(asset_name) candidate = asset_root.join(normalized_name). = asset_root. return unless candidate.to_s.start_with?("#{}/") candidate rescue ThemeAssetPath::UnsafePathError nil end |
#asset_root ⇒ Object
79 80 81 |
# File 'app/services/plum/theme.rb', line 79 def asset_root root.join("assets") end |
#author ⇒ Object
21 22 23 |
# File 'app/services/plum/theme.rb', line 21 def manifest["author"].presence end |
#block_definition(handle) ⇒ Object
56 57 58 59 60 61 |
# File 'app/services/plum/theme.rb', line 56 def block_definition(handle) handle = handle.to_s return if handle.blank? blocks.find { |block| block["handle"] == handle } end |
#block_template(handle, variant: :web) ⇒ Object
63 64 65 66 67 68 |
# File 'app/services/plum/theme.rb', line 63 def block_template(handle, variant: :web) path = block_template_path(handle, variant: variant) return unless path&.file? path.read end |
#block_template_path(handle, variant: :web) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'app/services/plum/theme.rb', line 70 def block_template_path(handle, variant: :web) return unless variant.to_sym == :web normalized_handle = handle.to_s return if normalized_handle.blank? || !normalized_handle.match?(/\A[a-z][a-z0-9_]*\z/) root.join("blocks", "#{normalized_handle}.liquid") end |
#blocks ⇒ Object
50 51 52 53 54 |
# File 'app/services/plum/theme.rb', line 50 def blocks Array(manifest["blocks"]).filter_map do |block| normalize_block_definition(block) end end |
#category ⇒ Object
29 30 31 |
# File 'app/services/plum/theme.rb', line 29 def category manifest["category"].presence end |
#description ⇒ Object
25 26 27 |
# File 'app/services/plum/theme.rb', line 25 def description manifest["description"].presence end |
#layout_path(layout_name = "base") ⇒ Object
99 100 101 |
# File 'app/services/plum/theme.rb', line 99 def layout_path(layout_name = "base") root.join("layouts", "#{layout_name}.liquid") end |
#name ⇒ Object
13 14 15 |
# File 'app/services/plum/theme.rb', line 13 def name manifest["name"].presence || handle.titleize end |
#parent_handle ⇒ Object
40 41 42 |
# File 'app/services/plum/theme.rb', line 40 def parent_handle manifest["extends"].presence || manifest["parent"].presence end |
#screenshot_path ⇒ Object
33 34 35 36 37 38 |
# File 'app/services/plum/theme.rb', line 33 def screenshot_path path = normalize_asset_reference(manifest["screenshot"]) return if path.blank? asset_path(path)&.file? ? path : nil end |
#settings_fields ⇒ Object
44 45 46 47 48 |
# File 'app/services/plum/theme.rb', line 44 def settings_fields Array(manifest.dig("settings", "fields")).filter_map do |field| normalize_setting_field(field) end end |
#template_path(template_name) ⇒ Object
95 96 97 |
# File 'app/services/plum/theme.rb', line 95 def template_path(template_name) root.join("templates", "#{template_name}.liquid") end |
#version ⇒ Object
17 18 19 |
# File 'app/services/plum/theme.rb', line 17 def version manifest["version"].presence end |