Class: Nanook::WalletAccount
- Inherits:
-
Object
- Object
- Nanook::WalletAccount
- Defined in:
- lib/nanook/wallet_account.rb
Constant Summary collapse
- UNITS =
[:raw, :nano]
- DEFAULT_UNIT =
:nano
Instance Method Summary collapse
- #account_id ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
-
#initialize(rpc, wallet, account) ⇒ WalletAccount
constructor
A new instance of WalletAccount.
-
#inspect ⇒ Object
:nodoc:.
-
#method_missing(m, *args, &block) ⇒ Object
Any method of Nanook::Account can be called on this class too.
- #pay(to:, amount:, unit: DEFAULT_UNIT, id:) ⇒ Object
-
#receive(block = nil) ⇒ Object
Returns false if no block to receive.
- #wallet_id ⇒ Object
Constructor Details
#initialize(rpc, wallet, account) ⇒ WalletAccount
Returns a new instance of WalletAccount.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/nanook/wallet_account.rb', line 7 def initialize(rpc, wallet, account) @rpc = rpc @wallet = wallet @account = account # An object to delegate account methods that don't # expect a wallet param in the RPC call, to allow this # class to support all methods that can be called on Nanook::Account @nanook_account_instance = Nanook::Account.new(@rpc, @account) # Wallet instance to call contains? on to check account # is in wallet @nanook_wallet_instance = Nanook::Wallet.new(@rpc, @wallet) if @account account_must_belong_to_wallet! end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
Any method of Nanook::Account can be called on this class too
98 99 100 101 102 103 104 |
# File 'lib/nanook/wallet_account.rb', line 98 def method_missing(m, *args, &block) if @nanook_account_instance.respond_to?(m) @nanook_account_instance.send(m, *args, &block) else super(m, *args, &block) end end |
Instance Method Details
#account_id ⇒ Object
26 27 28 |
# File 'lib/nanook/wallet_account.rb', line 26 def account_id @account end |
#create ⇒ Object
30 31 32 33 |
# File 'lib/nanook/wallet_account.rb', line 30 def create wallet_required! rpc(:account_create)[:account] end |
#destroy ⇒ Object
35 36 37 38 39 40 |
# File 'lib/nanook/wallet_account.rb', line 35 def destroy wallet_required! (rpc(:account_remove)[:removed] == 1).tap do |success| @known_valid_accounts.delete(@account) if success end end |
#inspect ⇒ Object
:nodoc:
42 43 44 |
# File 'lib/nanook/wallet_account.rb', line 42 def inspect # :nodoc: "#{self.class.name}(wallet_id: #{wallet_id}, account_id: #{account_id}, object_id: \"#{"0x00%x" % (object_id << 1)}\")" end |
#pay(to:, amount:, unit: DEFAULT_UNIT, id:) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/nanook/wallet_account.rb', line 46 def pay(to:, amount:, unit: DEFAULT_UNIT, id:) wallet_required! unless UNITS.include?(unit) raise ArgumentError.new("Unsupported unit: #{unit}") end # Check that to: account is valid unless Nanook::Account.new(@rpc, to).exists? raise ArgumentError.new("To account does not exist (#{to})") end raw = if unit.to_sym.eql?(:nano) Nanook::Util.NANO_to_raw(amount) else amount end # account is called source, so don't use the normal rpc method p = { wallet: @wallet, source: @account, destination: to, amount: raw, id: id } response = @rpc.call(:send, p) if response.has_key?(:error) return response[:error] end response[:block] end |
#receive(block = nil) ⇒ Object
Returns false if no block to receive
83 84 85 86 87 88 89 90 91 |
# File 'lib/nanook/wallet_account.rb', line 83 def receive(block=nil) wallet_required! if block.nil? _receive_without_block else _receive_with_block(block) end end |
#wallet_id ⇒ Object
93 94 95 |
# File 'lib/nanook/wallet_account.rb', line 93 def wallet_id @wallet end |