Class: TestCentricity::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/testcentricity_mobile/data_objects/data_objects_helper.rb

Direct Known Subclasses

EnvironData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_extensionObject

Returns the value of attribute file_extension.



39
40
41
# File 'lib/testcentricity_mobile/data_objects/data_objects_helper.rb', line 39

def file_extension
  @file_extension
end

#file_pathObject

Returns the value of attribute file_path.



38
39
40
# File 'lib/testcentricity_mobile/data_objects/data_objects_helper.rb', line 38

def file_path
  @file_path
end

Class Method Details

.calculate_dynamic_value(value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/testcentricity_mobile/data_objects/data_objects_helper.rb', line 123

def self.calculate_dynamic_value(value)
  test_value = value.split('!', 2)
  parameter = test_value[1].split('.', 2)
  result = case parameter[0]
           when 'Date'
             Chronic.parse(parameter[1])
           when 'FormattedDate', 'FormatDate'
             date_time_params = parameter[1].split(' format! ', 2)
             date_time = Chronic.parse(date_time_params[0].strip)
             date_time.to_s.format_date_time("#{date_time_params[1].strip}")
           else
             if Faker.constants.include?(parameter[0].to_sym)
               eval("Faker::#{parameter[0]}.#{parameter[1]}")
             else
               eval(test_value[1])
             end
           end
  result.to_s
end

.map_values(data, value_converters) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/testcentricity_mobile/data_objects/data_objects_helper.rb', line 115

def self.map_values(data, value_converters)
  data.each_with_object({}) do |(k, v), new_hash|
    converter = value_converters[k]
    v = converter.convert(v) if converter
    new_hash[k] = v
  end
end

.process_data(data, options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/testcentricity_mobile/data_objects/data_objects_helper.rb', line 94

def self.process_data(data, options)
  # calculate dynamic values if any are specified

  if data.is_a?(Hash)
    data.each do |key, value|
      data[key] = calculate_dynamic_value(value) if value.to_s.start_with?('eval!')
    end
  end
  return data unless options

  # convert keys to symbols if :keys_as_symbols is true in options

  if options.key?(:keys_as_symbols) && options[:keys_as_symbols]
    data.transform_keys!(&:to_sym)
  end
  # convert values if :value_converters are specified in options

  if options.key?(:value_converters)
    map_values(data, options[:value_converters])
  else
    data
  end
end

Instance Method Details

#read_file(file_name, key = nil, node_name = nil, options = nil) ⇒ Object



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
# File 'lib/testcentricity_mobile/data_objects/data_objects_helper.rb', line 41

def read_file(file_name, key = nil, node_name = nil, options = nil)
  # check to see if full file path was specified

  if File.exist?(file_name)
    @file_path = file_name
  else
    # construct the full file path to the file to be read

    @file_path = "#{PRIMARY_DATA_PATH}#{file_name}"
    unless File.exist?(@file_path)
      @file_path = "#{SECONDARY_DATA_PATH}#{file_name}"
      raise "File #{file_name} not found in Primary or Secondary folders in config folder" unless File.exist?(@file_path)
    end
  end
  # determine file type and read in data from file

  @file_extension = File.extname(file_name)
  data = case @file_extension
         when '.yml'
           YAML.load_file(@file_path)
         when'.json'
           JSON.parse(File.read(@file_path))
         when '.xml'
           xml_data = File.read(@file_path)
           Hash.from_xml(xml_data)
         when '.csv'
           if options
             SmarterCSV.process(@file_path, options)
           else
             SmarterCSV.process(@file_path)
           end
         else
           raise "#{file_name} is not a supported file type"
         end
  # return data if sourced from a .csv file

  return data if @file_extension == '.csv'

  # read data from specified key and/or node

  result = if key
             raise "Key #{key} not found" unless data.key?(key)

             if node_name
               raise "Node #{node_name} not found" unless data[key].key?(node_name)

               data[key][node_name]
             else
               data[key]
             end
           else
             data
           end
  self.class.send(:process_data, result, options)
end