Module: Achiever::Config
- Defined in:
- lib/config.rb
Constant Summary collapse
- DEFAULT =
{ data: {}, mtime: Time.at(0), achievements_file: 'config/achievements.yml', default_image: '', default_type: 'accumulation', default_visibility: 'visible' }.freeze
- PRIVATE =
%i[data mtime].freeze
Class Method Summary collapse
- .append_features(rcvr, names: %i[config configure],, prefix: :'') ⇒ Object
-
.methods_for(config, rcvr, prefix) ⇒ Object
define convinence methods for each of the configuration paramaters special methods added for parameters that take booleans, e.g.
Class Method Details
.append_features(rcvr, names: %i[config configure],, prefix: :'') ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/config.rb', line 16 def append_features(rcvr, names: %i[config configure], prefix: :'') methods_for(DEFAULT, rcvr, prefix) + names.map do |name| # If called with a block, executes the block setting the argument to rcvr, # else returns the configuration hash rcvr.define_singleton_method(name) do |&block| @config ||= Achiever::Config::DEFAULT.dup block.nil? ? @config : block.call(rcvr) end end end |
.methods_for(config, rcvr, prefix) ⇒ Object
define convinence methods for each of the configuration paramaters special methods added for parameters that take booleans, e.g. dont_xxxx or xxxx!
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/config.rb', line 31 def methods_for(config, rcvr, prefix) config.reject { |k, _| PRIVATE.include?(k) }.map do |k, v| (case v when true, false [[:"dont_#{k}", -> { @config[k] = false }], [:"do_#{k}", -> { @config[k] = true }], [:"#{k}!", -> { @config[k] = true }], [:"#{k}?", -> { @config[k] }], [:"#{k}=", lambda { |l| unless [TrueClass, FalseClass].include?(l.class) raise TypeError, "expected #{l.inspect} to be a kind_of? Bool (true, or false)" end @config[k] = l }]] else [[:"#{k}=", lambda { |l| unless l.is_a?(v.class) raise TypeError, "expected #{l.inspect} to be a kind_of? #{v.class}" end @config[k] = l }]] end + [[:"#{k}", -> { @config[k] }]] ).map do |meth, body| rcvr.define_singleton_method(:"#{prefix}#{meth}", &body) end end.flatten end |