module Plant::Core::ClassMethods

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

Public Instance Methods

defaults() click to toggle source
# File lib/plant/core.rb, line 30
def defaults
  { scenario: nil,
    parameters: {} }
end
find_main(node) click to toggle source

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

# 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) click to toggle source

Private Finds a specific scenario

# 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 = {}) click to toggle source

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
# 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 = {}) click to toggle source

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

# 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) click to toggle source

Interpolation of content

# 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