class Quandl::Fabricate::Data

Attributes

attributes[RW]
columns[RW]
data[RW]
frequency[RW]
nils[RW]
offset[RW]
rows[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/quandl/fabricate/data.rb, line 11
def initialize(*args)
  self.attributes = default_options.merge args.extract_options!
end
rand(*args) click to toggle source
# File lib/quandl/fabricate/data.rb, line 5
def self.rand(*args)
  new(*args).random
end

Public Instance Methods

assign_attributes(attrs) click to toggle source

mass assignment protection

# File lib/quandl/fabricate/data.rb, line 85
def assign_attributes(attrs)
  attrs.each do |name, value|
    self.send("#{name}=", value) if self.respond_to?("#{name}=")
  end
end
attributes=(attrs) click to toggle source
# File lib/quandl/fabricate/data.rb, line 79
def attributes=(attrs)
  assign_attributes(attrs)
  attributes
end
date(index) click to toggle source
# File lib/quandl/fabricate/data.rb, line 40
def date(index)
  (Date.today - ( index * ( frequencies[frequency] ) - offset ))
end
default_options() click to toggle source
# File lib/quandl/fabricate/data.rb, line 65
def default_options
  {
    offset:     1,
    rows:       20 + rand(100),
    columns:    2 + rand(4),
    frequency:  :daily,
    monkey:     6
  }
end
frequencies() click to toggle source
# File lib/quandl/fabricate/data.rb, line 61
def frequencies
  {daily: 1, weekly: 7, monthly: 30, quarterly: 90, annual: 365}
end
point(row_index, column_index) click to toggle source
# File lib/quandl/fabricate/data.rb, line 44
def point(row_index, column_index)
  percent = ( (rand(10).to_f / 1000) ) - ( (rand(10).to_f / 850) ) + 1
  # increase the value
  trending_point[column_index] ||= column_index * column_index + 10
  trending_point[column_index] = trending_point[column_index] * percent
  # increase
  nils ? nil : trending_point[column_index]
end
random() click to toggle source
# File lib/quandl/fabricate/data.rb, line 24
def random
  data = []
  index = 0
  until data.count >= rows
    data << row(index) unless nils
    index += 1
  end
  Quandl::Data.new( data ).to_precision!(14).sort_descending
end
random_column() click to toggle source
# File lib/quandl/fabricate/data.rb, line 19
def random_column
  self.columns = 1
  random
end
row(index) click to toggle source
# File lib/quandl/fabricate/data.rb, line 34
def row(index)
  row = [ date(index) ]
  columns.times{|column_index| row << point(index, column_index) }
  row
end
to_csv(*args) click to toggle source
# File lib/quandl/fabricate/data.rb, line 15
def to_csv(*args)
  data.collect(&:to_csv).join
end