Module: Yettings

Defined in:
lib/yettings.rb,
lib/yettings/railtie.rb

Defined Under Namespace

Classes: Railtie, UndefinedYetting

Class Method Summary collapse

Class Method Details

.create_yetting_class(yml_file) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/yettings.rb', line 22

def create_yetting_class(yml_file)
  hash = load_yml(yml_file)
  klass_name = File.basename(yml_file).gsub(".yml","").camelize
  klass_name = "#{klass_name}Yetting" unless klass_name=="Yetting"
  klass = Object.const_set(klass_name,Class.new)
  hash.each do |key,value|
    klass.define_singleton_method(key){ value }
  end
  klass.class_eval do
    def self.method_missing(method_id,*args)
      raise UndefinedYetting, "#{method_id} is not defined in #{self.to_s}"
    end
  end
end

.find_ymlsObject



15
16
17
18
19
20
# File 'lib/yettings.rb', line 15

def find_ymls
  main_file = "#{Rails.root.to_s}/config/yetting.yml"
  yettings_main_file = File.exists?(main_file) ? [main_file] : []
  yettings_namespaced_files = Dir.glob("#{Rails.root.to_s}/config/yettings/**/*.yml")
  yettings_main_file.concat(yettings_namespaced_files)
end

.load_yml(yml_file) ⇒ Object



37
38
39
40
# File 'lib/yettings.rb', line 37

def load_yml(yml_file)
  erb = ERB.new(File.read(yml_file)).result
  YAML.load(erb).to_hash[Rails.env]
end

.setup!Object



9
10
11
12
13
# File 'lib/yettings.rb', line 9

def setup!
  find_ymls.each do |yml|
    create_yetting_class(yml)
  end
end