Class: Corraios::Containers::Base
- Inherits:
-
Object
- Object
- Corraios::Containers::Base
- Defined in:
- lib/corraios/containers/base.rb
Class Method Summary collapse
- .add_attribute(name, restrictions, query_attribute_name = nil, round_value = true) ⇒ Object
- .attributes_restrictions ⇒ Object
- .floor_for(attribute, value) ⇒ Object
- .minimum_value_for(attribute) ⇒ Object
- .query_attribute_name(attribute_name) ⇒ Object
- .query_attribute_names ⇒ Object
- .set_format(format) ⇒ Object
- .valid_field?(attribute, value) ⇒ Boolean
- .valid_fields?(attributes) ⇒ Boolean
Instance Method Summary collapse
- #assert_minimum_measures! ⇒ Object
- #attributes_names ⇒ Object
- #format ⇒ Object
-
#initialize(attributes) ⇒ Base
constructor
A new instance of Base.
- #is_a?(type) ⇒ Boolean
- #to_package ⇒ Object
- #to_query_params ⇒ Object
- #valid? ⇒ Boolean
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_restrictions ⇒ Object
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_names ⇒ Object
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
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
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_names ⇒ Object
31 32 33 |
# File 'lib/corraios/containers/base.rb', line 31 def attributes_names self.class.instance_variable_get('@attributes_restrictions').keys end |
#format ⇒ Object
27 28 29 |
# File 'lib/corraios/containers/base.rb', line 27 def format self.class.instance_variable_get('@format') end |
#is_a?(type) ⇒ 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_package ⇒ Object
17 18 19 |
# File 'lib/corraios/containers/base.rb', line 17 def to_package raise NotImplementedError end |
#to_query_params ⇒ Object
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
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 |