Class: LedgerLite::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ledger-lite/base.rb

Direct Known Subclasses

Ledger, V2::Ledger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



72
73
74
75
76
77
78
79
80
81
# 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

Instance Attribute Details

#addrObject (readonly)

make addr private e.g. remove - why? e.g. use hash forwards/delegates - why not?



65
66
67
# File 'lib/ledger-lite/base.rb', line 65

def addr
  @addr
end

Class Method Details

.configObject



59
60
61
# File 'lib/ledger-lite/base.rb', line 59

def self.config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

lets you use

Ledger.configure do |config|
   config.coinbase = ['Keukenhof†']
end

Yields:



55
56
57
# File 'lib/ledger-lite/base.rb', line 55

def self.configure
  yield( config )
end

Instance Method Details

#unpack_transactions(blocks) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object Also known as: add, <<



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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