Class: YouthTree::Settings
- Inherits:
-
Object
- Object
- YouthTree::Settings
show all
- Defined in:
- lib/youth_tree/settings.rb
Constant Summary
collapse
- VERSION =
"1.0.2".freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}) ⇒ Settings
Returns a new instance of Settings.
9
10
11
12
|
# File 'lib/youth_tree/settings.rb', line 9
def initialize(hash = {})
@hash = Hash.new { |h,k| h[k] = self.class.new }
hash.each_pair { |k, v| self[k] = v }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &blk) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/youth_tree/settings.rb', line 43
def method_missing(name, *args, &blk)
name = name.to_s
key, modifier = name[0..-2], name[-1, 1]
case modifier
when '?'
has?(key)
when '='
send(:[]=, key, *args)
else
self[name]
end
end
|
Class Method Details
.default ⇒ Object
82
83
84
|
# File 'lib/youth_tree/settings.rb', line 82
def default
@@default ||= new(load_from_file)
end
|
.load_from_file ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/youth_tree/settings.rb', line 70
def load_from_file
if !File.readable?(settings_path)
$stderr.puts "Unable to load settings from #{settings_path} - Please check it exists and is readable."
return {}
end
contents = File.read(settings_path)
contents = ERB.new(contents).result
contents = YAML.load(contents)
(contents["default"] || {}).deep_merge(contents[Rails.env] || {})
end
|
.method_missing(name, *args, &blk) ⇒ Object
95
96
97
|
# File 'lib/youth_tree/settings.rb', line 95
def method_missing(name, *args, &blk)
default.send(name, *args, &blk)
end
|
.reset! ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/youth_tree/settings.rb', line 86
def reset!
@@default = nil
default YouthTree::Settings.setup_mailer!
@setup_callback.call if defined?(@setup_callback) && @setup_callback
true
end
|
.respond_to?(name, key = false) ⇒ Boolean
99
100
101
|
# File 'lib/youth_tree/settings.rb', line 99
def respond_to?(name, key = false)
true
end
|
.settings_path ⇒ Object
66
67
68
|
# File 'lib/youth_tree/settings.rb', line 66
def settings_path
@@settings_path ||= Rails.root.join("config", "settings.yml").to_s
end
|
.setup(value = nil, &blk) ⇒ Object
62
63
64
|
# File 'lib/youth_tree/settings.rb', line 62
def setup(value = nil, &blk)
@@setup_callback = (blk || value)
end
|
.setup_mailer! ⇒ Object
Sets up ActionMailer to use settings from Settings.
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/youth_tree/settings.rb', line 112
def setup_mailer!
return unless mailer?
s = mailer
ActionMailer::Base.default_url_options[:host] = s.host
ActionMailer::Base.delivery_method = s.delivery_method.to_sym
ActionMailer::Base.smtp_settings = s.smtp_settings.to_hash if s.smtp_settings?
ActionMailer::Base.sendmail_settings = s.sendmail_settings.to_hash if s.sendmail_settings?
ActionMailer::Base.default :from => s.from
if s.delivery_method.to_sym == :sendgrid
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => "25",
:authentication => :plain,
:user_name => s.sendgrid.username,
:password => s.sendgrid.password,
:domain => s.sendgrid.domain
}
end
end
|
.ssl? ⇒ Boolean
103
104
105
|
# File 'lib/youth_tree/settings.rb', line 103
def ssl?
!disable_ssl? && (force_ssl? || Rails.env.production?)
end
|
.ssl_protocol ⇒ Object
107
108
109
|
# File 'lib/youth_tree/settings.rb', line 107
def ssl_protocol
ssl? ? "https" : "http"
end
|
Instance Method Details
#[](k) ⇒ Object
22
23
24
|
# File 'lib/youth_tree/settings.rb', line 22
def [](k)
@hash[k.to_sym]
end
|
#[]=(k, v) ⇒ Object
18
19
20
|
# File 'lib/youth_tree/settings.rb', line 18
def []=(k, v)
@hash[k.to_sym] = normalize_attr(v)
end
|
#blank? ⇒ Boolean
35
36
37
|
# File 'lib/youth_tree/settings.rb', line 35
def blank?
@hash.blank?
end
|
#fetch(key, default = nil) ⇒ Object
31
32
33
|
# File 'lib/youth_tree/settings.rb', line 31
def fetch(key, default = nil)
has?(key) ? self[key] : default
end
|
#has?(key) ⇒ Boolean
26
27
28
29
|
# File 'lib/youth_tree/settings.rb', line 26
def has?(key)
key = key.to_sym
@hash.has_key?(key) && @hash[key].present?
end
|
#present? ⇒ Boolean
39
40
41
|
# File 'lib/youth_tree/settings.rb', line 39
def present?
!blank?
end
|
#respond_to?(name, key = false) ⇒ Boolean
56
57
58
|
# File 'lib/youth_tree/settings.rb', line 56
def respond_to?(name, key = false)
true
end
|
#to_hash ⇒ Object
14
15
16
|
# File 'lib/youth_tree/settings.rb', line 14
def to_hash
unpack_attr @hash
end
|