Class: Stellar::ClaimPredicate

Inherits:
Object
  • Object
show all
Extended by:
FactoryMethods
Defined in:
lib/stellar/claim_predicate.rb

Overview

Represents claim predicate on Stellar network.

Defined Under Namespace

Modules: FactoryMethods

Instance Method Summary collapse

Methods included from FactoryMethods

after, before, before_absolute_time, before_relative_time, compose, unconditional

Instance Method Details

#and(other) ⇒ ClaimPredicate Also known as: &

Constructs an `and` claim predicate.

This predicate will be fulfilled if both `self` and `other` predicates are fulfilled.

Parameters:

Returns:

Raises:

  • (TypeError)

101
102
103
104
# File 'lib/stellar/claim_predicate.rb', line 101

def and(other)
  raise TypeError, "no conversion from #{other.class.name} to ClaimPredicate" unless ClaimPredicate === other
  ClaimPredicate.new(ClaimPredicateType::AND, [self, other])
end

#describeObject


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/stellar/claim_predicate.rb', line 164

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

#evaluate(created_at, claiming_at) ⇒ Boolean

Evaluates the predicate value for provided inputs.

Parameters:

  • created_at (#to_time|#to_int)

    closing time of the ledger containing CreateClaimableBalance operation

  • 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 stellar-core using the closing time of a ledger containing ClaimClaimableBalance operation, in either successful or failed state.

Returns:

  • (Boolean)

    `true` if this predicate would allow claiming the balance, `false` otherwise


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/stellar/claim_predicate.rb', line 139

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

#inspectObject


194
195
196
# File 'lib/stellar/claim_predicate.rb', line 194

def inspect
  "#<ClaimPredicate: #{describe}>"
end

#notClaimPredicate Also known as: [email protected]

Constructs a `not` claim predicate.

This predicate will be fulfilled if `self` is not fulfilled.

Returns:


125
126
127
# File 'lib/stellar/claim_predicate.rb', line 125

def not
  ClaimPredicate.new(ClaimPredicateType::NOT, self)
end

#or(other) ⇒ ClaimPredicate Also known as: |

Constructs an `or` claim predicate.

This predicate will be fulfilled if either of `self` or `other` predicates is fulfilled.

Parameters:

Returns:

Raises:

  • (TypeError)

114
115
116
117
# File 'lib/stellar/claim_predicate.rb', line 114

def or(other)
  raise TypeError, "no conversion from #{other.class.name} to ClaimPredicate" unless ClaimPredicate === other
  ClaimPredicate.new(ClaimPredicateType::OR, [self, other])
end