Class: Quandl::Fabricate::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/quandl/fabricate/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Data

Returns a new instance of Data.



9
10
11
# File 'lib/quandl/fabricate/data.rb', line 9

def initialize(*args)
  self.attributes = default_options.merge args.extract_options!
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



7
8
9
# File 'lib/quandl/fabricate/data.rb', line 7

def attributes
  @attributes
end

#columnsObject

Returns the value of attribute columns.



7
8
9
# File 'lib/quandl/fabricate/data.rb', line 7

def columns
  @columns
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/quandl/fabricate/data.rb', line 7

def data
  @data
end

#frequencyObject

Returns the value of attribute frequency.



7
8
9
# File 'lib/quandl/fabricate/data.rb', line 7

def frequency
  @frequency
end

#nilsObject

Returns the value of attribute nils.



7
8
9
# File 'lib/quandl/fabricate/data.rb', line 7

def nils
  @nils
end

#offsetObject

Returns the value of attribute offset.



7
8
9
# File 'lib/quandl/fabricate/data.rb', line 7

def offset
  @offset
end

#rowsObject

Returns the value of attribute rows.



7
8
9
# File 'lib/quandl/fabricate/data.rb', line 7

def rows
  @rows
end

Class Method Details

.rand(*args) ⇒ Object



3
4
5
# File 'lib/quandl/fabricate/data.rb', line 3

def self.rand(*args)
  new(*args).random
end

Instance Method Details

#assign_attributes(attrs) ⇒ Object

mass assignment protection



83
84
85
86
87
# File 'lib/quandl/fabricate/data.rb', line 83

def assign_attributes(attrs)
  attrs.each do |name, value|
    self.send("#{name}=", value) if self.respond_to?("#{name}=")
  end
end

#date(index) ⇒ Object



38
39
40
# File 'lib/quandl/fabricate/data.rb', line 38

def date(index)
  (Date.today - ( index * ( frequencies[frequency] ) - offset )).jd
end

#default_optionsObject



63
64
65
66
67
68
69
70
71
# File 'lib/quandl/fabricate/data.rb', line 63

def default_options
  {
    offset:     1,
    rows:       20 + rand(100),
    columns:    2 + rand(4),
    frequency:  :daily,
    monkey:     6
  }
end

#frequenciesObject



59
60
61
# File 'lib/quandl/fabricate/data.rb', line 59

def frequencies
  {daily: 1, weekly: 7, monthly: 30, quarterly: 90, annual: 365}
end

#point(row_index, column_index) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/quandl/fabricate/data.rb', line 42

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

#randomObject



22
23
24
25
26
27
28
29
30
# File 'lib/quandl/fabricate/data.rb', line 22

def random
  data = []
  index = 0
  until data.count >= rows
    data << row(index) unless nils
    index += 1
  end
  Quandl::Data.new( data ).sort_descending
end

#random_columnObject



17
18
19
20
# File 'lib/quandl/fabricate/data.rb', line 17

def random_column
  self.columns = 1
  random
end

#row(index) ⇒ Object



32
33
34
35
36
# File 'lib/quandl/fabricate/data.rb', line 32

def row(index)
  row = [ date(index) ]
  columns.times{|column_index| row << point(index, column_index) }
  row
end

#to_csv(*args) ⇒ Object



13
14
15
# File 'lib/quandl/fabricate/data.rb', line 13

def to_csv(*args)
  data.collect(&:to_csv).join
end


51
52
53
# File 'lib/quandl/fabricate/data.rb', line 51

def trending_point
  @trending_point ||= {}
end