Sha256: 21ccdeaa35b26d12480c932e8785c9ff25e810e5d333007699ceb01f07e0fa45

Contents?: true

Size: 1.17 KB

Versions: 18

Compression:

Stored size: 1.17 KB

Contents

module PagSeguro
  class Item
    include ActiveModel::Validations
    
    attr_accessor :id, :description, :amount, :quantity, :shipping_cost, :weight
        
    validates_presence_of :id, :description, :amount, :quantity
    validates_format_of :amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
    validates_format_of :shipping_cost, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
    validates_format_of :weight, with: /^\d+$/, message: " must be an integer"
    validate :quantity_amount
    
    def initialize(attributes = {})
      @id = attributes[:id]
      @description = attributes[:description]
      @amount = attributes[:amount]
      @quantity = attributes[:quantity]
      @shipping_cost = attributes[:shipping_cost]
      @weight = attributes[:weight]
    end
    
    def description
      @description.present? && @description.size > 100 ? @description[0..99] : @description
    end
    
    protected
      def quantity_amount
        errors.add(:quantity, " must be a number between 1 and 999") if @quantity.present? && (@quantity == "0" || @quantity.to_s !~ /^\d{1,3}$/)
      end
    end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
pag_seguro-0.4.1 lib/pag_seguro/item.rb
pag_seguro-0.4.0 lib/pag_seguro/item.rb
pag_seguro-0.3.2 lib/pag_seguro/item.rb
pag_seguro-0.3.1 lib/pag_seguro/item.rb
pag_seguro-0.3.0 lib/pag_seguro/item.rb
pag_seguro-0.2.3 lib/pag_seguro/item.rb
pag_seguro-0.2.2 lib/pag_seguro/item.rb
pag_seguro-0.2.1 lib/pag_seguro/item.rb
pag_seguro-0.2.0 lib/pag_seguro/item.rb
pag_seguro-0.1.9 lib/pag_seguro/item.rb
pag_seguro-0.1.8 lib/pag_seguro/item.rb
pag_seguro-0.1.7 lib/pag_seguro/item.rb
pag_seguro-0.1.6 lib/pag_seguro/item.rb
pag_seguro-0.1.4 lib/pag_seguro/item.rb
pag_seguro-0.1.3 lib/pag_seguro/item.rb
pag_seguro-0.1.2 lib/pag_seguro/item.rb
pag_seguro-0.1.1 lib/pag_seguro/item.rb
pag_seguro-0.1.0 lib/pag_seguro/item.rb