Class: SimplyGenius::Atmos::Template
- Inherits:
-
Object
- Object
- SimplyGenius::Atmos::Template
- Includes:
- GemLogger::LoggerSupport
- Defined in:
- lib/simplygenius/atmos/template.rb
Constant Summary collapse
- TEMPLATES_SPEC_FILE =
'templates.yml'
- TEMPLATES_ACTIONS_FILE =
'templates.rb'
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #actions ⇒ Object
- #actions_path ⇒ Object
- #config ⇒ Object
- #config_path ⇒ Object
- #context_path ⇒ Object
- #dependencies ⇒ Object
- #dup ⇒ Object
-
#initialize(name, directory, source, context: {}) ⇒ Template
constructor
A new instance of Template.
- #optional ⇒ Object
- #scoped_context ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
-
#walk_dependencies(seen = Set.new) ⇒ Object
depth first iteration of dependencies.
Constructor Details
#initialize(name, directory, source, context: {}) ⇒ Template
Returns a new instance of Template.
15 16 17 18 19 20 21 |
# File 'lib/simplygenius/atmos/template.rb', line 15 def initialize(name, directory, source, context: {}) @name = name @directory = directory @source = source @context = context @context = SettingsHash.new(@context) unless @context.kind_of?(SettingsHash) end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
13 14 15 |
# File 'lib/simplygenius/atmos/template.rb', line 13 def context @context end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
13 14 15 |
# File 'lib/simplygenius/atmos/template.rb', line 13 def directory @directory end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/simplygenius/atmos/template.rb', line 13 def name @name end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
13 14 15 |
# File 'lib/simplygenius/atmos/template.rb', line 13 def source @source end |
Instance Method Details
#actions ⇒ Object
48 49 50 |
# File 'lib/simplygenius/atmos/template.rb', line 48 def actions @actions ||= (File.exist?(actions_path) ? File.read(actions_path) : "") end |
#actions_path ⇒ Object
44 45 46 |
# File 'lib/simplygenius/atmos/template.rb', line 44 def actions_path File.join(directory, TEMPLATES_ACTIONS_FILE) end |
#config ⇒ Object
56 57 58 59 60 61 |
# File 'lib/simplygenius/atmos/template.rb', line 56 def config @config ||= begin data = File.read(config_path) SettingsHash.new(YAML.load(data) || {}) end end |
#config_path ⇒ Object
52 53 54 |
# File 'lib/simplygenius/atmos/template.rb', line 52 def config_path File.join(directory, TEMPLATES_SPEC_FILE) end |
#context_path ⇒ Object
31 32 33 |
# File 'lib/simplygenius/atmos/template.rb', line 31 def context_path name.gsub('-', '_').gsub('/', '.') end |
#dependencies ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/simplygenius/atmos/template.rb', line 69 def dependencies @dependencies ||= begin deps = Array(config[:dependent_templates]) deps.collect do |d| if d.kind_of?(String) tmpl = SourcePath.find_template(d) elsif d.kind_of?(Hash) raise ArgumentError.new("Template must be named with name key: #{tmpl}") unless d[:name] tmpl = SourcePath.find_template(d[:name]) tmpl.context.merge!(d[:context]) if d[:context] else raise TypeError.new("Invalid template structure: #{d}") end tmpl end end end |
#dup ⇒ Object
88 89 90 91 |
# File 'lib/simplygenius/atmos/template.rb', line 88 def dup dependencies Marshal.load(Marshal.dump(self)) end |
#optional ⇒ Object
63 64 65 66 67 |
# File 'lib/simplygenius/atmos/template.rb', line 63 def optional result = config[:optional] || {} raise TypeError.new("Template config item :optional must be a hash: #{result.inspect}") unless result.is_a?(Hash) result end |
#scoped_context ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/simplygenius/atmos/template.rb', line 35 def scoped_context result = context.notation_get(context_path) if result.nil? context.notation_put(context_path, SettingsHash.new, additive: false) result = context.notation_get(context_path) end result end |
#to_h ⇒ Object
27 28 29 |
# File 'lib/simplygenius/atmos/template.rb', line 27 def to_h SettingsHash.new({name: name, source: source.to_h, context: context}) end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/simplygenius/atmos/template.rb', line 23 def to_s "#{name}" end |
#walk_dependencies(seen = Set.new) ⇒ Object
depth first iteration of dependencies
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/simplygenius/atmos/template.rb', line 94 def walk_dependencies(seen=Set.new) Enumerator.new do |yielder| if seen.include?(name) seen << name raise ArgumentError.new("Circular template dependency: #{seen.to_a.join(" => ")}") end seen << name dependencies.each do |dep| dep = dep.dup dep.context.merge!(context) dep.walk_dependencies(seen.dup).each do |d| yielder << d end end yielder << dup end end |