Class: Yacht::Loader
- Inherits:
-
Object
- Object
- Yacht::Loader
- Defined in:
- lib/yacht/loader.rb,
lib/yacht/rails.rb,
lib/yacht/javascript.rb,
lib/yacht/classy_struct.rb
Overview
TODO: Rename YachtLoader to Yacht and somehow incorporate ClassyStruct
Class Attribute Summary collapse
-
.dir ⇒ Object
Returns the value of attribute dir.
-
.environment ⇒ Object
use the current rails environment by default.
Class Method Summary collapse
- .all ⇒ Object
-
.all_with_rails_env ⇒ Object
Add current Rails environment to defined keys.
- .base_config ⇒ Object
- .classy_struct_instance ⇒ Object
- .config_file_for(config_type) ⇒ Object
- .full_file_path_for_config(config_type) ⇒ Object
- .js_keys ⇒ Object
- .local_config ⇒ Object
- .to_classy_struct(opts = {}) ⇒ Object
- .to_hash(opts = {}) ⇒ Object
-
.to_js_snippet(opts = {}) ⇒ Object
Returns a string snippet that can be eval’d in javascript or included in the DOM.
- .valid_config_types ⇒ Object
- .whitelist ⇒ Object
Class Attribute Details
.dir ⇒ Object
Returns the value of attribute dir.
8 9 10 |
# File 'lib/yacht/rails.rb', line 8 def dir @dir ||= Rails.root.join('config', 'yacht') end |
.environment ⇒ Object
use the current rails environment by default
4 5 6 |
# File 'lib/yacht/rails.rb', line 4 def environment @environment ||= Rails.env end |
Class Method Details
.all ⇒ Object
28 29 30 |
# File 'lib/yacht/loader.rb', line 28 def all chain_configs(base_config, self.environment).deep_merge(local_config) end |
.all_with_rails_env ⇒ Object
Add current Rails environment to defined keys
17 18 19 |
# File 'lib/yacht/rails.rb', line 17 def all_with_rails_env all_without_rails_env.merge('rails_env' => Rails.env) end |
.base_config ⇒ Object
50 51 52 |
# File 'lib/yacht/loader.rb', line 50 def base_config load_config_file(:base) || raise( Yacht::LoadError.new("Couldn't load base config") ) end |
.classy_struct_instance ⇒ Object
6 7 8 |
# File 'lib/yacht/classy_struct.rb', line 6 def classy_struct_instance @classy_struct_instance ||= ClassyStruct.new end |
.config_file_for(config_type) ⇒ Object
18 19 20 21 22 |
# File 'lib/yacht/loader.rb', line 18 def config_file_for(config_type) raise Yacht::LoadError.new "#{config_type} is not a valid config type" unless valid_config_types.include?(config_type.to_s) full_file_path_for_config(config_type) end |
.full_file_path_for_config(config_type) ⇒ Object
12 13 14 |
# File 'lib/yacht/rails.rb', line 12 def full_file_path_for_config(config_type) dir.join("#{config_type}.yml") end |
.js_keys ⇒ Object
15 16 17 18 19 |
# File 'lib/yacht/javascript.rb', line 15 def js_keys load_config_file(:js_keys, :expect_to_load => Array) || begin raise Yacht::LoadError.new("Couldn't load js_keys") end end |
.local_config ⇒ Object
54 55 56 |
# File 'lib/yacht/loader.rb', line 54 def local_config load_config_file(:local) || {} end |
.to_classy_struct(opts = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/yacht/classy_struct.rb', line 10 def to_classy_struct(opts={}) classy_struct_instance.new( self.to_hash(opts) ) rescue StandardError => e # don't do anything to our own custom errors if e.is_a? Yacht::LoadError raise e else raise Yacht::LoadError.new("Error creating ClassyStruct: #{e.}") end end |
.to_hash(opts = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/yacht/loader.rb', line 35 def to_hash(opts={}) opts[:apply_whitelist?] ||= false unless opts.has_key?(:apply_whitelist?) self.environment = opts[:env] if opts.has_key?(:env) if opts[:apply_whitelist?] all.slice(*whitelist) else all end end |
.to_js_snippet(opts = {}) ⇒ Object
Returns a string snippet that can be eval’d in javascript
or included in the DOM
9 10 11 12 13 |
# File 'lib/yacht/javascript.rb', line 9 def to_js_snippet(opts={}) hash_to_merge = opts.delete(:merge) || {} hash = to_hash(opts).slice(*js_keys).merge(hash_to_merge) ";var Yacht = #{hash.to_json};" end |
.valid_config_types ⇒ Object
24 25 26 |
# File 'lib/yacht/loader.rb', line 24 def valid_config_types %w( base local whitelist js_keys ) end |