Sha256: 126837e93fd46416717529e6aaf459c1c91108189262473c9bf23b663454ad84

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

module KingFormat
  module MoneyFields

    def self.included(base)
      base.send :class_inheritable_accessor, :money_fields
      base.money_fields = []
      base.extend(ClassMethods)
    end

    module ClassMethods
      #  Defines the fields returned by self.money_fields.
      # ===== Parameter
      # fieldnames<Array[Symbol]>:: fieldnames/instance method names as symbols
      # ==== Example
      # class Invoice
      #   has_money_fields :total, :amout, :calculated_tax
      #
      def has_money_fields(*fieldnames)
        self.money_fields = fieldnames
        include InstanceMethods
      end

      # Check if a given field is declared as money field
      # ==== Parameter
      # fieldname<String>:: The fieldname to check. Is be casted into a symbol.
      def is_money_field?(fieldname)
        self.money_fields.include?(fieldname.to_sym)
      end

    end #ClassMethods

    module InstanceMethods
      # Check if a given field is declared as money field
      # ==== Parameter
      # fieldname<String>:: The fieldname to check. Is casted into a symbol.
      def is_money_field?(fieldname)
        self.class.money_fields.include?(fieldname.to_sym)
      end
    end
    
  end #Fields
end#KingFormat

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
king_views-1.1.0 king_format/lib/model_mixins/has_money_fields.rb
king_views-1.0.5 king_format/lib/model_mixins/has_money_fields.rb
king_views-1.0.4 king_format/lib/model_mixins/has_money_fields.rb
king_views-1.0.3 king_format/lib/model_mixins/has_money_fields.rb
king_views-1.0.2 king_format/lib/model_mixins/has_money_fields.rb
king_views-1.0.1 king_format/lib/model_mixins/has_money_fields.rb
king_views-1.0.0 king_format/lib/model_mixins/has_money_fields.rb