<commit_before># -*- encoding : utf-8 -*-
# Top Level Module of the the gem.
# @author <NAME>
# @version 0.9
# @since 0.1

module Exchange
  # @author <NAME>
  # Money Objects instantiated from the money class can be used for basic mathematical operations and currency conversions
  # @version 0.1
  # @since 0.1
  #
  class Money
    include Comparable
    
    # @return [BigDecimal] number The number the money object has been instantiated from
    #
    attr_accessor :value
    
    # @return [Symbol, String] currency the currency of the money object
    #
    attr_accessor :currency
    
    # @return [Time] The time at which the conversion has taken place or should take place if the object is involved in operations
    #
    attr_accessor :time
    
    # @attr_reader
    # @return [Exchange::Money] The original money object this money object was converted from
    #
    attr_reader :from
    
    # @attr_reader
    # @return [Exchange::ExternalAPI] The current api subclass
    attr_reader :api
    
    # Intialize the currency with a number and a currency
    # @param [Integer, Float] value The number the currency is instantiated from
    # @param [Symbol] currency_arg The currency the money object is in as a downcased symbol
    # @param [Hash] opts Optional Parameters for instantiation
    # @option opts [Time] :at The time at which conversion took place
    # @option opts [String,Symbol] :from The money object this money object was converted from
    # @version 0.9
    #
    # @example Instantiate a money object of 40 US Dollars
    #   Exchange::Money.new(40, :usd) 
    #     #=> #<Exchange::Money @number=40.0 @currency=:usd @time=#<Time>>
    # @example Instantiate a money object of 40 US Dollars and convert it to Euro. It shows the conversion date and the original currency
    #   Exchange::Money.new(40, :usd).to(:eur, :at => Time.gm(2012,9,1)) 
    #     #=> #<Exchange::Money @number=37.0 @currency=:usd @time=#<Time> @from=#<Exchange::Money @number=40.0 @currency=:usd>>
    #
    def initialize value, currency_arg=nil, opts={}, &block
      currency_arg      = ISO.assert_currency!(currency_arg) if currency_arg
      
      @from             = opts[:from]
      @api              = Exchange.configuration.api.subclass
      
      yield(self) if block_given?
      
      self.time             = Helper.assure_time(time || opts[:at], :default => :now)
      self.value            = ISO.instantiate(value, currency || currency_arg)
      self.currency         = currency || currency_arg
    end
    
    # Method missing is used to handle conversions from one money object to another. It only handles currencies which are available in
    # the API class set in the configuration.
    # @example convert to chf
    #   Exchange::Money.new(40,:usd).to(:chf)    
    # @example convert to sek at a given time
    #   Exchange::Money.new(40,:nok).to(:sek, :at => Time.gm(2012,2,2))
    #
    def method_missing method, *args, &block
      value.send method, *args, &block
    end
    
    # Converts this instance of currency into another currency
    # @return [Exchange::Money] An instance of Exchange::Money with the converted number and the converted currency
    # @param [Symbol, String] other The currency to convert the number to
    # @param [Hash] options An options hash
    # @option [Time] :at The timestamp of the rate the conversion took place in
    # @example convert to 'chf'
    #   Exchange::Money.new(40,:usd).to(:chf)
    # @example convert to 'sek' at a specific rate
    #   Exchange::Money.new(40,:nok).to(:sek, :at => Time.gm(2012,2,2))
    #
    def to other, options={}
      other = ISO.assert_currency!(other)
      
      if api_supports_currency?(other)
        opts = { :at => time, :from => self }.merge(options)
        Money.new(api.new.convert(value, currency, other, opts), other, opts)
      elsif fallback!
        to other, options
      else
        raise_no_rate_error(other)<commit_msg>fix checking of supported currency<commit_after># -*- encoding : utf-8 -*-
# Top Level Module of the the gem.
# @author <NAME>
# @version 0.9
# @since 0.1

module Exchange
  # @author <NAME>
  # Money Objects instantiated from the money class can be used for basic mathematical operations and currency conversions
  # @version 0.1
  # @since 0.1
  #
  class Money
    include Comparable
    
    # @return [BigDecimal] number The number the money object has been instantiated from
    #
    attr_accessor :value
    
    # @return [Symbol, String] currency the currency of the money object
    #
    attr_accessor :currency
    
    # @return [Time] The time at which the conversion has taken place or should take place if the object is involved in operations
    #
    attr_accessor :time
    
    # @attr_reader
    # @return [Exchange::Money] The original money object this money object was converted from
    #
    attr_reader :from
    
    # @attr_reader
    # @return [Exchange::ExternalAPI] The current api subclass
    attr_reader :api
    
    # Intialize the currency with a number and a currency
    # @param [Integer, Float] value The number the currency is instantiated from
    # @param [Symbol] currency_arg The currency the money object is in as a downcased symbol
    # @param [Hash] opts Optional Parameters for instantiation
    # @option opts [Time] :at The time at which conversion took place
    # @option opts [String,Symbol] :from The money object this money object was converted from
    # @version 0.9
    #
    # @example Instantiate a money object of 40 US Dollars
    #   Exchange::Money.new(40, :usd) 
    #     #=> #<Exchange::Money @number=40.0 @currency=:usd @time=#<Time>>
    # @example Instantiate a money object of 40 US Dollars and convert it to Euro. It shows the conversion date and the original currency
    #   Exchange::Money.new(40, :usd).to(:eur, :at => Time.gm(2012,9,1)) 
    #     #=> #<Exchange::Money @number=37.0 @currency=:usd @time=#<Time> @from=#<Exchange::Money @number=40.0 @currency=:usd>>
    #
    def initialize value, currency_arg=nil, opts={}, &block
      currency_arg      = ISO.assert_currency!(currency_arg) if currency_arg
      
      @from             = opts[:from]
      @api              = Exchange.configuration.api.subclass
      
      yield(self) if block_given?
      
      self.time             = Helper.assure_time(time || opts[:at], :default => :now)
      self.value            = ISO.instantiate(value, currency || currency_arg)
      self.currency         = currency || currency_arg
    end
    
    # Method missing is used to handle conversions from one money object to another. It only handles currencies which are available in
    # the API class set in the configuration.
    # @example convert to chf
    #   Exchange::Money.new(40,:usd).to(:chf)    
    # @example convert to sek at a given time
    #   Exchange::Money.new(40,:nok).to(:sek, :at => Time.gm(2012,2,2))
    #
    def method_missing method, *args, &block
      value.send method, *args, &block
    end
    
    # Converts this instance of currency into another currency
    # @return [Exchange::Money] An instance of Exchange::Money with the converted number and the converted currency
    # @param [Symbol, String] other The currency to convert the number to
    # @param [Hash] options An options hash
    # @option [Time] :at The timestamp of the rate the conversion took place in
    # @example convert to 'chf'
    #   Exchange::Money.new(40,:usd).to(:chf)
    # @example convert to 'sek' at a specific rate
    #   Exchange::Money.new(40,:nok).to(:sek, :at => Time.gm(2012,2,2))
    #
    def to other, options={}
      other = ISO.assert_currency!(other)
      
      if api_supports_currency?(other) && api_supports_currency?(currency)
        opts = { :at => time, :from => self }.merge(options)
        Money.new(api.new.convert(value, currency, other, opts), other, opts)
      elsif fallback!
        to other, options
      else
        raise_no_rate_error(other)
{'fraction_non_alphanumeric': 0.10083718605522929, 'fraction_numerical': 0.013494939397725853, 'mean_word_length': 2.902486591906387, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7281133', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2304, 'n_words': 832}