Class: Ottogen::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ottogen/config.rb

Defined Under Namespace

Classes: Data, Error

Constant Summary collapse

DEFAULT_PATH =
'config.yml'
DATA_DIR =
'_data'
DATA_EXTENSIONS =
%w[.yml .yaml .json].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, data_files = {}, posts = [], collections = {}) ⇒ Config

Returns a new instance of Config.



61
62
63
64
65
66
# File 'lib/ottogen/config.rb', line 61

def initialize(values, data_files = {}, posts = [], collections = {})
  @values = values
  @data = Data.new(data_files)
  @posts = posts
  @collections = collections
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ottogen/config.rb', line 98

def method_missing(name, *args)
  key = name.to_s
  return @collections[key].items if @collections.key?(key)
  return @values[key] if @values.key?(key)

  super
end

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections.



68
69
70
# File 'lib/ottogen/config.rb', line 68

def collections
  @collections
end

#dataObject (readonly)

Returns the value of attribute data.



68
69
70
# File 'lib/ottogen/config.rb', line 68

def data
  @data
end

#postsObject (readonly)

Returns the value of attribute posts.



68
69
70
# File 'lib/ottogen/config.rb', line 68

def posts
  @posts
end

Class Method Details

.load(path = DEFAULT_PATH, drafts: false) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/ottogen/config.rb', line 17

def self.load(path = DEFAULT_PATH, drafts: false)
  raise Error, "config.yml not found at #{path}" unless File.exist?(path)

  values = YAML.safe_load_file(path) || {}
  new(values, load_data_files, load_posts(drafts: drafts), load_collections(values['collections']))
rescue Psych::SyntaxError => e
  raise Error, "malformed YAML in #{path}: #{e.message}"
end

Instance Method Details

#[](key) ⇒ Object



85
86
87
# File 'lib/ottogen/config.rb', line 85

def [](key)
  @values[key.to_s]
end

#asciidoctor_attributesObject



89
90
91
# File 'lib/ottogen/config.rb', line 89

def asciidoctor_attributes
  @values.transform_keys { |key| "site_#{key}" }
end

#categoriesObject



74
75
76
# File 'lib/ottogen/config.rb', line 74

def categories
  group_posts_by(&:categories)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/ottogen/config.rb', line 93

def respond_to_missing?(name, include_private = false)
  key = name.to_s
  @values.key?(key) || @collections.key?(key) || super
end

#tagsObject



70
71
72
# File 'lib/ottogen/config.rb', line 70

def tags
  group_posts_by(&:tags)
end