Module: Stellar::ClaimPredicate::FactoryMethods

Included in:
Stellar::ClaimPredicate
Defined in:
lib/stellar/claim_predicate.rb

Instance Method Summary collapse

Instance Method Details

#after(time) ⇒ ClaimPredicate

Constructs a negated predicate from either relative or absolute time based on the type of the input.

Parameters:

  • time (ActiveSupport::Duration|#to_time|#to_i)

    duration since ledger close time or absolute time value

Returns:

  • (ClaimPredicate)

    `before_relative_time` or `before_absolute_time` claim predicate.

See Also:


73
74
75
# File 'lib/stellar/claim_predicate.rb', line 73

def after(time)
  ~before(time)
end

#before(time) ⇒ ClaimPredicate

Constructs either relative or absolute time predicate based on the type of the input.

If input is an instance of `ActiveSupport::Duration` class it will be handled as a relative time (seconds since close time of the ledger), otherwise it will be treated as an absolute time.

It is intended to work with time helpers provided by ActiveSupport, like `1.day` (relative) or `2.weeks.from_now` (absolute).

Examples:

relative time

ClaimPredicate.before(2.days + 15.seconds)

absolute time

ClaimPredicate.before(5.hours.from_now)

Parameters:

  • time (ActiveSupport::Duration|#to_time|#to_i)

    duration since ledger close time or absolute time value

Returns:

  • (ClaimPredicate)

    `before_relative_time` or `before_absolute_time` claim predicate.


64
65
66
# File 'lib/stellar/claim_predicate.rb', line 64

def before(time)
  ActiveSupport::Duration === time ? before_relative_time(time.to_i) : before_absolute_time(time)
end

#before_absolute_time(timestamp) ⇒ ClaimPredicate

Constructs an `before_absolute_time` claim predicate.

This predicate will be fulfilled if the closing time of the ledger that includes the Stellar::CreateClaimableBalance operation is less than provided timestamp.

Parameters:

  • timestamp (#to_time|#to_int|#to_i)

    time value or timestamp

Returns:


41
42
43
44
45
# File 'lib/stellar/claim_predicate.rb', line 41

def before_absolute_time(timestamp)
  timestamp = timestamp.to_time if timestamp.respond_to?(:to_time)

  ClaimPredicate.new(ClaimPredicateType::BEFORE_ABSOLUTE_TIME, Integer(timestamp))
end

#before_relative_time(seconds) ⇒ ClaimPredicate

Constructs a `before_relative_time` claim predicate.

This predicate will be fulfilled if the closing time of the ledger that includes the Stellar::CreateClaimableBalance operation plus this relative time delta (in seconds) is less than the current time.

Parameters:

  • seconds (#to_int|#to_i)

    seconds since `closeTime` of the ledger in which the ClaimableBalanceEntry was created.

Returns:


29
30
31
# File 'lib/stellar/claim_predicate.rb', line 29

def before_relative_time(seconds)
  ClaimPredicate.new(ClaimPredicateType::BEFORE_RELATIVE_TIME, Integer(seconds))
end

#compose(&block) ⇒ ClaimPredicate

Compose a complex predicate by calling DSL methods from the block.

Examples:

ClaimPredicate.compose {
  after(15.minutes) & before(1.day) | after(1.week.from_now) & before(1.week.from_now + 1.day)
}

Yield Returns:

Returns:

  • (ClaimPredicate)

    `not(before_relative_time)` or `not(before_absolute_time)` claim predicate.


86
87
88
89
# File 'lib/stellar/claim_predicate.rb', line 86

def compose(&block)
  result = instance_eval(&block)
  result.nil? ? unconditional : result
end

#unconditionalClaimPredicate

Constructs an `unconditional` claim predicate.

This predicate will be always fulfilled.

Returns:


16
17
18
# File 'lib/stellar/claim_predicate.rb', line 16

def unconditional
  ClaimPredicate.new(ClaimPredicateType::UNCONDITIONAL)
end