Class: Bunko::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/bunko/configuration.rb

Defined Under Namespace

Classes: CollectionCustomizer, PostTypeCustomizer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



46
47
48
49
50
51
52
53
54
# File 'lib/bunko/configuration.rb', line 46

def initialize
  @reading_speed = 250 # words per minute
  @excerpt_length = 160 # characters
  @auto_update_word_count = true # automatically update word_count when content changes
  @valid_statuses = %w[draft published scheduled]
  @post_types = [] # Must be configured in initializer
  @collections = [] # Multi-type collections
  @allow_static_pages = true # Enable standalone pages feature by default
end

Instance Attribute Details

#allow_static_pagesObject

Returns the value of attribute allow_static_pages.



44
45
46
# File 'lib/bunko/configuration.rb', line 44

def allow_static_pages
  @allow_static_pages
end

#auto_update_word_countObject

Returns the value of attribute auto_update_word_count.



44
45
46
# File 'lib/bunko/configuration.rb', line 44

def auto_update_word_count
  @auto_update_word_count
end

#collectionsObject

Returns the value of attribute collections.



44
45
46
# File 'lib/bunko/configuration.rb', line 44

def collections
  @collections
end

#excerpt_lengthObject

Returns the value of attribute excerpt_length.



44
45
46
# File 'lib/bunko/configuration.rb', line 44

def excerpt_length
  @excerpt_length
end

#post_typesObject

Returns the value of attribute post_types.



44
45
46
# File 'lib/bunko/configuration.rb', line 44

def post_types
  @post_types
end

#reading_speedObject

Returns the value of attribute reading_speed.



44
45
46
# File 'lib/bunko/configuration.rb', line 44

def reading_speed
  @reading_speed
end

#valid_statusesObject

Returns the value of attribute valid_statuses.



44
45
46
# File 'lib/bunko/configuration.rb', line 44

def valid_statuses
  @valid_statuses
end

Instance Method Details

#collection(name, title: nil, post_types: nil, scope: nil, &block) ⇒ Object



92
93
94
95
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bunko/configuration.rb', line 92

def collection(name, title: nil, post_types: nil, scope: nil, &block)
  # Validate name format (must use underscores, not hyphens)
  name_str = name.to_s

  if name_str.include?("-")
    raise ArgumentError, "Collection name '#{name_str}' cannot contain hyphens. Use underscores instead (e.g., 'long_reads'). URLs will automatically use hyphens (/long-reads/)."
  end

  unless name_str.match?(/\A[a-z0-9_]+\z/)
    raise ArgumentError, "Collection name '#{name_str}' must contain only lowercase letters, numbers, and underscores"
  end

  # Check for conflicts with existing post_types
  if post_type_exists?(name_str)
    raise ArgumentError, "Collection name '#{name_str}' conflicts with existing PostType name"
  end

  # Check for conflicts with existing collections
  if collection_exists?(name_str)
    raise ArgumentError, "Collection '#{name_str}' already exists"
  end

  # Require at least post_types param or block
  unless post_types || block_given?
    raise ArgumentError, "Collection '#{name_str}' requires either post_types parameter or a configuration block"
  end

  # Auto-generate title from name (e.g., "long_reads" → "Long Reads")
  generated_title = title || name_str.titleize

  # Normalize post_types to array of names (using underscores, not hyphens)
  normalized_post_types = post_types ? Array(post_types).map { |pt| pt.to_s.parameterize.tr("-", "_") } : []

  collection = {
    name: name_str,
    title: generated_title,
    post_types: normalized_post_types,
    scope: scope
  }

  # Allow customization via block (block overrides params)
  if block_given?
    customizer = CollectionCustomizer.new(collection)
    block.call(customizer)
  end

  # Validate that post_types was set
  if collection[:post_types].empty?
    raise ArgumentError, "Collection '#{name_str}' must specify at least one post_type"
  end

  @collections << collection
end

#find_collection(name) ⇒ Object



150
151
152
# File 'lib/bunko/configuration.rb', line 150

def find_collection(name)
  @collections.find { |c| c[:name] == name.to_s }
end

#find_post_type(name) ⇒ Object



146
147
148
# File 'lib/bunko/configuration.rb', line 146

def find_post_type(name)
  @post_types.find { |pt| pt[:name] == name.to_s }
end

#post_type(name, title: nil, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bunko/configuration.rb', line 56

def post_type(name, title: nil, &block)
  # Validate name format (must use underscores, not hyphens, for Ruby class naming)
  name_str = name.to_s

  if name_str.include?("-")
    raise ArgumentError, "PostType name '#{name_str}' cannot contain hyphens. Use underscores instead (e.g., 'case_study'). URLs will automatically use hyphens (/case-study/)."
  end

  unless name_str.match?(/\A[a-z0-9_]+\z/)
    raise ArgumentError, "PostType name '#{name_str}' must contain only lowercase letters, numbers, and underscores"
  end

  # Reserved name for static pages feature
  if name_str == "pages"
    raise ArgumentError, "PostType name 'pages' is reserved for the static pages feature. Use config.allow_static_pages to control this feature."
  end

  # Check for conflicts with existing collections
  if collection_exists?(name_str)
    raise ArgumentError, "PostType name '#{name_str}' conflicts with existing collection name"
  end

  # Auto-generate title from name (e.g., "case_study" → "Case Study")
  generated_title = title || name_str.titleize

  post_type = {name: name_str, title: generated_title}

  # Allow customization via block (block overrides params)
  if block_given?
    customizer = PostTypeCustomizer.new(post_type)
    block.call(customizer)
  end

  @post_types << post_type
end