Class: Postview::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/postview/settings.rb

Overview

Copyright © 2009 Hallison Batista

Constant Summary collapse

DEFAULTS =

Default values that will be used for valid attributes in settings file.

{
  :site => {
    :title     => "Postview",
    :subtitle  => "Post your articles",
    :author    => "Postview",
    :email     => "[email protected]",
    :domain    => "example.com",
    :directory => "/var/www/example",
    :theme     => "default",
    :token     => "e44257415827b00557ae5505f93e112d6158c4ba3c567aefbbff47288c6bf7cd" # Password: admin
  },
  :directories => {
    :posts   => "posts",
    :archive => "posts/archive",
    :drafts  => "posts/drafts",
    :themes  => "themes"
  },
  :sections => {
    :root    => "/",
    :posts   => "/posts",
    :tags    => "/tags",
    :archive => "/archive",
    :drafts  => "/drafts",
    :search  => "/search",
    :about   => "/about",
    :manager => "/manager"
  }
}
FILE_NAME =
"settings.yml"
FILE_DIR =
"config"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Settings

Initialize the settings using attributes passed by arguments. Only attributes valid are read. See DEFAULTS.



49
50
51
52
53
# File 'lib/postview/settings.rb', line 49

def initialize(attributes = {})
  attributes.symbolize_keys.merge(DEFAULTS) do |key, value, default|
    value || default
  end.instance_variables_set_to(self)
end

Instance Attribute Details

#directoriesObject (readonly)

Directories that be used for load files.



42
43
44
# File 'lib/postview/settings.rb', line 42

def directories
  @directories
end

#sectionsObject (readonly)

Section names that be used in mapping routes into application.



45
46
47
# File 'lib/postview/settings.rb', line 45

def sections
  @sections
end

#siteObject (readonly)

Site attributes



39
40
41
# File 'lib/postview/settings.rb', line 39

def site
  @site
end

Class Method Details

.build_default_fileObject

Build settings file using DEFAULTS.



101
102
103
104
105
# File 'lib/postview/settings.rb', line 101

def self.build_default_file
  file.open "w+" do |file|
    file << DEFAULTS.to_yaml
  end unless file.exist?
end

.fileObject

Returns settings file.



108
109
110
# File 'lib/postview/settings.rb', line 108

def self.file
  Postview::path.join(FILE_DIR, FILE_NAME)
end

.loadObject

Load a default settings values from file placed in config/settings.yml.



80
81
82
# File 'lib/postview/settings.rb', line 80

def self.load
  load_file(file)
end

.load_file(file_name) ⇒ Object

Load a specific settings file. Returns default values for attributes not found.



86
87
88
89
90
91
92
# File 'lib/postview/settings.rb', line 86

def self.load_file(file_name)
  begin
    new(YAML.load_file(file_name) || DEFAULTS)
  rescue Errno::ENOENT
    new(DEFAULTS)
  end
end

.load_file_from(path) ⇒ Object

Load file from a path.



95
96
97
98
# File 'lib/postview/settings.rb', line 95

def self.load_file_from(path)
  file = Pathname.new(path).join(FILE_DIR, FILE_NAME)
  new(YAML.load_file(file))
end

Instance Method Details

#build_all_finders_for(site) ⇒ Object

Build finders for site. The finders load all post files placed in directories setted in directories values.



67
68
69
70
71
72
# File 'lib/postview/settings.rb', line 67

def build_all_finders_for(site)
  site.find = build_finder_for(:posts)
  site.find_in_archive = build_finder_for(:archive)
  site.find_in_drafts = build_finder_for(:drafts)
  site
end

#build_finder_for(directory) ⇒ Object

Build a specific finder for directory.



75
76
77
# File 'lib/postview/settings.rb', line 75

def build_finder_for(directory)
  Postage::Finder.new(path_to(directory))
end

#build_pageObject

Build a simple structure for pages.



61
62
63
# File 'lib/postview/settings.rb', line 61

def build_page
  OpenStruct.new(:title => "", :keywords => [])
end

#build_siteObject

Build site using attributes found in site key.



56
57
58
# File 'lib/postview/settings.rb', line 56

def build_site
  build_all_finders_for(Site.new(site))
end

#file_names_from(directory, pattern = "**.*") ⇒ Object

Check directory and returns file that matches with a pattern.



113
114
115
# File 'lib/postview/settings.rb', line 113

def file_names_from(directory, pattern = "**.*")
  path_to(directory, pattern)
end

#path_to(name, *paths) ⇒ Object

Returns a valid directory loaded from settings file.



118
119
120
121
# File 'lib/postview/settings.rb', line 118

def path_to(name, *paths)
  return Pathname.new(directories[name], *paths) if directories[name].match(%r{^/.*})
  Postview::path.join(directories[name], *paths)
end

#to_hashObject

Parse all attributes to hash.



124
125
126
127
128
129
# File 'lib/postview/settings.rb', line 124

def to_hash
  DEFAULTS.keys.inject({}) do |hash, method|
    hash[method] = send(method)
    hash
  end
end