Class: NFE::Register
- Inherits:
-
Object
- Object
- NFE::Register
- Defined in:
- lib/register.rb
Constant Summary collapse
- VALID_TYPES =
[1, 6, 9]
- REQUIRED_FIELDS =
[]
- VALID_FIELDS =
[]
- DEFAULTS =
{}
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#required_fields ⇒ Object
readonly
Returns the value of attribute required_fields.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #<<(fields) ⇒ Object
-
#initialize(type) ⇒ Register
constructor
A new instance of Register.
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #valid_register_field?(name) ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(type) ⇒ Register
Returns a new instance of Register.
20 21 22 23 24 25 |
# File 'lib/register.rb', line 20 def initialize type @type = type @fields = Array.new @required_fields = Array.new(self.class::REQUIRED_FIELDS) validate! end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
8 9 10 |
# File 'lib/register.rb', line 8 def fields @fields end |
#required_fields ⇒ Object (readonly)
Returns the value of attribute required_fields.
8 9 10 |
# File 'lib/register.rb', line 8 def required_fields @required_fields end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/register.rb', line 8 def type @type end |
Instance Method Details
#<<(fields) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/register.rb', line 36 def << fields raise Errors::InvalidParamError, /Expecting Hash parameter/ if !fields.is_a? Hash fields = self.class::DEFAULTS.merge(fields) fields.each do |name, value| if valid_register_field?(name) field = RPSField.new name, value if field.valid? field.check_value @fields << field else raise Errors::InvalidFieldError, /The field #{field.name}; Value: #{field.value} is invalid. Check its value/ end else raise Errors::NonExistentFieldError, /Register: #{self.class}; Name: #{name}; Value: #{value}/ end end return @fields end |
#to_hash ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/register.rb', line 72 def to_hash fields_hash = Hash.new @fields.each do |field| fields_hash[field.name] = field.value end return fields_hash end |
#to_s ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/register.rb', line 81 def to_s string = @type.to_s self.class::VALID_FIELDS.each do |field_name| string += self.to_hash[field_name] end return "#{string}\n" end |
#valid? ⇒ Boolean
27 28 29 30 |
# File 'lib/register.rb', line 27 def valid? set_non_filled return (@required_fields - self.to_hash.keys).empty? end |
#valid_register_field?(name) ⇒ Boolean
32 33 34 |
# File 'lib/register.rb', line 32 def valid_register_field? name return (self.class::VALID_FIELDS).include?(name) end |
#validate! ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/register.rb', line 10 def validate! if !@type.is_a? String and !@type.is_a? Integer raise Errors::InvalidParamError, /Parameter type must be String or Integer/ end if !VALID_TYPES.include?(@type.to_i) raise Errors::RPSRegisterTypeError, /Invalid RPS Register type/ end end |