lib/unit_measurements/base.rb in unit_measurements-5.2.0 vs lib/unit_measurements/base.rb in unit_measurements-5.3.0

- old
+ new

@@ -5,10 +5,17 @@ require "active_support/all" require "unit_measurements/version" module UnitMeasurements class << self + # Allows setting an instance of +Configuration+ containing values of desired + # configurable options. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 5.3.0 + attr_writer :configuration + # Creates a new unit group based on the provided +block+ of instructions. # # The +build+ method allows you to define and create a custom unit group with # units and their conversions. It takes a block of instructions as an argument, # which is evaluated by an instance of +UnitGroupBuilder+. @@ -38,14 +45,10 @@ # end # # cache "length.json" # end # - # @param block - # A block of instructions for defining units and their conversions within - # the unit group. - # # @yield [builder] # A block that defines the units to be added to the unit group. # The block takes a {UnitGroupBuilder} instance as a parameter. # # @yieldparam builder [UnitGroupBuilder] @@ -74,13 +77,61 @@ end @unit_group = builder.build end end + + # Returns an instance of +Configuration+ with the values of desired configurable + # options of +*unit_measurements*+. If instance is not present, it initializes + # a new instance of {Configuration}. + # + # @return [Configuration] An instance of +Configuration+. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 5.3.0 + def configuration + @configuration ||= Configuration.new + end + + # Reset the configuration to its default state. + # + # @example + # UnitMeasurements.reset + # + # @return [Configuration] A new +Configuration+ object. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 5.3.0 + def reset + @configuration = Configuration.new + end + + # Configures options of the +*UnitMeasurements*+ module using a block. It + # yields the current +Configuration+ instance for updating default values of + # options by new values specified within a block. + # + # @example + # UnitMeasurements.configure do |config| + # config.use_cache = false + # end + # + # @yield [configuration] The current +Configuration+ instance. + # + # @yieldparam [Configuration] configuration + # An instance of +Configuration+ with the new values of options. + # + # @yieldreturn [Configuration] The updated +Configuration+ instance. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 5.3.0 + def configure + yield configuration + end end end # The following requires load various components of the unit measurements library. +require "unit_measurements/configuration" require "unit_measurements/cache" require "unit_measurements/unit_group_builder" require "unit_measurements/unit" require "unit_measurements/unit_group" require "unit_measurements/arithmetic"