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
- #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 end end |
Class Method Details
.add_attribute(name, restrictions, query_attribute_name = nil, round_value = true) ⇒ Object
43 44 45 46 47 |
# File 'lib/corraios/containers/base.rb', line 43 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
57 58 59 |
# File 'lib/corraios/containers/base.rb', line 57 def attributes_restrictions @attributes_restrictions ||= {} end |
.floor_for(attribute, value) ⇒ Object
70 71 72 73 74 |
# File 'lib/corraios/containers/base.rb', line 70 def floor_for(attribute, value) minimum_value = minimum_value_for(attribute) minimum_value < value ? value : minimum_value end |
.minimum_value_for(attribute) ⇒ Object
76 77 78 |
# File 'lib/corraios/containers/base.rb', line 76 def minimum_value_for(attribute) attributes_restrictions[attribute].first end |
.query_attribute_name(attribute_name) ⇒ Object
80 81 82 |
# File 'lib/corraios/containers/base.rb', line 80 def query_attribute_name(attribute_name) query_attribute_names[attribute_name] end |
.query_attribute_names ⇒ Object
61 62 63 |
# File 'lib/corraios/containers/base.rb', line 61 def query_attribute_names @query_attribute_names ||= {} end |
.set_format(format) ⇒ Object
49 50 51 |
# File 'lib/corraios/containers/base.rb', line 49 def set_format(format) @format = format end |
.valid_field?(attribute, value) ⇒ Boolean
53 54 55 |
# File 'lib/corraios/containers/base.rb', line 53 def valid_field?(attribute, value) attributes_restrictions[attribute] && attributes_restrictions[attribute].include?(value) end |
.valid_fields?(attributes) ⇒ Boolean
65 66 67 68 |
# File 'lib/corraios/containers/base.rb', line 65 def valid_fields?(attributes) attributes.keys.size == attributes_restrictions.keys.size && attributes.all? { |attribute, value| valid_field?(attribute, value) } end |
Instance Method Details
#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 |