Class: Heisencoin::Market
- Inherits:
-
Object
- Object
- Heisencoin::Market
- Defined in:
- lib/heisencoin/market.rb
Instance Attribute Summary collapse
-
#offers ⇒ Object
Returns the value of attribute offers.
Instance Method Summary collapse
- #better_than(a, b) ⇒ Object
- #drop_offers(exchange) ⇒ Object
- #import(exchange, offers) ⇒ Object
-
#initialize(half) ⇒ Market
constructor
A new instance of Market.
- #offers_better_than(price) ⇒ Object
- #sorted_insert(array, element) ⇒ Object
Constructor Details
#initialize(half) ⇒ Market
Returns a new instance of Market.
5 6 7 8 9 10 11 12 |
# File 'lib/heisencoin/market.rb', line 5 def initialize(half) @offers = [] if half == :bid || half == :ask @half = half else raise "Bad market type parameter" end end |
Instance Attribute Details
#offers ⇒ Object
Returns the value of attribute offers.
3 4 5 |
# File 'lib/heisencoin/market.rb', line 3 def offers @offers end |
Instance Method Details
#better_than(a, b) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/heisencoin/market.rb', line 43 def better_than(a,b) if @half == :ask a < b elsif @half == :bid a > b end end |
#drop_offers(exchange) ⇒ Object
21 22 23 |
# File 'lib/heisencoin/market.rb', line 21 def drop_offers(exchange) offers.reject!{|o| o.exchange == exchange} end |
#import(exchange, offers) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/heisencoin/market.rb', line 14 def import(exchange, offers) offers.each do |raw_offer| offer = Offer.from_array(exchange, [raw_offer.first, raw_offer.last]) sorted_insert(@offers, offer) {|offer| offer.price} end end |
#offers_better_than(price) ⇒ Object
51 52 53 |
# File 'lib/heisencoin/market.rb', line 51 def offers_better_than(price) offers.select{|offer| better_than(offer.price, price)} end |
#sorted_insert(array, element) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/heisencoin/market.rb', line 25 def sorted_insert(array, element) value = yield element if array.length == 0 || better_than(value, (yield array[0])) array.unshift(element) else last_index = array.length-1 for idx in 0..last_index if not better_than((yield array[idx]), value) array.insert(idx, element) break end if idx == last_index array.push(element) end end end end |