Class: Yacht::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/yacht/loader.rb,
lib/yacht/javascript.rb,
lib/yacht/classy_struct.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dirObject

Returns the value of attribute dir.



10
11
12
# File 'lib/yacht/loader.rb', line 10

def dir
  @dir
end

.environmentObject



5
6
7
# File 'lib/yacht/loader.rb', line 5

def environment
  @environment ||= 'default'
end

Class Method Details

.allObject



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_configObject



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_instanceObject



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_keysObject



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_configObject



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.message}")
  end
end

.to_hash(opts = {}) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: {})

    the options for creating the hash

Options Hash (opts):

  • :env (String)

    environment to use from base.yml

  • :apply_whitelist? (Boolean) — default: false

    only include keys in whitelist.yml



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

Parameters:

  • opts (Hash) (defaults to: {})

    the options to pass to to_hash

Options Hash (opts):

  • :merge (Hash) — default: {}

    hash to be merged into to_hash



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_typesObject



26
27
28
# File 'lib/yacht/loader.rb', line 26

def valid_config_types
  %w( base local whitelist js_keys )
end

.whitelistObject



48
49
50
# File 'lib/yacht/loader.rb', line 48

def whitelist
  load_config_file(:whitelist, :expect_to_load => Array) || raise( Yacht::LoadError.new("Couldn't load whitelist") )
end