Class: Pekky::Content::Fields
- Inherits:
-
Object
- Object
- Pekky::Content::Fields
- Includes:
- Enumerable
- Defined in:
- lib/pekky/content.rb
Overview
The fields class provides an interface for iterating over a record’s fields, checking to see if a field exists and other sundry tasks.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Allows the proxy to be used by a hash, but under the hood it will actually coerce values and push them into the cache.
-
#each ⇒ Object
The each method, which is required by the Enumerable module.
-
#has?(name) ⇒ Boolean
Checks to see if the specified field exists, returning true or false.
-
#initialize(fields) ⇒ Fields
constructor
The fields proxy is initialized with the raw values pulled from the content YAML files.
Constructor Details
#initialize(fields) ⇒ Fields
The fields proxy is initialized with the raw values pulled from the content YAML files.
111 112 113 |
# File 'lib/pekky/content.rb', line 111 def initialize(fields) @fields = fields end |
Instance Method Details
#[](name) ⇒ Object
Allows the proxy to be used by a hash, but under the hood it will actually coerce values and push them into the cache. This is the entirety of the lazy-coercion. Very simple.
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/pekky/content.rb', line 118 def [](name) name_ = name.to_sym if @fields.has_key?(name) @fields[name] elsif @fields.has_key?(name_) @fields[name_] else raise FieldMissingError.new(name) end end |
#each ⇒ Object
The each method, which is required by the Enumerable module. We iterate over the raw values, since they may not be in the cache. We then access them via #[] to ensure that they are coerced and cached.
137 138 139 |
# File 'lib/pekky/content.rb', line 137 def each @fields.each {|k, v| yield k, v } end |
#has?(name) ⇒ Boolean
Checks to see if the specified field exists, returning true or false.
130 131 132 |
# File 'lib/pekky/content.rb', line 130 def has?(name) !!@fields.has_key?(name) end |