class Bytewise::Context

Attributes

filename[RW]
klass[R]
vars[R]

Public Class Methods

new(klass, variables: {}, filename: nil) click to toggle source
command

klass { Object } The current controller context

# File lib/brace_markup/context.rb, line 9
def initialize(klass, variables: {}, filename: nil)
  @vars = variables
  @klass = klass
  @filename = filename
end
resolve_reference(name, target) click to toggle source

Resolves a reference key in a specified target

@param name { String|Array } @param target { Object }

# File lib/brace_markup/context.rb, line 35
def self.resolve_reference(name, target)
  parts = name.is_a?(Array) ? name : name.split('.')
  first = parts.shift
  value = nil

  if target.class.method_defined? :'[]'
    # Is numeric
    if first =~ /^[0-9]+$/
      value = target[first.to_i]
    end

    unless target.is_a? Array
      # First try it with the Symbol
      value = target[first.to_sym] if value.nil?
      # ... then with the String
      value = target[first] if value.nil?
    end
  end

  if value.nil?
    begin
      value = target.send(first)
    rescue NoMethodError
      begin
        value = target.instance_variable_get("@#{first}")
      rescue NameError
        value = nil
      end
    end
  end

  parts.length < 1 ? value : self.resolve_reference(parts, value)
end

Public Instance Methods

resolve_reference(name) click to toggle source

First tries to resolve the reference in the variables, then in the current class (controller) context

# File lib/brace_markup/context.rb, line 19
def resolve_reference(name)
  ref = Context.resolve_reference(name, @vars)

  if ref.nil?
    ref = Context.resolve_reference(name, @klass)
  end

  ref
end