class LedgerLite::Base
Attributes
addr[R]
Public Class Methods
config()
click to toggle source
# File lib/ledger-lite/base.rb, line 59 def self.config @config ||= Configuration.new end
configure() { |config| ... }
click to toggle source
lets you use
Ledger.configure do |config| config.coinbase = ['Keukenhof†'] end
# File lib/ledger-lite/base.rb, line 55 def self.configure yield( config ) end
new( *args )
click to toggle source
# File lib/ledger-lite/base.rb, line 72 def initialize( *args ) @addr = {} ## add all transactions passed in on startup; if no args - do nothing unless args.empty? ## note: MUST unsplat (*) args ## otherwise args get "double" wrapped in array write( *args ) end end
Public Instance Methods
unpack_transactions( blocks )
click to toggle source
# File lib/ledger-lite/base.rb, line 115 def unpack_transactions( blocks ) ## "unpack" transactions from possible (optional) blocks ## and return "flattend" **single** array of transactions blocks.reduce( [] ) do |acc, block| if block.respond_to?( :transactions ) ## bingo! assume it's block if it has transactions method acc + block.transactions else ## note: otherwise assumes it's just a "plain" **single** transaction tx = block acc + [tx] ## wrap in array (use acc << tx - with side effects/mutate in place - why? why not?) end end end
write( *args )
click to toggle source
# File lib/ledger-lite/base.rb, line 85 def write( *args ) puts "write:" pp args ## note: allow/support splat-* for now for convenience (auto-wraps args into array) if args.size == 1 && args[0].is_a?( Array ) puts " unwrap array in array" blks_or_txns = args[0] ## "unwrap" array in array elsif args.size == 1 && defined?( Blockchain ) && args[0].is_a?( Blockchain ) ## support passing in of "top-level" defined blockchain class if defined ## pass along all blocks ("unwrapped" from blockchain) blks_or_txns = [] args[0].each { |b| blks_or_txns << b } else blks_or_txns = args ## use "auto-wrapped" splat array end ## "unpack" transactions from possible (optional) blocks ## and return "flattend" **single** array of transactions transactions = unpack_transactions( blks_or_txns ) ## unpack & unsplat array (to pass in args to send) => from, to, amount transactions.each { |tx| send( *unpack(tx) ) } end