Class: Corraios::Containers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/corraios/containers/base.rb

Direct Known Subclasses

Envelope, Package, Roll

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
# File 'lib/corraios/containers/base.rb', line 7

def initialize(attributes)
  attributes.each do |attr, value|
    send "#{attr}=", value.to_f
  end
end

Class Method Details

.add_attribute(name, restrictions, query_attribute_name = nil, round_value = true) ⇒ Object



49
50
51
52
53
# 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_restrictionsObject



63
64
65
# File 'lib/corraios/containers/base.rb', line 63

def attributes_restrictions
  @attributes_restrictions ||= {}
end

.floor_for(attribute, value) ⇒ Object



76
77
78
79
80
# 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) ⇒ Object



82
83
84
# File 'lib/corraios/containers/base.rb', line 82

def minimum_value_for(attribute)
  attributes_restrictions[attribute].first
end

.query_attribute_name(attribute_name) ⇒ Object



86
87
88
# File 'lib/corraios/containers/base.rb', line 86

def query_attribute_name(attribute_name)
  query_attribute_names[attribute_name]
end

.query_attribute_namesObject



67
68
69
# File 'lib/corraios/containers/base.rb', line 67

def query_attribute_names
  @query_attribute_names ||= {}
end

.set_format(format) ⇒ Object



55
56
57
# File 'lib/corraios/containers/base.rb', line 55

def set_format(format)
  @format = format
end

.valid_field?(attribute, value) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# 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) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# 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

Instance Method Details

#assert_minimum_measures!Object



41
42
43
44
45
# 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_namesObject



31
32
33
# File 'lib/corraios/containers/base.rb', line 31

def attributes_names
  self.class.instance_variable_get('@attributes_restrictions').keys
end

#formatObject



27
28
29
# File 'lib/corraios/containers/base.rb', line 27

def format
  self.class.instance_variable_get('@format')
end

#is_a?(type) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/corraios/containers/base.rb', line 13

def is_a?(type)
  self.class.name.split('::').last.downcase.to_sym == type
end

#to_packageObject

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/corraios/containers/base.rb', line 17

def to_package
  raise NotImplementedError
end

#to_query_paramsObject



21
22
23
24
25
# 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?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/corraios/containers/base.rb', line 35

def valid?
  attributes_names.all? do |attr|
    self.class.valid_field?(attr, send(attr))
  end
end