Class: PdfFill::HashConverter
- Inherits:
-
Object
- Object
- PdfFill::HashConverter
- Defined in:
- lib/pdf_fill/hash_converter.rb
Constant Summary collapse
- ITERATOR =
'%iterator%'
- EXTRAS_TEXT =
"See add'l info page"
Instance Attribute Summary collapse
-
#extras_generator ⇒ Object
readonly
Returns the value of attribute extras_generator.
Instance Method Summary collapse
- #add_array_to_extras(arr, pdftk_keys) ⇒ Object
- #add_to_extras(key_data, v, i) ⇒ Object
- #check_for_overflow(arr, pdftk_keys) ⇒ Object
- #convert_val_as_date(v) ⇒ Object
- #convert_val_as_string(v) ⇒ Object
- #convert_value(v, key_data, is_overflow = false) ⇒ Object
-
#initialize(date_strftime) ⇒ HashConverter
constructor
A new instance of HashConverter.
- #overflow?(key_data, value, from_array_overflow = false) ⇒ Boolean
- #set_value(v, key_data, i, from_array_overflow = false) ⇒ Object
- #transform_array(form_data, pdftk_keys) ⇒ Object
- #transform_data(form_data:, pdftk_keys:, i: nil, from_array_overflow: false) ⇒ Object
Constructor Details
#initialize(date_strftime) ⇒ HashConverter
Returns a new instance of HashConverter.
14 15 16 17 18 |
# File 'lib/pdf_fill/hash_converter.rb', line 14 def initialize(date_strftime) @pdftk_form = {} @date_strftime = date_strftime @extras_generator = ExtrasGenerator.new end |
Instance Attribute Details
#extras_generator ⇒ Object (readonly)
Returns the value of attribute extras_generator.
12 13 14 |
# File 'lib/pdf_fill/hash_converter.rb', line 12 def extras_generator @extras_generator end |
Instance Method Details
#add_array_to_extras(arr, pdftk_keys) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pdf_fill/hash_converter.rb', line 81 def add_array_to_extras(arr, pdftk_keys) arr.each_with_index do |v, i| i = nil if pdftk_keys[:always_overflow] if v.is_a?(Hash) v.each do |key, val| add_to_extras(pdftk_keys[key], convert_value(val, pdftk_keys[key], true), i) end else add_to_extras(pdftk_keys, convert_value(v, pdftk_keys, true), i) end end end |
#add_to_extras(key_data, v, i) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pdf_fill/hash_converter.rb', line 66 def add_to_extras(key_data, v, i) return if v.blank? return if key_data.try(:[], :question_text).blank? i = nil if key_data[:skip_index] v = "$#{v}" if key_data[:dollar] v = v.extras_value if v.is_a?(PdfFill::FormValue) @extras_generator.add_text( v, key_data.slice(:question_num, :question_suffix, :question_text).merge( i: ) ) end |
#check_for_overflow(arr, pdftk_keys) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pdf_fill/hash_converter.rb', line 111 def check_for_overflow(arr, pdftk_keys) return true if pdftk_keys[:always_overflow] return true if arr.size > (pdftk_keys[:limit] || 0) arr.each do |item| next if item.blank? || !item.is_a?(Hash) item.each do |k, v| return true if overflow?(pdftk_keys[k], v) end end false end |
#convert_val_as_date(v) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pdf_fill/hash_converter.rb', line 43 def convert_val_as_date(v) v = v.to_s date_split = v.split('-') date_args = Array.new(3) { |i| date_split[i].to_i } if Date.valid_date?(*date_args) Date.new(*date_args).strftime(@date_strftime) else convert_val_as_string(v) end end |
#convert_val_as_string(v) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pdf_fill/hash_converter.rb', line 30 def convert_val_as_string(v) case v when Array return v.map do |item| convert_val_as_string(item) end.join(', ') when PdfFill::FormValue return v end v.to_s end |
#convert_value(v, key_data, is_overflow = false) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/pdf_fill/hash_converter.rb', line 20 def convert_value(v, key_data, is_overflow = false) if [true, false].include?(v) && !is_overflow v ? 1 : 0 elsif key_data.try(:[], :format) == 'date' convert_val_as_date(v) else convert_val_as_string(v) end end |
#overflow?(key_data, value, from_array_overflow = false) ⇒ Boolean
56 57 58 59 60 61 62 63 64 |
# File 'lib/pdf_fill/hash_converter.rb', line 56 def overflow?(key_data, value, from_array_overflow = false) return false if value.blank? || from_array_overflow value = value.to_s if value.is_a?(Numeric) limit = key_data.try(:[], :limit) limit.present? && value.size > limit end |
#set_value(v, key_data, i, from_array_overflow = false) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pdf_fill/hash_converter.rb', line 94 def set_value(v, key_data, i, from_array_overflow = false) k = key_data[:key] return if k.blank? k = k.gsub(ITERATOR, i.to_s) unless i.nil? new_value = convert_value(v, key_data) if overflow?(key_data, new_value, from_array_overflow) add_to_extras(key_data, new_value, i) new_value = EXTRAS_TEXT end @pdftk_form[k] = new_value end |
#transform_array(form_data, pdftk_keys) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/pdf_fill/hash_converter.rb', line 126 def transform_array(form_data, pdftk_keys) has_overflow = check_for_overflow(form_data, pdftk_keys) if has_overflow first_key = pdftk_keys[:first_key] transform_data( form_data: { first_key => EXTRAS_TEXT }, pdftk_keys:, i: 0, from_array_overflow: true ) add_array_to_extras(form_data, pdftk_keys) else form_data.each_with_index do |v, idx| transform_data(form_data: v, pdftk_keys:, i: idx) end end end |
#transform_data(form_data:, pdftk_keys:, i: nil, from_array_overflow: false) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/pdf_fill/hash_converter.rb', line 147 def transform_data(form_data:, pdftk_keys:, i: nil, from_array_overflow: false) return if form_data.nil? || pdftk_keys.nil? case form_data when Array transform_array(form_data, pdftk_keys) when Hash form_data.each do |k, v| transform_data( form_data: v, pdftk_keys: pdftk_keys[k], i:, from_array_overflow: ) end else set_value(form_data, pdftk_keys, i, from_array_overflow) end @pdftk_form end |