Class: Taketo::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/taketo/dsl.rb

Defined Under Namespace

Classes: ConfigError, ScopeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory = Taketo::ConstructsFactory.new) ⇒ DSL

Returns a new instance of DSL.



42
43
44
45
46
47
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



40
41
42
# File 'lib/taketo/dsl.rb', line 40

def config
  @config
end

#current_scope_objectObject (readonly)

Returns the value of attribute current_scope_object.



40
41
42
# File 'lib/taketo/dsl.rb', line 40

def current_scope_object
  @current_scope_object
end

#shared_server_configsObject (readonly)

Returns the value of attribute shared_server_configs.



40
41
42
# File 'lib/taketo/dsl.rb', line 40

def shared_server_configs
  @shared_server_configs
end

Class Method Details

.define_method_in_scope(name, *parent_scopes, &block) ⇒ Object



31
32
33
34
35
36
37
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#configure(filename = nil, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# 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