Class: TorqueBox::Configuration::Entry

Inherits:
BlankSlate
  • Object
show all
Defined in:
lib/torquebox/configuration.rb

Direct Known Subclasses

OptionsEntry, ThingWithOptionsEntry, ThingsEntry

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, entry_map, options = { }) ⇒ Entry

Returns a new instance of Entry.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/torquebox/configuration.rb', line 38

def initialize(name, config, entry_map, options = { })
  @name = name
  @config = config
  @entry_map = entry_map
  @parents = options.delete( :parents ) || []
  @options = options
  @line_number = find_line_number
  @entry_options = { }
  
  if options[:require_parent] && ([options[:require_parent]].flatten & @parents).empty?
    raise ConfigurationError.new( "#{@name} only allowed inside #{options[:require_parent]}", @line_number )
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/torquebox/configuration.rb', line 103

def method_missing(method, *args, &block)
  klass = @entry_map[method]
  if klass
    entry = klass.new( method, @entry_options, @entry_map, :parents => @parents + [@name] )
    entry.process( *args, &block )
  else
    add_options( method.to_sym => args.first )
  end
end

Class Method Details

.const_missing(name) ⇒ Object



83
84
85
# File 'lib/torquebox/configuration.rb', line 83

def self.const_missing(name)
  FakeConstant.new( name ).to_const
end

.with_settings(options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/torquebox/configuration.rb', line 87

def self.with_settings(options)
  klass = self
  proxy = Object.new
  (class << proxy; self; end).__send__( :define_method, :new ) do |*args|
    if args.last.is_a?( Hash )
      args.last.merge!( options )
    else
      args << options
    end
    klass.new( *args )
  end
  proxy
end

Instance Method Details

#add_options(option) ⇒ Object



113
114
115
# File 'lib/torquebox/configuration.rb', line 113

def add_options( option )
  @entry_options.merge!( option )
end

#eval_block(&block) ⇒ Object



79
80
81
# File 'lib/torquebox/configuration.rb', line 79

def eval_block(&block)
  block.arity < 1 ? self.instance_eval( &block ) : block.call( self )
end

#finalize_optionsObject



117
118
119
120
121
122
123
124
# File 'lib/torquebox/configuration.rb', line 117

def finalize_options
  if @options[:discrete]
    local_config << @entry_options
  else
    @entry_options = local_config.merge!( @entry_options )
  end
  local_config
end

#find_line_numberObject



52
53
54
55
56
57
# File 'lib/torquebox/configuration.rb', line 52

def find_line_number
  caller.each do |line|
    return $1 if line =~ /\(eval\):(\d+):/
  end
  nil
end

#local_configObject



126
127
128
129
# File 'lib/torquebox/configuration.rb', line 126

def local_config
  @config[@name.to_s] = [] if @options[:discrete] && !@config[@name.to_s].is_a?(Array)
  @config[@name.to_s] ||= {}  
end

#local_config=(value) ⇒ Object



131
132
133
# File 'lib/torquebox/configuration.rb', line 131

def local_config=(value)
  @config[@name.to_s] = value
end

#process(*args, &block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/torquebox/configuration.rb', line 59

def process(*args, &block)
  process_args( args )
  eval_block( &block ) if block_given?
  validate_options
  finalize_options
  local_config
end

#process_args(unused) ⇒ Object



67
68
69
70
# File 'lib/torquebox/configuration.rb', line 67

def process_args(unused)
  # no op
  @config
end

#validate_optionsObject



72
73
74
75
76
77
# File 'lib/torquebox/configuration.rb', line 72

def validate_options
  if @options[:validate]
    validator = Validator.new( @options[:validate], @name, @entry_options )
    raise ConfigurationError.new( validator.message, @line_number ) unless validator.valid?
  end
end