Class: NFE::RPSField
- Inherits:
-
Object
- Object
- NFE::RPSField
- Defined in:
- lib/rps_field.rb
Defined Under Namespace
Modules: Type
Instance Attribute Summary collapse
-
#auto_fill ⇒ Object
readonly
Returns the value of attribute auto_fill.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #alphanumeric? ⇒ Boolean
- #check_value ⇒ Object
- #default_char ⇒ Object
- #email? ⇒ Boolean
- #fill! ⇒ Object
-
#initialize(name, value = "") ⇒ RPSField
constructor
A new instance of RPSField.
- #length ⇒ Object
- #numeric? ⇒ Boolean
- #set_configs ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(name, value = "") ⇒ RPSField
Returns a new instance of RPSField.
17 18 19 20 21 22 23 24 25 |
# File 'lib/rps_field.rb', line 17 def initialize name, value = "" @name = name @value = value @auto_fill = true validate! @name = @name.to_sym @value.upcase! set_configs end |
Instance Attribute Details
#auto_fill ⇒ Object (readonly)
Returns the value of attribute auto_fill.
3 4 5 |
# File 'lib/rps_field.rb', line 3 def auto_fill @auto_fill end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/rps_field.rb', line 3 def name @name end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
3 4 5 |
# File 'lib/rps_field.rb', line 3 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/rps_field.rb', line 3 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
3 4 5 |
# File 'lib/rps_field.rb', line 3 def value @value end |
Instance Method Details
#alphanumeric? ⇒ Boolean
166 167 168 |
# File 'lib/rps_field.rb', line 166 def alphanumeric? return @value.match(/^[[:alnum:]\s]+$/) != nil end |
#check_value ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rps_field.rb', line 139 def check_value case @name when :layout_version if !["001", "002"].include? @value raise Errors::LayoutVersionError, /Only 001 and 002 versions supported/ end when :rps_type if !@value.strip.eql?("RPS") and !@value.strip.eql?("RPS-M") raise Errors::RPSTypeError, /RPS types allowed: RPS and RPS-M/ end when :rps_status if !["T", "F", "A", "B", "M", "N", "X", "V", "P", "C"].include? @value raise Errors::RPSStatusError, /Invalid RPS situation\/status. Please, check the manual (section 4.3.6)/ end when :iss_by if !["1", "2", "3"].include? @value raise Errors::ISSByError, /Invalid ISS. Please, check the manual (section 4.3.11)/ end when :taker_type if !["1", "2", "3"].include? @value raise Errors::TakerTypeError, /Invalid Taker type. Please, check the manual (section 4.3.12)/ end when :start_date, :end_date, :issuing_date Date.strptime(@value, "%Y%m%d") end end |
#default_char ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/rps_field.rb', line 112 def default_char case @type when Type::NUM return "0" else return " " end end |
#email? ⇒ Boolean
174 175 176 |
# File 'lib/rps_field.rb', line 174 def email? return @value.match(/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i) != nil end |
#fill! ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/rps_field.rb', line 182 def fill! while self.length < @size do if self.default_char.eql? "0" @value = self.default_char + @value else @value << self.default_char end end end |
#length ⇒ Object
178 179 180 |
# File 'lib/rps_field.rb', line 178 def length return @value.length end |
#numeric? ⇒ Boolean
170 171 172 |
# File 'lib/rps_field.rb', line 170 def numeric? return @value.match(/^[0-9]+$/) != nil end |
#set_configs ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rps_field.rb', line 27 def set_configs case @name when :taker_email @size = 75 @type = Type::EMAIL when :aliquot @size = 4 @type = Type::NUM when :total_detail_lines @size = 7 @type = Type::NUM when :taker_document @size = 14 @type = Type::NUM when :amount, :tax_amount, :total_amount, :total_tax_amount, :pis_pasep, :cofins, :inss, :ir, :cssl @size = 15 @type = Type::NUM when :rps_number, :matriculation @size = 12 @type = Type::NUM when :taker_ccm @size = 8 @type = Type::NUM when :private_blank_field @size = 200 @type = Type::ALPHA when :taker_name @size = 75 @type = Type::ALPHA when :address_type @size = 3 @type = Type::ALPHA when :address, :city @size = 50 @type = Type::ALPHA when :address_number, :tributary_source @size = 10 @type = Type::ALPHA when :complement, :district @size = 30 @type = Type::ALPHA when :service_description @size = @value.length @type = Type::ALPHA when :rps_type, :rps_serial @size = 5 @type = Type::ALPHA when :layout_version @size = 3 @type = Type::NUM @auto_fill = false when :start_date, :end_date, :issuing_date, :municipal_registration @size = 8 @type = Type::NUM @auto_fill = false when :zip_code @size = 8 @type = Type::NUM when :state_registration, :cei @size = 12 @type = Type::NUM when :service_code @size = 5 @type = Type::NUM @auto_fill = false when :tributary_percentage @size = 5 @type = Type::NUM when :iss_by, :taker_type @size = 1 @type = Type::NUM @auto_fill = false when :city_ibge_code @size = 7 @type = Type::NUM when :rps_status @size = 1 @type = Type::ALPHA @auto_fill = false when :uf @size = 2 @type = Type::ALPHA end end |
#valid? ⇒ Boolean
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rps_field.rb', line 121 def valid? if @value.empty? valid = true else case @type when Type::ALPHA valid = self.alphanumeric? when Type::EMAIL valid = self.email? when Type::NUM valid = self.numeric? end end self.fill! if @auto_fill == true return (valid and self.length == @size) end |
#validate! ⇒ Object
11 12 13 14 15 |
# File 'lib/rps_field.rb', line 11 def validate! raise Errors::InvalidParamError, /Parameter name must be String or Symbol/ if !@name .is_a? String and !@name.is_a? Symbol raise Errors::InvalidParamError, /Parameter value must be String/ if !@value.is_a? String raise Errors::InvalidParamError if @name.empty? end |