Class: X::MatchingRule
- Inherits:
-
Object
- Object
- X::MatchingRule
- Includes:
- Serialization, ValueEquality, ValueMarshalling, Resources::Serialization, Resources::ValueEquality, Resources::ValueMarshalling
- Defined in:
- x-resources/lib/x/resources/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-streams, 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-resources depends on x-streams 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
-
#==(other) ⇒ Boolean
(also: #eql?)
included
from Resources::ValueEquality
Check whether another object holds the same attributes.
-
#as_json ⇒ Hash{String => Object}
included
from Resources::Serialization
The attributes, as a JSON encoder and ActiveSupport read them.
-
#deconstruct_keys(keys) ⇒ Hash{Symbol => Integer, String, nil}
The identifier and tag of the rule, which a pattern matches against.
-
#encode_with(coder) ⇒ void
included
from Resources::ValueMarshalling
Write the state Marshal writes as YAML.
-
#hash ⇒ Integer
included
from Resources::ValueEquality
The hash of the object, which equal objects share.
-
#id ⇒ Integer
The identifier the API gave the rule.
-
#init_with(coder) ⇒ void
included
from Resources::ValueMarshalling
Restore a value YAML read, frozen, as Marshal restores one.
-
#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.
-
#marshal_dump ⇒ Array(Integer, Hash{String => Object})
included
from Resources::ValueMarshalling
The state Marshal writes.
-
#marshal_load(state) ⇒ void
included
from Resources::ValueMarshalling
Restore a value Marshal read, frozen as the value that was written was.
-
#tag ⇒ String?
The tag the rule is labelled with.
-
#to_h {|key, value| ... } ⇒ Hash
included
from Resources::Serialization
The attributes, or with a block the Hash of the pairs it returns for them.
-
#to_json(state = nil) ⇒ String
included
from Resources::Serialization
The attributes as JSON.
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.
61 62 63 64 65 66 67 |
# File 'x-resources/lib/x/resources/matching_rule.rb', line 61 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.
46 47 48 |
# File 'x-resources/lib/x/resources/matching_rule.rb', line 46 def attrs @attrs end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql? Originally defined in module Resources::ValueEquality
Check whether another object holds the same attributes
#as_json ⇒ Hash{String => Object} Originally defined in module Resources::Serialization
The attributes, as a JSON encoder and ActiveSupport read them
#deconstruct_keys(keys) ⇒ Hash{Symbol => Integer, String, nil}
The identifier and tag of the rule, which a pattern matches against
96 |
# File 'x-resources/lib/x/resources/matching_rule.rb', line 96 def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[id tag]) |
#encode_with(coder) ⇒ void Originally defined in module Resources::ValueMarshalling
This method returns an undefined value.
Write the state Marshal writes as YAML
YAML would write the instance variables of the value, and read them back into one that is not frozen, so it says how it is written: each part of the state Marshal writes, under its name.
#hash ⇒ Integer Originally defined in module Resources::ValueEquality
The hash of the object, which equal objects share
#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.
80 |
# File 'x-resources/lib/x/resources/matching_rule.rb', line 80 def id = Shape.integer(attrs["id"]) || raise(ArgumentError, "a matching rule needs an id") |
#init_with(coder) ⇒ void Originally defined in module Resources::ValueMarshalling
This method returns an undefined value.
Restore a value YAML read, frozen, as Marshal restores one
#inspect ⇒ String
Summarize the rule for the console
104 |
# File 'x-resources/lib/x/resources/matching_rule.rb', line 104 def inspect = "#<#{self.class} id=#{id} tag=#{tag.inspect}>" |
#marshal_dump ⇒ Array(Integer, Hash{String => Object}) Originally defined in module Resources::ValueMarshalling
The state Marshal writes
What is written is plain data, led by the number of its format, so that a value written by one release of 1.x is read by a later one: its attributes, as the API sent them.
#marshal_load(state) ⇒ void Originally defined in module Resources::ValueMarshalling
This method returns an undefined value.
Restore a value Marshal read, frozen as the value that was written was
#tag ⇒ String?
The tag the rule is labelled with
87 |
# File 'x-resources/lib/x/resources/matching_rule.rb', line 87 def tag = attrs["tag"] |
#to_h {|key, value| ... } ⇒ Hash Originally defined in module Resources::Serialization
The attributes, or with a block the Hash of the pairs it returns for them
With a block it is the to_h of the attributes, as a Hash builds it, rather than the attributes, the block ignored.
#to_json(state = nil) ⇒ String Originally defined in module Resources::Serialization
The attributes as JSON