class Digitalbits::ClaimPredicate
Represents claim predicate on DigitalBits network.
Public Instance Methods
Constructs an `and` claim predicate.
This predicate will be fulfilled if both `self` and `other` predicates are fulfilled.
@param other [ClaimPredicate] another predicate
@return [ClaimPredicate] `and` claim predicate
# File lib/digitalbits/claim_predicate.rb, line 100 def and(other) raise TypeError, "no conversion from #{other.class.name} to ClaimPredicate" unless ClaimPredicate === other ClaimPredicate.new(ClaimPredicateType::AND, [self, other]) end
# File lib/digitalbits/claim_predicate.rb, line 163 def describe case switch when ClaimPredicateType::UNCONDITIONAL "always" when ClaimPredicateType::BEFORE_RELATIVE_TIME dur = ActiveSupport::Duration.build(value) "less than #{dur.inspect} since creation" when ClaimPredicateType::BEFORE_ABSOLUTE_TIME "before #{Time.at(value).to_formatted_s(:db)}" when ClaimPredicateType::AND value.map(&:describe).join(" and ") when ClaimPredicateType::OR "(" << value.map(&:describe).join(" or ") << ")" when ClaimPredicateType::NOT case value.switch when ClaimPredicateType::UNCONDITIONAL "never" when ClaimPredicateType::BEFORE_RELATIVE_TIME dur = ActiveSupport::Duration.build(value.value) "#{dur.inspect} or more since creation" when ClaimPredicateType::BEFORE_ABSOLUTE_TIME "after #{Time.at(value.value).to_formatted_s(:db)}" else "not (#{value.describe})" end else raise ArgumentError, "evaluation is not implemented for #{switch.name} predicate" end end
Evaluates the predicate value for provided inputs.
@param created_at [#to_time|#to_int] closing time of the ledger containing CreateClaimableBalance operation @param claiming_at [#to_time|#to_int|ActiveSupport::Duration] time point to evaluate predicate at, either
absolute time or duration relative to `created_at`. In reality predicate will be evaluated by digitalbits-core using the closing time of a ledger containing ClaimClaimableBalance operation, in either successful or failed state.
@return [Boolean] `true` if this predicate would allow claiming the balance, `false` otherwise
# File lib/digitalbits/claim_predicate.rb, line 138 def evaluate(created_at, claiming_at) created_at = created_at.to_time if created_at.respond_to?(:to_time) claiming_at = created_at + claiming_at if claiming_at.is_a?(ActiveSupport::Duration) claiming_at = claiming_at.to_time if claiming_at.respond_to?(:to_time) return false if claiming_at < created_at case switch when ClaimPredicateType::UNCONDITIONAL true when ClaimPredicateType::BEFORE_RELATIVE_TIME Integer(claiming_at) < Integer(created_at) + value when ClaimPredicateType::BEFORE_ABSOLUTE_TIME Integer(claiming_at).to_i < value when ClaimPredicateType::AND value[0].evaluate(created_at, claiming_at) && value[1].evaluate(created_at, claiming_at) when ClaimPredicateType::OR value[0].evaluate(created_at, claiming_at) || value[1].evaluate(created_at, claiming_at) when ClaimPredicateType::NOT !value.evaluate(created_at, claiming_at) else raise ArgumentError, "evaluation is not implemented for #{switch.name} predicate" end end
# File lib/digitalbits/claim_predicate.rb, line 193 def inspect "#<ClaimPredicate: #{describe}>" end
Constructs a `not` claim predicate.
This predicate will be fulfilled if `self` is not fulfilled.
@return [ClaimPredicate] `not` claim predicate
# File lib/digitalbits/claim_predicate.rb, line 124 def not ClaimPredicate.new(ClaimPredicateType::NOT, self) end
Constructs an `or` claim predicate.
This predicate will be fulfilled if either of `self` or `other` predicates is fulfilled.
@param other [ClaimPredicate] another predicate.
@return [ClaimPredicate] `or` claim predicate
# File lib/digitalbits/claim_predicate.rb, line 113 def or(other) raise TypeError, "no conversion from #{other.class.name} to ClaimPredicate" unless ClaimPredicate === other ClaimPredicate.new(ClaimPredicateType::OR, [self, other]) end