Class: Quandl::Fabricate::Data
- Inherits:
-
Object
- Object
- Quandl::Fabricate::Data
- Defined in:
- lib/quandl/fabricate/data.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#data ⇒ Object
Returns the value of attribute data.
-
#frequency ⇒ Object
Returns the value of attribute frequency.
-
#nils ⇒ Object
Returns the value of attribute nils.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#rows ⇒ Object
Returns the value of attribute rows.
Class Method Summary collapse
Instance Method Summary collapse
-
#assign_attributes(attrs) ⇒ Object
mass assignment protection.
- #date(index) ⇒ Object
- #default_options ⇒ Object
- #frequencies ⇒ Object
-
#initialize(*args) ⇒ Data
constructor
A new instance of Data.
- #point(row_index, column_index) ⇒ Object
- #random ⇒ Object
- #random_column ⇒ Object
- #row(index) ⇒ Object
- #to_csv(*args) ⇒ Object
- #trending_point ⇒ Object
Constructor Details
#initialize(*args) ⇒ Data
Returns a new instance of Data.
11 12 13 |
# File 'lib/quandl/fabricate/data.rb', line 11 def initialize(*args) self.attributes = .merge args. end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
9 10 11 |
# File 'lib/quandl/fabricate/data.rb', line 9 def attributes @attributes end |
#columns ⇒ Object
Returns the value of attribute columns.
9 10 11 |
# File 'lib/quandl/fabricate/data.rb', line 9 def columns @columns end |
#data ⇒ Object
Returns the value of attribute data.
9 10 11 |
# File 'lib/quandl/fabricate/data.rb', line 9 def data @data end |
#frequency ⇒ Object
Returns the value of attribute frequency.
9 10 11 |
# File 'lib/quandl/fabricate/data.rb', line 9 def frequency @frequency end |
#nils ⇒ Object
Returns the value of attribute nils.
9 10 11 |
# File 'lib/quandl/fabricate/data.rb', line 9 def nils @nils end |
#offset ⇒ Object
Returns the value of attribute offset.
9 10 11 |
# File 'lib/quandl/fabricate/data.rb', line 9 def offset @offset end |
#rows ⇒ Object
Returns the value of attribute rows.
9 10 11 |
# File 'lib/quandl/fabricate/data.rb', line 9 def rows @rows end |
Class Method Details
.rand(*args) ⇒ Object
5 6 7 |
# File 'lib/quandl/fabricate/data.rb', line 5 def self.rand(*args) new(*args).random end |
Instance Method Details
#assign_attributes(attrs) ⇒ Object
mass assignment protection
85 86 87 88 89 |
# 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 |
#date(index) ⇒ Object
40 41 42 |
# File 'lib/quandl/fabricate/data.rb', line 40 def date(index) (Date.today - ( index * ( frequencies[frequency] ) - offset )) end |
#default_options ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/quandl/fabricate/data.rb', line 65 def { offset: 1, rows: 20 + rand(100), columns: 2 + rand(4), frequency: :daily, monkey: 6 } end |
#frequencies ⇒ Object
61 62 63 |
# 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) ⇒ Object
44 45 46 47 48 49 50 51 |
# 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 ⇒ Object
24 25 26 27 28 29 30 31 32 |
# 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 ⇒ Object
19 20 21 22 |
# File 'lib/quandl/fabricate/data.rb', line 19 def random_column self.columns = 1 random end |
#row(index) ⇒ Object
34 35 36 37 38 |
# 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) ⇒ Object
15 16 17 |
# File 'lib/quandl/fabricate/data.rb', line 15 def to_csv(*args) data.collect(&:to_csv).join end |
#trending_point ⇒ Object
53 54 55 |
# File 'lib/quandl/fabricate/data.rb', line 53 def trending_point @trending_point ||= {} end |