Class: RailsSiteEngine::SiteProfiles

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_site_engine/site_profiles.rb

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("../../config/site_profiles.yml", __dir__)

Instance Method Summary collapse

Constructor Details

#initialize(path: DEFAULT_PATH) ⇒ SiteProfiles

Returns a new instance of SiteProfiles.



8
9
10
11
# File 'lib/rails_site_engine/site_profiles.rb', line 8

def initialize(path: DEFAULT_PATH)
  @path = Pathname.new(path)
  @profiles = load_profiles
end

Instance Method Details

#concrete_profile_keys ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rails_site_engine/site_profiles.rb', line 56

def concrete_profile_keys
  profile_keys.each_with_object([]) do |key, memo|
    entry = profile(key)
    next unless entry.is_a?(Hash)

    specialties = normalize_hash(entry["specialties"])
    if specialties.empty?
      template = string_value(entry["template"])
      memo << template unless template.empty?
      next
    end

    specialties.each_value do |specialty|
      next unless specialty.is_a?(Hash)

      template = string_value(specialty["template"])
      memo << template unless template.empty?
    end
  end.uniq
end

#default_specialty(profile_key) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/rails_site_engine/site_profiles.rb', line 41

def default_specialty(profile_key)
  profile_key = normalize_key(profile_key)
  keys = specialty_keys(profile_key)
  return nil if keys.empty?

  configured = string_value(profile(profile_key)&.dig("default_specialty"))
  return configured if keys.include?(configured)

  keys.first
end

#fallback_concrete_profile ⇒ Object



81
82
83
# File 'lib/rails_site_engine/site_profiles.rb', line 81

def fallback_concrete_profile
  concrete_profile_keys.first
end

#profile(profile_key) ⇒ Object



17
18
19
# File 'lib/rails_site_engine/site_profiles.rb', line 17

def profile(profile_key)
  @profiles[normalize_key(profile_key)]
end

#profile_keys ⇒ Object



13
14
15
# File 'lib/rails_site_engine/site_profiles.rb', line 13

def profile_keys
  @profiles.keys
end

#profile_label(profile_key) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rails_site_engine/site_profiles.rb', line 21

def profile_label(profile_key)
  entry = profile(profile_key)
  return normalize_key(profile_key) if entry.nil?

  label = string_value(entry["label"])
  label.empty? ? normalize_key(profile_key) : label
end

#requires_specialty?(profile_key) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rails_site_engine/site_profiles.rb', line 52

def requires_specialty?(profile_key)
  specialty_keys(profile_key).any?
end

#resolve_concrete_profile(site_profile:, trade_specialty: nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rails_site_engine/site_profiles.rb', line 95

def resolve_concrete_profile(site_profile:, trade_specialty: nil)
  profile_key = normalize_key(site_profile)
  entry = profile(profile_key)
  return nil unless entry.is_a?(Hash)

  specialties = normalize_hash(entry["specialties"])
  if specialties.any?
    specialty_key = normalize_key(trade_specialty)
    specialty_key = default_specialty(profile_key) if specialty_key.empty?
    specialty = specialties[specialty_key]
    return nil unless specialty.is_a?(Hash)

    template = string_value(specialty["template"])
    return nil if template.empty?

    return template
  end

  template = string_value(entry["template"])
  return nil if template.empty?

  template
end

#specialty_keys(profile_key) ⇒ Object



29
30
31
# File 'lib/rails_site_engine/site_profiles.rb', line 29

def specialty_keys(profile_key)
  specialties_for(profile_key).keys
end

#specialty_label(profile_key, specialty_key) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rails_site_engine/site_profiles.rb', line 33

def specialty_label(profile_key, specialty_key)
  entry = specialties_for(profile_key)[normalize_key(specialty_key)]
  return normalize_key(specialty_key) if entry.nil?

  label = string_value(entry["label"])
  label.empty? ? normalize_key(specialty_key) : label
end

#template_root_for(concrete_profile_key) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/rails_site_engine/site_profiles.rb', line 85

def template_root_for(concrete_profile_key)
  key = normalize_key(concrete_profile_key)
  return nil unless valid_concrete_profile?(key)

  Pathname.new(DEFAULT_PATH)
    .dirname
    .join("../lib/generators/rails_site_engine/install/templates/site_profiles", key)
    .expand_path
end

#valid_concrete_profile?(key) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rails_site_engine/site_profiles.rb', line 77

def valid_concrete_profile?(key)
  concrete_profile_keys.include?(normalize_key(key))
end