Module: Itrigga::Config::InstanceMethods
- Defined in:
- lib/itrigga/config/macros.rb
Instance Method Summary collapse
- #apply_action_mailer_config(site_key) ⇒ Object
- #from_file(f) ⇒ Object
- #from_hash(h) ⇒ Object
- #initialize(file = nil) ⇒ Object
- #load_site_config(site_name) ⇒ Object
- #load_site_config_file(site_name, filename) ⇒ Object
- #load_site_config_for_all_sites ⇒ Object
- #loaded_file ⇒ Object
- #loaded_file_basename ⇒ Object
- #site_config(site_key) ⇒ Object
- #site_config_files(site_dir) ⇒ Object
- #site_config_root ⇒ Object
- #site_config_with_defaults(site_key) ⇒ Object
- #site_keys ⇒ Object
- #to_file(f) ⇒ Object
- #to_hash ⇒ Object
Instance Method Details
#apply_action_mailer_config(site_key) ⇒ Object
177 178 179 180 181 182 |
# File 'lib/itrigga/config/macros.rb', line 177 def apply_action_mailer_config( site_key ) Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE) mailer_settings = site_config_with_defaults(site_key)[:actionmailer] ActionMailer::Base.smtp_settings = mailer_settings[:smtp_settings] end |
#from_file(f) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/itrigga/config/macros.rb', line 102 def from_file( f ) raise ArgumentError.new("#{f.to_s} is not a valid file") unless File.file?(f.to_s) h = File.open( f ){ |yml| ::YAML::load( yml ) } c = from_hash(h) self.instance_variable_set("@from_file", f.to_s) c end |
#from_hash(h) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/itrigga/config/macros.rb', line 93 def from_hash( h ) h.keys.each do |k| self.class.def_param(k,h[k]) unless params.include?(k) self.class.instance_variable_set("@#{k}", h[k] ) self.instance_variable_set("@#{k}", h[k] ) end self end |
#initialize(file = nil) ⇒ Object
80 81 82 83 |
# File 'lib/itrigga/config/macros.rb', line 80 def initialize(file=nil) self.from_file(file) unless file.to_s.empty? self end |
#load_site_config(site_name) ⇒ Object
145 146 147 148 149 150 151 152 |
# File 'lib/itrigga/config/macros.rb', line 145 def load_site_config( site_name ) h = {} site_config_files(site_name).each do |f| key_name = f.gsub(/(.*)\.yml/, '\1').to_sym h[key_name] = load_site_config_file(site_name, f) end h end |
#load_site_config_file(site_name, filename) ⇒ Object
140 141 142 143 |
# File 'lib/itrigga/config/macros.rb', line 140 def load_site_config_file( site_name, filename ) path = File.join(site_config_root, site_name, filename) File.open( path ){ |yml| ::YAML::load( yml ) } end |
#load_site_config_for_all_sites ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/itrigga/config/macros.rb', line 154 def load_site_config_for_all_sites default_config = load_site_config('default_site') h = {} site_keys.each do |site| h[site] = default_config.merge(load_site_config(site)) end self.sites = h self end |
#loaded_file ⇒ Object
116 117 118 |
# File 'lib/itrigga/config/macros.rb', line 116 def loaded_file self.instance_variable_get("@from_file") end |
#loaded_file_basename ⇒ Object
120 121 122 |
# File 'lib/itrigga/config/macros.rb', line 120 def loaded_file_basename loaded_file ? File.basename(loaded_file) : nil end |
#site_config(site_key) ⇒ Object
166 167 168 |
# File 'lib/itrigga/config/macros.rb', line 166 def site_config( site_key ) self.sites[site_key] end |
#site_config_files(site_dir) ⇒ Object
134 135 136 137 138 |
# File 'lib/itrigga/config/macros.rb', line 134 def site_config_files(site_dir) Dir.chdir(File.join(site_config_root, site_dir)) do Dir['*.yml'] end end |
#site_config_root ⇒ Object
124 125 126 |
# File 'lib/itrigga/config/macros.rb', line 124 def site_config_root "#{RAILS_ROOT}/config/itrigga" end |
#site_config_with_defaults(site_key) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/itrigga/config/macros.rb', line 170 def site_config_with_defaults( site_key ) config = site_config('default_site').clone this_site_config = site_config(site_key) config.merge!(this_site_config) if this_site_config config end |
#site_keys ⇒ Object
128 129 130 131 132 |
# File 'lib/itrigga/config/macros.rb', line 128 def site_keys Dir.chdir(site_config_root) do Dir['*/'].map{ |d| d.gsub( /(.*)[\/\\]/, '\1' ) } end end |
#to_file(f) ⇒ Object
110 111 112 113 114 |
# File 'lib/itrigga/config/macros.rb', line 110 def to_file(f) File.open( f, "w+" ) do |f| f.write( to_hash.to_yaml ) end end |
#to_hash ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/itrigga/config/macros.rb', line 85 def to_hash h = {} params.each do |p| h[p] = param(p) end h end |