Class: Plum::Theme

Inherits:
Object
  • Object
show all
Defined in:
app/services/plum/theme.rb

Constant Summary collapse

SUPPORTED_SETTING_TYPES =
%w[text textarea color boolean select].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#handleObject (readonly)

Returns the value of attribute handle.



5
6
7
# File 'app/services/plum/theme.rb', line 5

def handle
  @handle
end

#manifestObject (readonly)

Returns the value of attribute manifest.



5
6
7
# File 'app/services/plum/theme.rb', line 5

def manifest
  @manifest
end

#rootObject (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).expand_path
  expanded_asset_root = asset_root.expand_path

  return unless candidate.to_s.start_with?("#{expanded_asset_root}/")

  candidate
rescue ThemeAssetPath::UnsafePathError
  nil
end

#asset_rootObject



79
80
81
# File 'app/services/plum/theme.rb', line 79

def asset_root
  root.join("assets")
end

#authorObject



21
22
23
# File 'app/services/plum/theme.rb', line 21

def author
  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

#blocksObject



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

#categoryObject



29
30
31
# File 'app/services/plum/theme.rb', line 29

def category
  manifest["category"].presence
end

#descriptionObject



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

#nameObject



13
14
15
# File 'app/services/plum/theme.rb', line 13

def name
  manifest["name"].presence || handle.titleize
end

#parent_handleObject



40
41
42
# File 'app/services/plum/theme.rb', line 40

def parent_handle
  manifest["extends"].presence || manifest["parent"].presence
end

#screenshot_pathObject



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_fieldsObject



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

#versionObject



17
18
19
# File 'app/services/plum/theme.rb', line 17

def version
  manifest["version"].presence
end