Class: Universa::FSStore::Entry

Inherits:
StoredContractBase show all
Extended by:
Forwardable
Defined in:
lib/universa/fs_store/entry.rb

Overview

The StoredContractBase implementation to work with FileStore. Not finished

Instance Attribute Summary

Attributes inherited from StoredContractBase

#chain_store

Instance Method Summary collapse

Methods inherited from StoredContractBase

#has_contract?, #initialize, #origin, #packed_contract, #packed_contract=, #parent, #save

Constructor Details

This class inherits a constructor from Universa::StoredContractBase

Instance Method Details

#amountBigDecimal

Returns contract.state.data.amount or nil. See Contract#amount for more.

Returns:

  • (BigDecimal)

    contract.state.data.amount or nil. See Contract#amount for more.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/universa/fs_store/entry.rb', line 14

class Entry < Universa::StoredContractBase

  extend Forwardable

  # (see StoredContract#load)
  def load(hash_id)
    init_with_hash_id hash_id
    self
  end

  # initialize new instance with an existing contract
  # @param [Contract] contract to store
  def init_with_contract(contract)
    self.contract = contract
    self
  end

  # initialize new instance with attributes YAML file
  # @param [String] file_name of the +.unicon.yaml+ file
  def load_from_yaml_file(file_name)
    init_with_yaml_file_name file_name
    self
  end

  # (see StoredContract#hash_id)
  def hash_id
    @id
  end

  # (see StoredContract#contract=)
  def contract= new_contract
    @id = new_contract.hash_id
    prepare_file_names
    super
    # we will always rewrite existing file to be sure it is correct
    open(@file_name, 'wb') {|f| f << contract.packed}
    # now we are to extract and rewrite attributes
    load_attributes_from_contract # it will save them too
  end

  # Implement lazy load logic
  # @return [Contract] instance loaded at first call only
  def contract
    # load it if it is not
    self.packed_contract = open(@file_name, 'rb') {|f| f.read} unless has_contract?
    # @attributes must already be set
    super
  end

  def_delegators :@attributes, :name, :currency, :amount

  protected

  # initialize instance for an existing file. Should be a contract already stored in the connected store.
  # @param [Object] hash_id to construct from
  def init_with_hash_id(hash_id)
    raise IllegalStateError, "already initialized" if @id
    @id = hash_id
    prepare_file_names
    load_attributes_from_file
    # attrs are already in the file so we need not to save them
  end

  # Load from attributes file name
  def init_with_yaml_file_name file_name
    @attr_file_name = file_name
    load_attributes_from_file
    prepare_file_names
  end

  # save attributes to .yaml file
  def save_attributes
    open(@attr_file_name, 'w') {|f| YAML.dump(@attributes, f)}
  end

  # load attributes from a contract (already assigned) and store them in the .yaml file
  def load_attributes_from_contract
    state = contract.state
    definition = contract.definition
    @attributes = SmartHash.new({
                                    id: hash_id.to_s,
                                    parent: parent&.to_s,
                                    origin: origin.to_s,
                                    name: definition.name,
                                    currency: definition.currency,
                                    amount: state.amount
                                })
    save_attributes
  end

  # load attributes from the .yaml file
  def load_attributes_from_file
    @attributes = SmartHash.new(YAML.load_file(@attr_file_name))
    @id = HashId.from_string @attributes.id
  end

  # prepare file name fields (@file_name and @attr_file_name)
  # @param [HashId] hash_id or ni to use one already set in @id
  def prepare_file_names(hash_id = nil)
    @file_name = "#{chain_store.root}/#{@id.to_url_safe_string[0..27]}.unicon"
    @attr_file_name = "#@file_name.yaml"
  end
end

#contractContract

Implement lazy load logic

Returns:

  • (Contract)

    instance loaded at first call only



56
57
58
59
60
61
# File 'lib/universa/fs_store/entry.rb', line 56

def contract
  # load it if it is not
  self.packed_contract = open(@file_name, 'rb') {|f| f.read} unless has_contract?
  # @attributes must already be set
  super
end

#contract=(new_contract) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/universa/fs_store/entry.rb', line 44

def contract= new_contract
  @id = new_contract.hash_id
  prepare_file_names
  super
  # we will always rewrite existing file to be sure it is correct
  open(@file_name, 'wb') {|f| f << contract.packed}
  # now we are to extract and rewrite attributes
  load_attributes_from_contract # it will save them too
end

#currencyString

Returns the contract.definition.data.currency value or nil.

Returns:

  • (String)

    the contract.definition.data.currency value or nil.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/universa/fs_store/entry.rb', line 14

