Class: X::MatchingRule
- Inherits:
-
Object
- Object
- X::MatchingRule
- Includes:
- Serialization, ValueEquality, ValueMarshalling
- Defined in:
- x-objects/lib/x/objects/matching_rule.rb
Overview
A rule of the filtered stream that a post the stream delivered matched: the identifier the API gave the rule, and the tag it is labelled with
The stream names a rule a post matched by its identifier and its tag alone, not by the value it matches, so this is not an X::StreamRule of x-streaming, which holds that value. It names the rule to X::StreamingClient#delete_rules by its identifier, as rule.id, or as rule.to_h, and it is found among the rules X::StreamingClient#rules reads by the identifier they share. x-objects depends on x-streaming for none of this: a stream builds each post with X::Post.from_response, as a request does.
It is frozen, compares equal to a rule of the same attributes, and matches a pattern of its identifier and tag,
as in rule in {tag: "ruby"}. Its attributes are what the stream sent of the rule, as those of a resource are, so
to_h, as_json, and to_json give them as the stream did, whatever the stream comes to send of a rule beside its
identifier and tag.
Instance Attribute Summary collapse
-
#attrs ⇒ Hash{String => Object}
readonly
The attributes of the rule, as the stream sends them.
Instance Method Summary collapse
-
#deconstruct_keys(keys) ⇒ Hash{Symbol => Integer, String, nil}
The identifier and tag of the rule, which a pattern matches against.
-
#id ⇒ Integer
The identifier the API gave the rule.
-
#initialize(attrs) ⇒ MatchingRule
constructor
Initialize a rule a post matched from the attributes the stream sent of it.
-
#inspect ⇒ String
Summarize the rule for the console.
-
#tag ⇒ String?
The tag the rule is labelled with.
Constructor Details
#initialize(attrs) ⇒ MatchingRule
Initialize a rule a post matched from the attributes the stream sent of it
The identifier and tag are read as it is built, so that a rule that holds either as something else raises here, rather than from its readers.
54 55 56 57 58 59 60 |
# File 'x-objects/lib/x/objects/matching_rule.rb', line 54 def initialize(attrs) @attrs = Utils.deep_freeze(Utils.attributes!(attrs)) id raise ArgumentError, "tag must be a String, not #{tag.inspect}" unless tag.nil? || tag.is_a?(String) freeze end |
Instance Attribute Details
#attrs ⇒ Hash{String => Object} (readonly)
The attributes of the rule, as the stream sends them
The identifier is a String, as the stream sends it, and a rule without a tag holds none.
39 40 41 |
# File 'x-objects/lib/x/objects/matching_rule.rb', line 39 def attrs @attrs end |
Instance Method Details
#deconstruct_keys(keys) ⇒ Hash{Symbol => Integer, String, nil}
The identifier and tag of the rule, which a pattern matches against
89 |
# File 'x-objects/lib/x/objects/matching_rule.rb', line 89 def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[id tag]) |
#id ⇒ Integer
The identifier the API gave the rule
The API sends it as a String, and it is read as an Integer, as the identifier of a stream rule is, and as strictly as any identifier is read: a String of digits alone, with no sign, underscore, or whitespace.
73 |
# File 'x-objects/lib/x/objects/matching_rule.rb', line 73 def id = Shape.integer(attrs["id"]) || raise(ArgumentError, "a matching rule needs an id") |
#inspect ⇒ String
Summarize the rule for the console
97 |
# File 'x-objects/lib/x/objects/matching_rule.rb', line 97 def inspect = "#<#{self.class} id=#{id} tag=#{tag.inspect}>" |
#tag ⇒ String?
The tag the rule is labelled with
80 |
# File 'x-objects/lib/x/objects/matching_rule.rb', line 80 def tag = attrs["tag"] |