lib/nugrant/parameters.rb in nugrant-2.0.0.dev2 vs lib/nugrant/parameters.rb in nugrant-2.0.0.pre1
- old
+ new
@@ -1,87 +1,33 @@
require 'nugrant/bag'
require 'nugrant/config'
require 'nugrant/helper/bag'
+require 'nugrant/mixin/parameters'
module Nugrant
class Parameters
- attr_reader :__current, :__user, :__system, :__defaults, :__all
-
##
# Create a new parameters object which holds completed
# merged values. The following precedence is used to decide
# which location has precedence over which location:
#
# (Highest) ------------------ (Lowest)
- # current < user < system < defaults
+ # project < user < system < defaults
#
- # =| Options
- # * +:config+ - A hash that will be passed to Nugrant::Config.new().
- # See Nugrant::Config constructor for options that you can use.
- # * +:defaults+ - The default values for the various parameters that will be read. This
- # must be a Hash object.
+ # =| Arguments
+ # * `config`
+ # A hash that will be passed to Nugrant::Config.new() or
+ # a Nugrant::Config instance directly.
+ # See Nugrant::Config constructor for options that you can use.
#
- def initialize(options = {})
- @__config = Config.new(options[:config])
-
- @__current = Helper::Bag.read(@__config.current_path, @__config.params_format)
- @__user = Helper::Bag.read(@__config.user_path, @__config.params_format)
- @__system = Helper::Bag.read(@__config.system_path, @__config.params_format)
- @__defaults = Bag.new(options[:defaults] || {})
-
- __compute_all()
- end
-
- def [](key)
- return @__all[key]
- end
-
- def method_missing(method, *args, &block)
- return @__all[method]
- end
-
- def empty?()
- @__all.empty?()
- end
-
- def has?(key)
- return @__all.has?(key)
- end
-
- def each(&block)
- @__all.each(&block)
- end
-
- ##
- # Set the new default values for the
- # various parameters contain by this instance.
- # This will call __compute_all() to recompute
- # correct precedences.
+ # * `options`
+ # An options hash that is passed to Mixin::Parameters.compute_bags! method.
+ # See Mixin::Parameters.compute_bags! for details on the various options
+ # available.
#
- # =| Attributes
- # * +elements+ - The new default elements
- #
- def defaults=(elements)
- @__defaults = Bag.new(elements)
-
- # When defaults change, we need to recompute parameters hierarchy
- __compute_all()
+ def initialize(config, options = {})
+ compute_bags!(config, options)
end
- ##
- # Recompute the correct precedences by merging the various
- # bag in the right order and return the result as a Nugrant::Bag
- # object.
- #
- def __compute_all()
- @__all = Bag.new()
- @__all.__merge!(@__defaults)
- @__all.__merge!(@__system)
- @__all.__merge!(@__user)
- @__all.__merge!(@__current)
- end
-
- def __to_hash()
- @__all.__to_hash()
- end
+ include Mixin::Parameters
end
end