Class: Postview::Settings
- Inherits:
-
Object
- Object
- Postview::Settings
- 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
-
#directories ⇒ Object
readonly
Directories that be used for load files.
-
#sections ⇒ Object
readonly
Section names that be used in mapping routes into application.
-
#site ⇒ Object
readonly
Site attributes.
Class Method Summary collapse
-
.build_default_file ⇒ Object
Build settings file using DEFAULTS.
-
.file ⇒ Object
Returns settings file.
-
.load ⇒ Object
Load a default settings values from file placed in
config/settings.yml
. -
.load_file(file_name) ⇒ Object
Load a specific settings file.
-
.load_file_from(path) ⇒ Object
Load file from a path.
Instance Method Summary collapse
-
#build_all_finders_for(site) ⇒ Object
Build finders for site.
-
#build_finder_for(directory) ⇒ Object
Build a specific finder for directory.
-
#build_page ⇒ Object
Build a simple structure for pages.
-
#build_site ⇒ Object
Build site using attributes found in
site
key. -
#file_names_from(directory, pattern = "**.*") ⇒ Object
Check directory and returns file that matches with a pattern.
-
#initialize(attributes = {}) ⇒ Settings
constructor
Initialize the settings using attributes passed by arguments.
-
#path_to(name, *paths) ⇒ Object
Returns a valid directory loaded from settings file.
-
#to_hash ⇒ Object
Parse all attributes to hash.
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
#directories ⇒ Object (readonly)
Directories that be used for load files.
42 43 44 |
# File 'lib/postview/settings.rb', line 42 def directories @directories end |
#sections ⇒ Object (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 |
#site ⇒ Object (readonly)
Site attributes
39 40 41 |
# File 'lib/postview/settings.rb', line 39 def site @site end |
Class Method Details
.build_default_file ⇒ Object
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 |
.file ⇒ Object
Returns settings file.
108 109 110 |
# File 'lib/postview/settings.rb', line 108 def self.file Postview::path.join(FILE_DIR, FILE_NAME) end |
.load ⇒ Object
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 |
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_page ⇒ Object
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_site ⇒ Object
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_hash ⇒ Object
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 |