Class: TransactionFaker::TransactionHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/transaction_faker/transaction_helper.rb

Class Method Summary collapse

Class Method Details

.convert_id(id_num, cat_division) ⇒ Object

Takes in a transaction ID number and floors it to match its superior category’s ID cat_division is either 1 (first category) or 2

i.e. 12034890 -> 12034000 or 12034890 -> 12000000


46
47
48
49
# File 'lib/transaction_faker/transaction_helper.rb', line 46

def self.convert_id(id_num, cat_division)
  divisor = 1000**(3 - cat_division)
  converted_id = ((id_num.to_f / divisor).to_i * divisor)
end

.create_category_array(id_num) ⇒ Object

Returns an array of strings with the category hierarchy,

i.e. ["Food and Drink", "Restaurants", "Mexican"]


35
36
37
38
39
40
41
# File 'lib/transaction_faker/transaction_helper.rb', line 35

def self.create_category_array(id_num)
  array = [
    Categories::FIRST_DIV[convert_id(id_num, 1)],
    Categories::SECOND_DIV[convert_id(id_num, 2)],
    Categories::THIRD_DIV[id_num]
  ].compact
end

.create_transactions(transaction_hash, account_id) ⇒ Object

Uses the TransactionHelper objects from the array created in ‘create_data’ to create an array of transactions over three months.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/transaction_faker/transaction_helper.rb', line 19

def self.create_transactions(transaction_hash, )
  3.times.flat_map do |month_offset|
    transaction_hash.flat_map do |category, data|
      subcategories = Categories::SUBCATEGORIES[category]

      normal_distribution(data[:mean], data[:std_dev], data[:monthly_freq]).map do |price|
        category_id = subcategories.sample
        cat_array = create_category_array(category_id)
        Transaction.new(, price, cat_array, category_id.to_s, month_offset + 1)
      end
    end
  end
end

.normal_distribution(mean, standard_dev, amount) ⇒ Object

Returns an array with “amount” number of random values that comply with the mean and the standard_dev.The function gen.rng() will return an error if standard_dev is 0, so 0.000001 provides the same functionality



11
12
13
14
15
# File 'lib/transaction_faker/transaction_helper.rb', line 11

def self.normal_distribution(mean, standard_dev, amount)
  standard_dev = 0.000001 if standard_dev == 0
  gen = Rubystats::NormalDistribution.new(mean, standard_dev)
  amount.times.map{ gen.rng().round(2) }
end