Class: Stellar::TrustLineFlags

Inherits:
Object
  • Object
show all
Defined in:
lib/stellar/trust_line_flags.rb

Class Method Summary collapse

Class Method Details

.make_mask(flags) ⇒ Fixnum

Converts an array of Stellar::TrustLineFlags members into an integers suitable for use in in a SetTrustLineFlagsOp.

Parameters:

Returns:

  • (Fixnum)

    the combined result


9
10
11
# File 'lib/stellar/trust_line_flags.rb', line 9

def self.make_mask(flags)
  normalize(flags).map(&:value).inject(&:|) || 0
end

.normalize(input) ⇒ Array<Stellar::TrustLineFlags>

Converts each element of the input array to Stellar::TrustLineFlags instance.

Parameters:

Returns:

Raises:

  • (TypeError)

    if any element of the input cannot be converted


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stellar/trust_line_flags.rb', line 27

def self.normalize(input)
  input.map do |val|
    case val
    when Stellar::TrustLineFlags
      val
    when ->(_) { members.key?(val.to_s) }
      from_name(val.to_s)
    when ->(_) { members.key?("#{val}_flag") }
      from_name("#{val}_flag")
    else
      raise TypeError, "unknown trustline flag: #{val}"
    end
  end
end

.parse_mask(combined) ⇒ Object

Converts an integer used in SetTrustLineFlagsOp on the set/clear flag options into an array of Stellar::TrustLineFlags members

@param combined [Fixnum]
@return [Array<Stellar::AccountFlags>]

18
19
20
# File 'lib/stellar/trust_line_flags.rb', line 18

def self.parse_mask(combined)
  members.values.select { |m| (m.value & combined) != 0 }
end

.set_clear_masks(flags) ⇒ Object


42
43
44
45
46
47
48
49
50
51
# File 'lib/stellar/trust_line_flags.rb', line 42

def self.set_clear_masks(flags)
  set_flags = []
  clear_flags = []

  Hash(flags).each do |flag, value|
    value.present? ? set_flags.push(flag) : clear_flags.push(flag)
  end

  {set_flags: make_mask(set_flags), clear_flags: make_mask(clear_flags)}
end