Class: Pekky::Content
- Inherits:
-
Object
- Object
- Pekky::Content
- Defined in:
- lib/pekky/content.rb
Overview
The content class is used to wrap around the YAML files loaded from disk. It provides coercions and some nice syntax for accessing the fields.
Defined Under Namespace
Classes: ContentMissingError, FieldMissingError, Fields
Class Method Summary collapse
-
.[](path) ⇒ Object
Returns a content instance populated with data from the file at the specified path.
-
.flush! ⇒ Object
Clears out any cached content instances.
- .get(path) ⇒ Object
Instance Method Summary collapse
-
#[](name) ⇒ Object
A simple accessor for grabbing an attribute without having to use #method_missing fall-back.
-
#fields ⇒ Object
Returns the fields instance.
-
#initialize(path) ⇒ Content
constructor
On initialization, the content class creates an instance of the Fields class.
-
#method_missing(name) ⇒ Object
Method missing is being used to allow us to access fields on the content instance as if they were declared as readers.
- #reload ⇒ Object
Constructor Details
#initialize(path) ⇒ Content
On initialization, the content class creates an instance of the Fields class. This instance encapsulates most of the logic for accessing values.
53 54 55 56 |
# File 'lib/pekky/content.rb', line 53 def initialize(path) @path = path load_file end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
Method missing is being used to allow us to access fields on the content instance as if they were declared as readers. Yes, it’s slower than using #class_eval, but it’s simple and the speed doesn’t matter frankly.
79 80 81 82 83 84 85 |
# File 'lib/pekky/content.rb', line 79 def method_missing(name) if match = name.to_s.match(/^(\S+)\?$/) @fields.has?(name) else @fields.has?(name) ? @fields[name] : super end end |
Class Method Details
.[](path) ⇒ Object
Returns a content instance populated with data from the file at the specified path. Errors if the file missing.
25 26 27 |
# File 'lib/pekky/content.rb', line 25 def self.[](path) @cache[path] ||= load_file(path) end |
.flush! ⇒ Object
Clears out any cached content instances.
47 48 49 |
# File 'lib/pekky/content.rb', line 47 def self.flush! @cache.clear end |
.get(path) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pekky/content.rb', line 29 def self.get(path) @cache[path] ||= begin full_path = File.join(Config[:content_dir], path) if File.directory?(full_path) contents = [] Dir.foreach(full_path) do |filename| unless File.directory?(filename) contents << new("#{path}/#{filename}") end end contents else new(path) end end end |
Instance Method Details
#[](name) ⇒ Object
A simple accessor for grabbing an attribute without having to use #method_missing fall-back.
60 61 62 |
# File 'lib/pekky/content.rb', line 60 def [](name) @fields[name] end |
#fields ⇒ Object
Returns the fields instance. Allows users to do things like:
content.fields.each {|name, value| puts value}
68 69 70 |
# File 'lib/pekky/content.rb', line 68 def fields @fields end |
#reload ⇒ Object
72 73 74 |
# File 'lib/pekky/content.rb', line 72 def reload load_file end |