Class: Conifer::File
- Inherits:
-
Object
- Object
- Conifer::File
- Defined in:
- lib/conifer/file.rb
Constant Summary collapse
- NotFoundError =
Class.new(StandardError)
- UnsupportedFormatError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#allowed_classes ⇒ Object
readonly
Returns the value of attribute allowed_classes.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #exists? ⇒ Boolean
- #filename ⇒ Object
-
#initialize(name, dir:, prefix: nil, format: :yml, allowed_classes: []) ⇒ File
constructor
A new instance of File.
- #parsed ⇒ Object
- #path ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(name, dir:, prefix: nil, format: :yml, allowed_classes: []) ⇒ File
Returns a new instance of File.
13 14 15 16 17 18 19 |
# File 'lib/conifer/file.rb', line 13 def initialize(name, dir:, prefix: nil, format: :yml, allowed_classes: []) @name = name @dir = dir @prefix = prefix @format = format @allowed_classes = allowed_classes end |
Instance Attribute Details
#allowed_classes ⇒ Object (readonly)
Returns the value of attribute allowed_classes.
11 12 13 |
# File 'lib/conifer/file.rb', line 11 def allowed_classes @allowed_classes end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
11 12 13 |
# File 'lib/conifer/file.rb', line 11 def dir @dir end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
11 12 13 |
# File 'lib/conifer/file.rb', line 11 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/conifer/file.rb', line 11 def name @name end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
11 12 13 |
# File 'lib/conifer/file.rb', line 11 def prefix @prefix end |
Instance Method Details
#[](key) ⇒ Object
21 22 23 24 |
# File 'lib/conifer/file.rb', line 21 def [](key) args = key.split('.').tap { |v| v.prepend(prefix) if prefix } parsed.dig(*args) end |
#exists? ⇒ Boolean
43 44 45 |
# File 'lib/conifer/file.rb', line 43 def exists? !path.nil? end |
#filename ⇒ Object
47 48 49 |
# File 'lib/conifer/file.rb', line 47 def filename "#{::File.basename(name.to_s, ".#{format}")}.#{format}" end |
#parsed ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/conifer/file.rb', line 26 def parsed @parsed ||= case format when :yml, :yaml YAML.safe_load(ERB.new(::File.read(path)).result, allowed_classes) when :json JSON.parse(ERB.new(::File.read(path)).result) else raise UnsupportedFormatError, "Format '#{format}' is not supported" end end |
#path ⇒ Object
37 38 39 40 41 |
# File 'lib/conifer/file.rb', line 37 def path return @path if defined? @path @path = find_path end |
#validate! ⇒ Object
51 52 53 |
# File 'lib/conifer/file.rb', line 51 def validate! raise NotFoundError, "Could not find file #{filename}" if path.nil? end |