lib/vault-tree/contract/contract.rb in vault-tree-0.3.4 vs lib/vault-tree/contract/contract.rb in vault-tree-0.6.0

- old
+ new

@@ -1,96 +1,39 @@ module VaultTree class Contract - attr_reader :json + attr_reader :json, :contract_hash, :vault_list, :contract_header, :external_input def initialize(json, params = {}) - @json = json - @master_passphrase = params[:master_passphrase] - @external_data = params[:external_data] + @contract_hash = Support::JSON.decode(json) + @contract_header = ContractHeader.new(contract_hash["header"]) + @vault_list = VaultList.new(contract_hash["vaults"], self) + @external_input = {} end - def close_vault(id, params = {data: nil}) - update_external_data(id: id , data: params[:data]) - validate_vault(id) - update_vaults vault(id).close - self + def close_vault(id, external_input = {data: nil}) + @external_input = @external_input.merge(external_input) + @vault_list = vault_list.close_vault(id) + self.as_json end - def retrieve_contents(id) - validate_vault(id) - vault(id).retrieve_contents + def open_vault(id, external_input = {}) + @external_input = @external_input.merge(external_input) + vault_list.open_vault(id) end def vault_closed?(id) - non_empty_contents?(id) + vault_list.vault_closed?(id) end def header - contract["header"] + contract_header.to_hash end def vaults - contract["vaults"] + vault_list.to_hash end - def vault(id) - id.nil? ? NullVault.new : Vault.new(id, vaults[id], self) - end - - def update_vaults(vault) - @contract["vaults"][vault.id] = vault.properties unless vault.kind_of?(NullVault) - end - def as_json ContractPresenter.new(self).as_json - end - - def master_passphrase - validate_passphrase - @master_passphrase - end - - def external_data_hash - @external_data - end - - def external_data(id) - @external_data[id] - end - - private - - def passphrase_present? - !! @master_passphrase - end - - def valid_id?(id) - id.nil? || vaults.include?(id) - end - - def non_empty_contents?(id) - ! empty_contents?(id) - end - - def empty_contents?(id) - vaults[id]['contents'].nil? || vaults[id]['contents'].empty? - end - - def contract - @contract ||= Support::JSON.decode(json) - end - - def validate_vault(id) - raise Exceptions::VaultDoesNotExist unless valid_id?(id) - end - - def validate_passphrase - raise Exceptions::MissingPassphrase unless passphrase_present? - end - - def update_external_data(params) - vault_id = params[:id] - data = params[:data] - @external_data = @external_data.merge({"#{vault_id}" => "#{data}"}) unless data.nil? end end end