Module: Plant::Core::ClassMethods

Defined in:
lib/plant/core.rb

Overview

All methods are class level, extend this class to allow for instance level variables

Instance Method Summary collapse

Instance Method Details

#defaultsObject



30
31
32
33
# File 'lib/plant/core.rb', line 30

def defaults
  { scenario: nil,
    parameters: {} }
end

#find_main(node) ⇒ Object

Private Find the main node or falls back to the node if main does not exist.



74
75
76
77
78
# File 'lib/plant/core.rb', line 74

def find_main(node)
  main = find_by(node_id: "#{node}.main")
  return main if main
  find_by!(node_id: "#{node}")
end

#find_specific(node, scenario) ⇒ Object

Private Finds a specific scenario



67
68
69
70
# File 'lib/plant/core.rb', line 67

def find_specific(node, scenario)
  scenario = scenario.scenario if scenario.respond_to? :scenario
  find_by(node_id: "#{node}.#{scenario}")
end

#get_node(node_id, options = {}) ⇒ Object

Gets a given node

=> Parameter: node ID (string)
=> Parameter: Options (hash), containing
   - scenario (optional)
     Can either be a string or an object with
     a scenario method, for example a user.

     If no specific scenario returned, then will
     default back to a nil scenario.

=> Throws: NotFound if node not found
=> Returns: A content object


25
26
27
28
# File 'lib/plant/core.rb', line 25

def get_node(node_id, options = {})
  options = defaults.merge(options)
  find_specific(node_id, options[:scenario]) || find_main(node_id)
end

#get_node_content(node_id, options = {}) ⇒ Object

Convenience method to get the content of a given node

=> Paramter: node ID (string)
=> Paramter: Options (hash)
 - scenario
   Can either be a string or an object with
   a scenario method, for example a user

 - Also can be any arbitary key to be injected into the
   returned content. For example
   When called get_node_content(node_id, foo: 'hhh)
   "This is content for #{foo} and bar"
   will return 'This is content for hhh and bar'

Note: If second parameter is a string, it will be treated as
scenario.

Also see: get_node



51
52
53
54
55
# File 'lib/plant/core.rb', line 51

def get_node_content(node_id, options = {})
  options = { scenario: options } unless options.is_a? Hash
  options = defaults.merge(options)
  inject_content(get_node(node_id, options).content, options)
end

#inject_content(content, options) ⇒ Object

Interpolation of content



58
59
60
61
62
63
# File 'lib/plant/core.rb', line 58

def inject_content(content, options)
  options.each do |key, replacement|
    content.gsub!("\#\{#{key}\}", replacement.to_s)
  end
  content
end