Class: Hippo::Field
- Inherits:
-
Object
- Object
- Hippo::Field
- Defined in:
- lib/hippo/field.rb
Instance Attribute Summary collapse
-
#composite ⇒ Object
Returns the value of attribute composite.
-
#composite_sequence ⇒ Object
Returns the value of attribute composite_sequence.
-
#datatype ⇒ Object
Returns the value of attribute datatype.
-
#maximum ⇒ Object
Returns the value of attribute maximum.
-
#minimum ⇒ Object
Returns the value of attribute minimum.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#required ⇒ Object
Returns the value of attribute required.
-
#restrictions ⇒ Object
Returns the value of attribute restrictions.
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#sequence ⇒ Object
Returns the value of attribute sequence.
Instance Method Summary collapse
- #formatted_value(value) ⇒ Object
- #generate_date(value) ⇒ Object
- #generate_decimal(value) ⇒ Object
- #generate_string(value) ⇒ Object
- #generate_time(value) ⇒ Object
- #invalid!(message = "Invalid value specified for #{self.datatype} field.") ⇒ Object
- #parse_date(value) ⇒ Object
- #parse_decimal(value) ⇒ Object
- #parse_string(value) ⇒ Object
- #parse_time(value) ⇒ Object
- #string_value(value) ⇒ Object
Instance Attribute Details
#composite ⇒ Object
Returns the value of attribute composite.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def composite @composite end |
#composite_sequence ⇒ Object
Returns the value of attribute composite_sequence.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def composite_sequence @composite_sequence end |
#datatype ⇒ Object
Returns the value of attribute datatype.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def datatype @datatype end |
#maximum ⇒ Object
Returns the value of attribute maximum.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def maximum @maximum end |
#minimum ⇒ Object
Returns the value of attribute minimum.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def minimum @minimum end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def @options end |
#required ⇒ Object
Returns the value of attribute required.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def required @required end |
#restrictions ⇒ Object
Returns the value of attribute restrictions.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def restrictions @restrictions end |
#separator ⇒ Object
Returns the value of attribute separator.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def separator @separator end |
#sequence ⇒ Object
Returns the value of attribute sequence.
7 8 9 |
# File 'lib/hippo/field.rb', line 7 def sequence @sequence end |
Instance Method Details
#formatted_value(value) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/hippo/field.rb', line 15 def formatted_value(value) return nil if value.nil? case datatype when :binary then value when :integer then value.to_i when :decimal then parse_decimal(value) when :date then parse_date(value) when :time then parse_time(value) else parse_string(value) end end |
#generate_date(value) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/hippo/field.rb', line 105 def generate_date(value) value ||= Date.today if value.class.to_s == 'Range' "#{value.first.strftime('%Y%m%d')}-#{value.last.strftime('%Y%m%d')}" elsif maximum == 6 value.strftime('%y%m%d') else value.strftime('%Y%m%d') end end |
#generate_decimal(value) ⇒ Object
57 58 59 60 61 |
# File 'lib/hippo/field.rb', line 57 def generate_decimal(value) value ||= BigDecimal.new('0') value.to_s('F').sub(/\.0\z/,'').rjust(minimum, '0') end |
#generate_string(value) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/hippo/field.rb', line 41 def generate_string(value) if required value.to_s.ljust(minimum) else value.to_s end end |
#generate_time(value) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/hippo/field.rb', line 72 def generate_time(value) value ||= Time.now if maximum == 4 || value.sec == 0 value.strftime('%H%M') else value.strftime('%H%M%S') end end |
#invalid!(message = "Invalid value specified for #{self.datatype} field.") ⇒ Object
145 146 147 |
# File 'lib/hippo/field.rb', line 145 def invalid!( = "Invalid value specified for #{self.datatype} field.") raise Hippo::Exceptions::InvalidValue.new end |
#parse_date(value) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hippo/field.rb', line 117 def parse_date(value) if value == '' invalid! if required return nil end case value.class.to_s when "Range" then value when "Date" then value when "Time" then value.to_date when "String" format = case value when /\A\d{6}\z/ then '%y%m%d' when /\A\d{8}\z/ then '%Y%m%d' when /\A(\d{8})-(\d{8})\z/ then return Date.parse($1, '%Y%m%d')..Date.parse($2, '%Y%m%d') else invalid! end Date.parse(value, format) else invalid! "Invalid datatype(#{value.class}) for date field." end rescue invalid! end |
#parse_decimal(value) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/hippo/field.rb', line 63 def parse_decimal(value) if value == '' invalid! if required return nil end BigDecimal.new(value.to_s) end |
#parse_string(value) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/hippo/field.rb', line 49 def parse_string(value) if value.to_s.empty? && !required nil else value.to_s.strip end end |
#parse_time(value) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hippo/field.rb', line 82 def parse_time(value) if value == '' invalid! if required return nil end case value.class.to_s when 'Time' then value when 'String' format = case value when /\A\d{4}\z/ then '%H%M' when /\A\d{6}\z/ then '%H%M%S' when /\A\d{7,8}\z/ then '%H%M%S%N' else invalid! end Time.strptime(value, format) else invalid! end rescue invalid! end |
#string_value(value) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hippo/field.rb', line 28 def string_value(value) return '' if value.nil? && !required case datatype when :binary then value when :integer then value.to_s.rjust(minimum, '0') when :decimal then generate_decimal(value) when :date then generate_date(value) when :time then generate_time(value) else generate_string(value) end end |