Module: Strata::RecordWriter::ClassMethods
- Defined in:
- lib/strata/record_writer.rb
Instance Method Summary collapse
-
#class_layout_rules ⇒ Object
TODO: remove this method or require config path to be specified OR..
- #define_attribute_accessors ⇒ Object
- #exposed_class_layout_rules ⇒ Object
- #filler_layout_rules ⇒ Object
- #from_s(string) ⇒ Object
- #matches_definition?(string) ⇒ Boolean
- #retrieve_field_value(string, field, rule) ⇒ Object
- #set_allowed_characters(chars) ⇒ Object
- #set_delimiter(delimiter) ⇒ Object
- #set_record_length(length) ⇒ Object
- #string_to_hash(string) ⇒ Object
- #template_options ⇒ Object
Instance Method Details
#class_layout_rules ⇒ Object
TODO: remove this method or require config path to be specified OR.. make it redundant by refactoring behind an add_rule method
114 115 116 117 118 119 |
# File 'lib/strata/record_writer.rb', line 114 def class_layout_rules file_name = "#{Absa::H2h::CONFIG_DIR}/#{self.name.split("::")[-2].underscore}.yml" record_type = self.name.split("::")[-1].underscore YAML.load(File.open(file_name))[record_type] end |
#define_attribute_accessors ⇒ Object
129 130 131 132 133 134 |
# File 'lib/strata/record_writer.rb', line 129 def define_attribute_accessors self.class_layout_rules.each do |k,v| (class << self; self; end).send :attr_accessor, k self.send :attr_accessor, k end end |
#exposed_class_layout_rules ⇒ Object
121 122 123 |
# File 'lib/strata/record_writer.rb', line 121 def exposed_class_layout_rules self.class_layout_rules.select {|key, rule| !(rule["expose"] == false && rule.has_key?("expose")) } end |
#filler_layout_rules ⇒ Object
125 126 127 |
# File 'lib/strata/record_writer.rb', line 125 def filler_layout_rules class_layout_rules.select {|key, rule| rule.has_key?("expose") && rule["expose"] == false && rule.has_key?("fixed_val")} end |
#from_s(string) ⇒ Object
191 192 193 194 |
# File 'lib/strata/record_writer.rb', line 191 def from_s(string) = self.string_to_hash(string) record = self.new() end |
#matches_definition?(string) ⇒ Boolean
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/strata/record_writer.rb', line 168 def matches_definition?(string) self.class_layout_rules.each do |field, rule| regex = rule['regex'] fixed_val = rule['fixed_val'] value = self.retrieve_field_value(string, field, rule) return false if fixed_val and value != fixed_val return false if regex and not value =~ /#{regex}/ end true end |
#retrieve_field_value(string, field, rule) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/strata/record_writer.rb', line 153 def retrieve_field_value(string, field, rule) offset = rule['offset'] - 1 length = rule['length'] field_type = rule['data_type'] value = string[offset, length] unless rule['fixed_val'] || rule['no_strip'] value = value.rstrip if field_type == 'A' value = value.to_i.to_s if field_type == 'N' end value end |
#set_allowed_characters(chars) ⇒ Object
109 110 111 |
# File 'lib/strata/record_writer.rb', line 109 def set_allowed_characters(chars) class_variable_set(:@@record_allowed_characters, chars) end |
#set_delimiter(delimiter) ⇒ Object
105 106 107 |
# File 'lib/strata/record_writer.rb', line 105 def set_delimiter(delimiter) class_variable_set(:@@record_delimiter, delimiter) end |
#set_record_length(length) ⇒ Object
101 102 103 |
# File 'lib/strata/record_writer.rb', line 101 def set_record_length(length) class_variable_set(:@@record_length, length) end |
#string_to_hash(string) ⇒ Object
181 182 183 184 185 186 187 188 189 |
# File 'lib/strata/record_writer.rb', line 181 def string_to_hash(string) hash = {} self.exposed_class_layout_rules.each do |field, rule| hash[field.to_sym] = self.retrieve_field_value(string, field, rule) end hash end |
#template_options ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/strata/record_writer.rb', line 136 def hash = {} self.exposed_class_layout_rules.each do |field, rule| value = rule.has_key?('fixed_val') ? rule['fixed_val'] : nil if value value = value.rjust(rule['length'], "0") if rule['data_type'] == 'N' value = value.ljust(rule['length'], " ") if rule['data_type'] == 'A' end hash[field.to_sym] = value end hash end |