class Taketo::DSL

Attributes

config[R]
current_scope_object[R]
shared_server_configs[R]

Public Class Methods

define_method_in_scope(name, *parent_scopes, &block) click to toggle source
# File lib/taketo/dsl.rb, line 31
def define_method_in_scope(name, *parent_scopes, &block)
  define_method name do |*args, &argument_block|
    ensure_nesting_allowed!(name, parent_scopes)
    args.push argument_block if argument_block
    instance_exec(*args, &block)
  end
end
define_scope(scope, *parent_scopes_and_options, &scope_setup_block) click to toggle source
# File lib/taketo/dsl.rb, line 10
def define_scope(scope, *parent_scopes_and_options, &scope_setup_block)
  options = parent_scopes_and_options.last.is_a?(Hash) ? parent_scopes_and_options.pop : {}
  parent_scopes = parent_scopes_and_options

  define_method scope do |*args, &scope_block|
    ensure_nesting_allowed!(scope, parent_scopes)
    name = args.shift || options[:default_name] or raise(ArgumentError, "Name not specified")

    scope_object = current_scope_object.find(scope, name) { @factory.create(scope, name) }

    in_scope(scope, scope_object) do
      instance_exec(current_scope_object, &scope_setup_block) if scope_setup_block
      scope_block.call
    end
  end

  define_method("#{scope}_scope?") do
    current_scope == scope
  end
end
new(factory = Taketo::ConstructsFactory.new) click to toggle source
# File lib/taketo/dsl.rb, line 42
def initialize(factory = Taketo::ConstructsFactory.new)
  @factory = factory
  @scope = [:config]
  @config = @current_scope_object = factory.create_config
  @shared_server_configs = Hash.new { |h, k| raise ConfigError, "Shared server config '#{k}' is not defined!"}
end

Public Instance Methods

configure(filename = nil, &block) click to toggle source
# File lib/taketo/dsl.rb, line 49
def configure(filename = nil, &block)
  if filename
    filename = filename.to_s
    config_text = File.read(filename)
    instance_eval config_text, filename, 1
  elsif block
    instance_eval(&block)
  else
    raise ArgumentError, "Either filename or block should be provided"
  end
  @config
end

Private Instance Methods

current_scope() click to toggle source
# File lib/taketo/dsl.rb, line 103
def current_scope
  @scope.last
end
current_scope?(scope) click to toggle source
# File lib/taketo/dsl.rb, line 107
def current_scope?(scope)
  current_scope == scope
end
ensure_nesting_allowed!(scope, parent_scopes) click to toggle source
# File lib/taketo/dsl.rb, line 121
def ensure_nesting_allowed!(scope, parent_scopes)
  if Array(parent_scopes).none? { |s| current_scope?(s) }
    raise ScopeError, "#{scope} can't be defined in #{current_scope} scope"
  end
end
extract_config_names_and_arguments(args) click to toggle source
# File lib/taketo/dsl.rb, line 127
def extract_config_names_and_arguments(args)
  hashes, names = args.partition { |arg| arg.is_a? Hash }
  configs_from_hashes = hashes.inject({}, &:merge)
  configs_from_names = Hash[names.map { |config_name| [config_name.to_sym, []] }]
  configs_from_hashes.merge(configs_from_names)
end
in_scope(scope, new_scope_object) { || ... } click to toggle source
# File lib/taketo/dsl.rb, line 111
def in_scope(scope, new_scope_object)
  parent_scope_object, @current_scope_object = @current_scope_object, new_scope_object
  @scope.push(scope)
  parent_scope_object.send("append_#{scope}", current_scope_object)
  current_scope_object.parent = parent_scope_object
  yield
  @scope.pop
  @current_scope_object = parent_scope_object
end