class Universa::StoredContractBase
under construction, pls don't use.
this is a base class for a contract stored in some contract chain. The implementation must inherit and implement its {#load} and {#save} methods at least. To do it, inherit and implement {ChainStore} to work with it.
Notable features:
-
contract could be assigned only once, no matter how, so its fields could be cached.
-
origin,
hash_id
and parent are cached. So other contract parameters should be.
Attributes
{ChainStore} instance to which it is connected
{Contract} instance stored in it. Can be lazy-loaded
{HashId} of the {#contract}
Public Class Methods
Construct implementation connected to a given store @param [ChainStore] chain_store
descendant class
# File lib/universa/stored_contract.rb, line 35 def initialize(chain_store) @chain_store = chain_store @chain_store.is_a?(ChainStore) or raise ArgumentError, "ChainStore instance required" @chain_store = chain_store end
Public Instance Methods
Assign contract to the instance. @param [Contracy] new_contract to store
# File lib/universa/stored_contract.rb, line 65 def contract=(new_contract) raise IllegalStateError, "contract can't be reassigned" if has_contract? @contract = new_contract @hash_id = @contract.hash_id @origin = @parent = nil end
For implementation logic, in particular, to make lazy loads. @return true if the stored contract is loaded into this instance
# File lib/universa/stored_contract.rb, line 43 def has_contract? !@contract.nil? end
override it to load the contract from the connected contract chain.
# File lib/universa/stored_contract.rb, line 59 def load hash_id raise NotFoundError end
@return [HashId] {#contract}.origin. See {Contract#origin}
# File lib/universa/stored_contract.rb, line 24 def origin @origin ||= @contract.origin end
Shortcut for `contract.packed`. See {Contract#packed} @return [String] binary string with contained contract packed transaction.
# File lib/universa/stored_contract.rb, line 49 def packed_contract @contract.packed end
Convenience method. Unoacks and stores the contract.
# File lib/universa/stored_contract.rb, line 73 def packed_contract=(new_packed_contract) self.contract = Contract.from_packed(new_packed_contract) end
@return [HashId] {#contract}.origin. See {Contract#parent}
# File lib/universa/stored_contract.rb, line 29 def parent @parent ||= @contract.parent end
override it to save the contract in the connected contract chain.
# File lib/universa/stored_contract.rb, line 54 def save raise NotFoundError end