Module: Strata::RecordWriter

Defined in:
lib/strata/record_writer.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#allowed_charactersObject



47
48
49
50
51
# File 'lib/strata/record_writer.rb', line 47

def allowed_characters
  if self.class.class_variable_defined?(:@@record_allowed_characters)
    self.class.class_variable_get(:@@record_allowed_characters)
  end
end

#exposed_rulesObject



8
9
10
# File 'lib/strata/record_writer.rb', line 8

def exposed_rules
  layout_rules.select {|key, rule| !(rule["expose"] == false && rule.has_key?("expose")) }
end

#filler_rulesObject



12
13
14
# File 'lib/strata/record_writer.rb', line 12

def filler_rules
  @filler_rules ||= self.class.filler_layout_rules  
end

#layout_rulesObject



4
5
6
# File 'lib/strata/record_writer.rb', line 4

def layout_rules
  @layout_rules ||= self.class.class_layout_rules  
end

#record_delimiterObject



43
44
45
# File 'lib/strata/record_writer.rb', line 43

def record_delimiter
  self.class.class_variable_get(:@@record_delimiter) || ""
end

#record_lengthObject



39
40
41
# File 'lib/strata/record_writer.rb', line 39

def record_length
  self.class.class_variable_get(:@@record_length) || 0
end

#set_filler(string) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/strata/record_writer.rb', line 31

def set_filler(string)
  filler_rules.each do |key, value|
    string[(value["offset"] - 1), value["length"]] = value["fixed_val"]
  end

  return string
end

#set_layout_variables(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/strata/record_writer.rb', line 16

def set_layout_variables(options = {})
  self.class.define_attribute_accessors

  options.each do |k,v|
    raise "#{k}: Argument is not a string" unless v.is_a? String
    self.class.send :attr_accessor, k
    self.send "#{k}=", v.upcase
  end

  layout_rules.each do |k,v|
    self.class.send(:attr_accessor, k) unless (v["expose"] && v["expose"] == false)
    # self.send "#{k}=", v['fixed_val'] if v.has_key? ""
  end
end

#to_sObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/strata/record_writer.rb', line 53

def to_s
  @string = " " * record_length + record_delimiter
  @string = set_filler(@string)

  @string = "#{@string}"

  self.exposed_rules.each do |field_name,rule|
    value = self.send(field_name) || ""

    value = value.rjust(rule['length'], "0") if rule['data_type'] == 'N'
    value = value.ljust(rule['length'], " ") if rule['data_type'] == 'A'
  
    offset = rule['offset'] - 1
    length = rule['length']
  
    @string[(offset), length] = value
  end 

  @string
end

#valid_characters(string) ⇒ Object



74
75
76
77
# File 'lib/strata/record_writer.rb', line 74

def valid_characters(string)
  return true unless allowed_characters
  string.each_char.all? {|c| allowed_characters.include?(c)}
end

#validate!(options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/strata/record_writer.rb', line 87

def validate!(options)
  options.each do |k,v|
    rule = layout_rules[k.to_s]
    
    validate_field(k, v)
    raise "#{k}: Input too long" if v.length > rule['length']
    raise "#{k}: Invalid data" if rule['regex'] && ((v =~ /#{rule['regex']}/) != 0)
    raise "#{k}: Invalid data - expected #{rule['fixed_val']}, got #{v}" if rule['fixed_val'] && (v != rule['fixed_val'])
    raise "#{k}: Numeric value required" if (rule['data_type'] == 'N') && !(Float(v) rescue false)
  end
end

#validate_field(field_name, field_value) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/strata/record_writer.rb', line 79

def validate_field(field_name, field_value)
  if field_name != :user_ref
    raise "#{field_name}: Invalid character used in #{field_value}" unless valid_characters(field_value)
  end
  
  raise "#{field_name}: Argument is not a string" unless field_value.is_a? String
end