class Corraios::Containers::Base

Public Class Methods

add_attribute(name, restrictions, query_attribute_name = nil, round_value = true) click to toggle source
# File lib/corraios/containers/base.rb, line 49
def add_attribute name, restrictions, query_attribute_name = nil, round_value = true
  attributes_restrictions[name] = restrictions
  query_attribute_names[name] = query_attribute_name
  attr_accessor name
end
attributes_restrictions() click to toggle source
# File lib/corraios/containers/base.rb, line 63
def attributes_restrictions
  @attributes_restrictions ||= {}
end
floor_for(attribute, value) click to toggle source
# File lib/corraios/containers/base.rb, line 76
def floor_for(attribute, value)
  minimum_value = minimum_value_for(attribute)

  minimum_value < value ? value : minimum_value
end
minimum_value_for(attribute) click to toggle source
# File lib/corraios/containers/base.rb, line 82
def minimum_value_for(attribute)
  attributes_restrictions[attribute].first
end
new(attributes) click to toggle source
# File lib/corraios/containers/base.rb, line 7
def initialize(attributes)
  attributes.each do |attr, value|
    send "#{attr}=", value.to_f
  end
end
query_attribute_name(attribute_name) click to toggle source
# File lib/corraios/containers/base.rb, line 86
def query_attribute_name(attribute_name)
  query_attribute_names[attribute_name]
end
query_attribute_names() click to toggle source
# File lib/corraios/containers/base.rb, line 67
def query_attribute_names
  @query_attribute_names ||= {}
end
set_format(format) click to toggle source
# File lib/corraios/containers/base.rb, line 55
def set_format(format)
  @format = format
end
valid_field?(attribute, value) click to toggle source
# File lib/corraios/containers/base.rb, line 59
def valid_field?(attribute, value)
  attributes_restrictions[attribute] && attributes_restrictions[attribute].include?(value)
end
valid_fields?(attributes) click to toggle source
# File lib/corraios/containers/base.rb, line 71
def valid_fields?(attributes)
  attributes.keys.size == attributes_restrictions.keys.size &&
    attributes.all? { |attribute, value| valid_field?(attribute, value) }
end

Public Instance Methods

assert_minimum_measures!() click to toggle source
# File lib/corraios/containers/base.rb, line 41
def assert_minimum_measures!
  attributes_names.each do |attr|
    send("#{attr}=", self.class.floor_for(attr, send(attr)))
  end
end
attributes_names() click to toggle source
# File lib/corraios/containers/base.rb, line 31
def attributes_names
  self.class.instance_variable_get('@attributes_restrictions').keys
end
format() click to toggle source
# File lib/corraios/containers/base.rb, line 27
def format
  self.class.instance_variable_get('@format')
end
is_a?(type) click to toggle source
# File lib/corraios/containers/base.rb, line 13
def is_a?(type)
  self.class.name.split('::').last.downcase.to_sym == type
end
to_package() click to toggle source
# File lib/corraios/containers/base.rb, line 17
def to_package
  raise NotImplementedError
end
to_query_params() click to toggle source
# File lib/corraios/containers/base.rb, line 21
def to_query_params
  attributes_names.map do |attr|
    "#{self.class.query_attribute_name(attr)}=#{send(attr).round(2).to_s.gsub('.', ',')}"
  end.join('&') + "&nCdFormato=#{format}"
end
valid?() click to toggle source
# File lib/corraios/containers/base.rb, line 35
def valid?
  attributes_names.all? do |attr|
    self.class.valid_field?(attr, send(attr))
  end
end