Class: Yacht::Loader
- Inherits:
-
Object
- Object
- Yacht::Loader
- Defined in:
- lib/yacht/loader.rb,
lib/yacht/javascript.rb,
lib/yacht/classy_struct.rb
Class Attribute Summary collapse
-
.dir ⇒ Object
Returns the value of attribute dir.
- .environment ⇒ Object
Class Method Summary collapse
- .all ⇒ Object
- .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.
10 11 12 |
# File 'lib/yacht/loader.rb', line 10 def dir @dir end |
.environment ⇒ Object
5 6 7 |
# File 'lib/yacht/loader.rb', line 5 def environment @environment ||= 'default' end |
Class Method Details
.all ⇒ Object
30 31 32 |
# File 'lib/yacht/loader.rb', line 30 def all Yacht::HashHelper.deep_merge( chain_configs(base_config, self.environment), local_config ) end |
.base_config ⇒ Object
52 53 54 |
# File 'lib/yacht/loader.rb', line 52 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 23 24 |
# File 'lib/yacht/loader.rb', line 18 def config_file_for(config_type) if !valid_config_types.include?(config_type.to_s) raise Yacht::LoadError.new "#{config_type} is not a valid config type" end full_file_path_for_config(config_type) end |
.full_file_path_for_config(config_type) ⇒ Object
12 13 14 15 16 |
# File 'lib/yacht/loader.rb', line 12 def full_file_path_for_config(config_type) raise Yacht::LoadError.new "No directory set" unless self.dir File.join( self.dir, "#{config_type}.yml" ) end |
.js_keys ⇒ Object
16 17 18 |
# File 'lib/yacht/javascript.rb', line 16 def js_keys load_config_file(:js_keys, :expect_to_load => Array) || raise( Yacht::LoadError.new("Couldn't load js_keys") ) end |
.local_config ⇒ Object
56 57 58 |
# File 'lib/yacht/loader.rb', line 56 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
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/yacht/loader.rb', line 37 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?] Yacht::HashHelper.slice(all, *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
10 11 12 13 14 |
# File 'lib/yacht/javascript.rb', line 10 def to_js_snippet(opts={}) hash_to_merge = opts.delete(:merge) || {} hash = Yacht::HashHelper.slice(to_hash(opts), *js_keys).merge(hash_to_merge) ";var Yacht = #{hash.to_json};" end |
.valid_config_types ⇒ Object
26 27 28 |
# File 'lib/yacht/loader.rb', line 26 def valid_config_types %w( base local whitelist js_keys ) end |