class Entry < Universa::StoredContractBase

  extend Forwardable

  # (see StoredContract#load)
  def load(hash_id)
    init_with_hash_id hash_id
    self
  end

  # initialize new instance with an existing contract
  # @param [Contract] contract to store
  def init_with_contract(contract)
    self.contract = contract
    self
  end

  # initialize new instance with attributes YAML file
  # @param [String] file_name of the +.unicon.yaml+ file
  def load_from_yaml_file(file_name)
    init_with_yaml_file_name file_name
    self
  end

  # (see StoredContract#hash_id)
  def hash_id
    @id
  end

  # (see StoredContract#contract=)
  def contract= new_contract
    @id = new_contract.hash_id
    prepare_file_names
    super
    # we will always rewrite existing file to be sure it is correct
    open(@file_name, 'wb') {|f| f << contract.packed}
    # now we are to extract and rewrite attributes
    load_attributes_from_contract # it will save them too
  end

  # Implement lazy load logic
  # @return [Contract] instance loaded at first call only
  def contract
    # load it if it is not
    self.packed_contract = open(@file_name, 'rb') {|f| f.read} unless has_contract?
    # @attributes must already be set
    super
  end

  def_delegators :@attributes, :name, :currency, :amount

  protected

  # initialize instance for an existing file. Should be a contract already stored in the connected store.
  # @param [Object] hash_id to construct from
  def init_with_hash_id(hash_id)
    raise IllegalStateError, "already initialized" if @id
    @id = hash_id
    prepare_file_names
    load_attributes_from_file
    # attrs are already in the file so we need not to save them
  end

  # Load from attributes file name
  def init_with_yaml_file_name file_name
    @attr_file_name = file_name
    load_attributes_from_file
    prepare_file_names
  end

  # save attributes to .yaml file
  def save_attributes
    open(@attr_file_name, 'w') {|f| YAML.dump(@attributes, f)}
  end

  # load attributes from a contract (already assigned) and store them in the .yaml file
  def load_attributes_from_contract
    state = contract.state
    definition = contract.definition
    @attributes = SmartHash.new({
                                    id: hash_id.to_s,
                                    parent: parent&.to_s,
                                    origin: origin.to_s,
                                    name: definition.name,
                                    currency: definition.currency,
                                    amount: state.amount
                                })
    save_attributes
  end

  # load attributes from the .yaml file
  def load_attributes_from_file
    @attributes = SmartHash.new(YAML.load_file(@attr_file_name))
    @id = HashId.from_string @attributes.id
  end

  # prepare file name fields (@file_name and @attr_file_name)
  # @param [HashId] hash_id or ni to use one already set in @id
  def prepare_file_names(hash_id = nil)
    @file_name = "#{chain_store.root}/#{@id.to_url_safe_string[0..27]}.unicon"
    @attr_file_name = "#@file_name.yaml"
  end
end

#hash_idObject



39
40
41
# File 'lib/universa/fs_store/entry.rb', line 39

def hash_id
  @id
end

#init_with_contract(contract) ⇒ Object

initialize new instance with an existing contract

Parameters:



26
27
28
29
# File 'lib/universa/fs_store/entry.rb', line 26

def init_with_contract(contract)
  self.contract = contract
  self
end

#load(hash_id) ⇒ Object



19
20
21
22
# File 'lib/universa/fs_store/entry.rb', line 19

def load(hash_id)
  init_with_hash_id hash_id
  self
end

#load_from_yaml_file(file_name) ⇒ Object

initialize new instance with attributes YAML file

Parameters:

  • file_name (String)

    of the .unicon.yaml file



33
34
35
36
# File 'lib/universa/fs_store/entry.rb', line 33

def load_from_yaml_file(file_name)
  init_with_yaml_file_name file_name
  self
end

#nameString

Returns the contract.definition.data.name value or nil.

Returns:

  • (String)

    the contract.definition.data.name value or nil.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/universa/fs_store/entry.rb', line 14

class Entry < Universa::StoredContractBase

  extend Forwardable

  # (see StoredContract#load)
  def load(hash_id)
    init_with_hash_id hash_id
    self
  end

  # initialize new instance with an existing contract
  # @param [Contract] contract to store
  def init_with_contract(contract)
    self.contract = contract
    self
  end

  # initialize new instance with attributes YAML file
  # @param [String] file_name of the +.unicon.yaml+ file
  def load_from_yaml_file(file_name)
    init_with_yaml_file_name file_name
    self
  end

  # (see StoredContract#hash_id)
  def hash_id
    @id
  end

  # (see StoredContract#contract=)
  def contract= new_contract
    @id = new_contract.hash_id
    prepare_file_names
    super
    # we will always rewrite existing file to be sure it is correct
    open(@file_name, 'wb') {|f| f << contract.packed}
    # now we are to extract and rewrite attributes
    load_attributes_from_contract # it will save them too
  end

  # Implement lazy load logic
  # @return [Contract] instance loaded at first call only
  def contract
    # load it if it is not
    self.packed_contract = open(@file_name, 'rb') {|f| f.read} unless has_contract?
    # @attributes must already be set
    super
  end

  def_delegators :@attributes, :name, :currency, :amount

  protected

  # initialize instance for an existing file. Should be a contract already stored in the connected store.
  # @param [Object] hash_id to construct from
  def init_with_hash_id(hash_id)
    raise IllegalStateError, "already initialized" if @id
    @id = hash_id
    prepare_file_names
    load_attributes_from_file
    # attrs are already in the file so we need not to save them
  end

  # Load from attributes file name
  def init_with_yaml_file_name file_name
    @attr_file_name = file_name
    load_attributes_from_file
    prepare_file_names
  end

  # save attributes to .yaml file
  def save_attributes
    open(@attr_file_name, 'w') {|f| YAML.dump(@attributes, f)}
  end

  # load attributes from a contract (already assigned) and store them in the .yaml file
  def load_attributes_from_contract
    state = contract.state
    definition = contract.definition
    @attributes = SmartHash.new({
                                    id: hash_id.to_s,
                                    parent: parent&.to_s,
                                    origin: origin.to_s,
                                    name: definition.name,
                                    currency: definition.currency,
                                    amount: state.amount
                                })
    save_attributes
  end

  # load attributes from the .yaml file
  def load_attributes_from_file
    @attributes = SmartHash.new(YAML.load_file(@attr_file_name))
    @id = HashId.from_string @attributes.id
  end

  # prepare file name fields (@file_name and @attr_file_name)
  # @param [HashId] hash_id or ni to use one already set in @id
  def prepare_file_names(hash_id = nil)
    @file_name = "#{chain_store.root}/#{@id.to_url_safe_string[0..27]}.unicon"
    @attr_file_name = "#@file_name.yaml"
  end
end