Sha256: d99d3fba44ca2336f349cb9dd3c2108b8706bfb7c9379bdd2358ad39698fb76f

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

motion_require 'number_row'

module Formotion
  module RowType
    class CurrencyRow < NumberRow
      def on_change(text_field)
        break_with_semaphore do
          edited_text = text_field.text
          entered_digits = edited_text.gsub %r{\D+}, ''
          decimal_num = 0.0

          if !entered_digits.empty?
            decimal_num = entered_digits.to_i * (10 ** currency_scale.to_i)
            decimal_num = decimal_num.to_f
          end

          row.value = decimal_num
          text_field.text = row_value
        end
      end

      def row_value
        number_formatter.stringFromNumber super.to_f
      end

      def value_for_save_hash
        number_formatter.numberFromString(row_value)
      end

      private
      def number_formatter
        @number_formatter ||= begin
          formatter = NSNumberFormatter.alloc.init
          formatter.numberStyle = NSNumberFormatterCurrencyStyle
          formatter.locale = NSLocale.currentLocale
          formatter
        end
      end

      def currency_scale
        @currency_scale ||= number_formatter.maximumFractionDigits * -1
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
formotion-1.8 lib/formotion/row_type/currency_row.rb
formotion-1.7 lib/formotion/row_type/currency_row.rb
formotion-1.6 lib/formotion/row_type/currency_row.rb
formotion-1.5.1 lib/formotion/row_type/currency_row.rb
formotion-1.5.0 lib/formotion/row_type/currency_row.rb
formotion-1.4.0 lib/formotion/row_type/currency_row.rb
formotion-1.3.1 lib/formotion/row_type/currency_row.rb