Class: TorqueBox::Configuration::Entry

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

Direct Known Subclasses

OptionsEntry, ThingWithOptionsEntry, ThingsEntry

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Returns a new instance of Entry



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

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

- (Object) method_missing(method, *args, &block)



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

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

+ (Object) const_missing(name)



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

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

+ (Object) with_settings(options)



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

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

- (Object) add_options(option)



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

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

- (Object) eval_block(&block)



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

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

- (Object) finalize_options



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

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

- (Object) find_line_number



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

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

- (Object) local_config



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

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

- (Object) local_config=(value)



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

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

- (Object) process(*args, &block)



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

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

- (Object) process_args(unused)



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

def process_args(unused)
  # no op
  @config
end

- (Object) validate_options



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

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