Module: FunWith::Configurations::ConfigAPI
- Included in:
- Config
- Defined in:
- lib/fun_with/configurations/config_api.rb
Overview
methods on the FunWith::Configurations::Config class itself.
Instance Method Summary collapse
- #from_file(file) ⇒ Object
- #from_hash(hash) ⇒ Object
- #from_yaml(yaml_string) ⇒ Object
- #fwc_overridden_methods ⇒ Object
- #key_check(sym) ⇒ Object
Instance Method Details
#from_file(file) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/fun_with/configurations/config_api.rb', line 13 def from_file( file ) file = file.fwf_filepath case file.ext when "rb" self.new do eval( file.read ) end when "yml", "yaml" self.from_yaml( file.read ) else warn( "Unknown filetype: #{file.ext} (file:#{file}}). Returning empty config." ) self.new end end |
#from_hash(hash) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/fun_with/configurations/config_api.rb', line 29 def from_hash( hash ) (config = self.new).tap do for k, v in hash config.send( k, v.is_a?( Hash ) ? self.from_hash( v ) : v ) end end config end |
#from_yaml(yaml_string) ⇒ Object
38 39 40 |
# File 'lib/fun_with/configurations/config_api.rb', line 38 def from_yaml( yaml_string ) self.from_hash( Psych.load( yaml_string ) ) end |
#fwc_overridden_methods ⇒ Object
42 43 44 |
# File 'lib/fun_with/configurations/config_api.rb', line 42 def fwc_overridden_methods ConfigOverriddenMethods.instance_methods.grep( /[^=]$/ ) end |
#key_check(sym) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/fun_with/configurations/config_api.rb', line 5 def key_check( sym ) @reserved_symbols ||= Config.instance_methods - self.fwc_overridden_methods raise KeyError.new("#{sym} is not a symbol") unless sym.is_a?(Symbol) raise KeyError.new("#{sym} is reserved for use by Hash") if @reserved_symbols.include?( sym ) end |