Class: Ramaze::Option::Holder

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Merger
Defined in:
lib/ramaze/option/holder.rb

Instance Method Summary collapse

Methods included from Merger

#merge!, #merge_cli!, #merge_prefixed!, #option_parser

Constructor Details

#initialize(options = {}) ⇒ Holder

Returns a new instance of Holder.



11
12
13
14
15
16
17
# File 'lib/ramaze/option/holder.rb', line 11

def initialize(options = {})
  @members = Set.new

  options.each do |key, value|
    add_option(key, value, complain = false)
  end
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
# File 'lib/ramaze/option/holder.rb', line 30

def [](key)
  __send__(key)
end

#[]=(key, value) ⇒ Object



34
35
36
37
# File 'lib/ramaze/option/holder.rb', line 34

def []=(key, value)
  @members << key.to_s.to_sym
  __send__("#{key}=", value)
end

#adapterObject



112
113
114
# File 'lib/ramaze/option/holder.rb', line 112

def adapter
  find_from_aliases(@adapter, :adapter_aliases, Ramaze::Adapter, "ramaze/adapter")
end

#add_option(key, value, complain = true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/ramaze/option/holder.rb', line 19

def add_option(key, value, complain = true)
  Log.warn("Adding #{key} to #{self}") if complain

  self.class.class_eval do
    attr_reader key unless method_defined?(key)
    attr_writer key unless method_defined?("#{key}=")
  end

  self[key] = value
end

#cacheObject



116
117
118
# File 'lib/ramaze/option/holder.rb', line 116

def cache
  find_from_aliases(@cache, :cache_aliases, Ramaze, "ramaze/cache")
end

#eachObject



41
42
43
44
45
# File 'lib/ramaze/option/holder.rb', line 41

def each
  @members.each do |member|
    yield member, self[member]
  end
end

#port=(number) ⇒ Object

Modified options



69
70
71
# File 'lib/ramaze/option/holder.rb', line 69

def port=(number)
  @port = number.to_i
end

#public_rootObject

Find a suitable public_root, if none of these is a directory just use the currently set one.



79
80
81
82
83
84
85
# File 'lib/ramaze/option/holder.rb', line 79

def public_root
  r = [ pr = @public_root,
    root/pr,
  ].find{|path| File.directory?(path) } || @public_root

  File.expand_path(r)
end

#public_root=(pr) ⇒ Object



73
74
75
# File 'lib/ramaze/option/holder.rb', line 73

def public_root=(pr)
  @public_root = pr
end

#setup(hash = {}) {|_self| ... } ⇒ Object

batch-assignment of key/value from hash, yields self if a block is given.

Yields:

  • (_self)

Yield Parameters:



62
63
64
65
# File 'lib/ramaze/option/holder.rb', line 62

def setup(hash = {})
  merge!(hash)
  yield(self) if block_given?
end

#startup(options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ramaze/option/holder.rb', line 47

def startup(options)
  options.each do |key, value|
    self[key] = value
  end

  merge!(ARGV)
  merge!(ENV)

  self.root ||= File.dirname(File.expand_path(runner))
  [self.load_engines].flatten.compact.each do |engine|
    Ramaze::Template.const_get(engine)
  end
end

#template_rootObject



107
108
109
110
# File 'lib/ramaze/option/holder.rb', line 107

def template_root
  Ramaze::deprecated "Global.template_root", "Global.view_root"
  self.view_root
end

#template_root=(tr) ⇒ Object



102
103
104
105
# File 'lib/ramaze/option/holder.rb', line 102

def template_root=(tr)
  Ramaze::deprecated "Global.template_root=", "Global.view_root="
  self.view_root = tr
end

#view_rootObject

Find a suitable view_root, if none of these is a directory just use the currently set one.



93
94
95
96
97
98
99
100
# File 'lib/ramaze/option/holder.rb', line 93

def view_root
  r = [ vr = @view_root,
    root/vr,
    root/'template',
  ].find{|path| File.directory?(path) } || @view_root

  File.expand_path(r)
end

#view_root=(vr) ⇒ Object



87
88
89
# File 'lib/ramaze/option/holder.rb', line 87

def view_root=(vr)
  @view_root = vr
end