Class: Hive::Transaction

Inherits:
Object
  • Object
show all
Includes:
JSONable, Utils
Defined in:
lib/hive/transaction.rb

Constant Summary collapse

ATTRIBUTES =
%i(id ref_block_num ref_block_prefix expiration operations
extensions signatures)

Instance Method Summary collapse

Methods included from Utils

#hexlify, #unhexlify

Methods included from JSONable

#as_json, included, #to_json

Constructor Details

#initialize(options = {}) ⇒ Transaction

Returns a new instance of Transaction.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hive/transaction.rb', line 11

def initialize(options = {})
  if !!(hex = options.delete(:hex))
    marshal = Marshal.new(hex: hex)
    marshal.transaction(trx: self)
  end
  
  options.each do |k, v|
    raise Hive::ArgumentError, "Invalid option specified: #{k}" unless ATTRIBUTES.include?(k.to_sym)

    send("#{k}=", v)
  end
  
  self.operations ||= []
  self.extensions ||= []
  self.signatures ||= []
  
  self.expiration = case @expiration
  when String then Time.parse(@expiration + 'Z')
  else; @expiration
  end
end

Instance Method Details

#==(other_trx) ⇒ Object



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
# File 'lib/hive/transaction.rb', line 71

def ==(other_trx)
  return true if self.equal? other_trx
  return false unless self.class == other_trx.class
  
  begin
    return false if self[:ref_block_num].to_i != transform_value(other_trx[:ref_block_num]).to_i
    return false if self[:ref_block_prefix].to_i != transform_value(other_trx[:ref_block_prefix]).to_i
    return false if self[:expiration].to_i != other_trx[:expiration].to_i
    return false if self[:operations].size != other_trx[:operations].size
    
    op_values = self[:operations].map do |type, value|
      [type.to_s, value.values.map{|v| transform_value(v).gsub(/[^a-zA-Z0-9-]/, '')}]
    end.flatten.sort
    
    other_op_values = other_trx[:operations].map do |type, value|
      [type.to_s, value.values.map{|v| transform_value(v).gsub(/[^a-zA-Z0-9-]/, '')}]
    end.flatten.sort
    
    # unless op_values == other_op_values
    #   require 'pry'; binding.pry
    # end
    
    op_values == other_op_values
  rescue => e
    # require 'pry'; binding.pry
    
    false
  end
end

#[](key) ⇒ Object



61
62
63
64
# File 'lib/hive/transaction.rb', line 61

def [](key)
  key = key.to_sym
  send(key) if self.class.attributes.include?(key)
end

#[]=(key, value) ⇒ Object



66
67
68
69
# File 'lib/hive/transaction.rb', line 66

def []=(key, value)
  key = key.to_sym
  send("#{key}=", value) if self.class.attributes.include?(key)
end

#expirationObject



49
50
51
52
53
54
55
# File 'lib/hive/transaction.rb', line 49

def expiration
  if @expiration.respond_to? :strftime
    @expiration.strftime('%Y-%m-%dT%H:%M:%S')
  else
    @expiration
  end
end

#expired?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/hive/transaction.rb', line 57

def expired?
  @expiration.nil? || @expiration < Time.now
end

#inspectObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hive/transaction.rb', line 33

def inspect
  properties = ATTRIBUTES.map do |prop|
    unless (v = instance_variable_get("@#{prop}")).nil?
      v = if v.respond_to? :strftime
        v.strftime('%Y-%m-%dT%H:%M:%S')
      else
        v
      end

      "@#{prop}=#{v}" 
    end
  end.compact.join(', ')
  
  "#<#{self.class.name} [#{properties}]>"
end