Class: X::MatchingRule

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Examples:

Build a rule a post matched

X::MatchingRule.new({"id" => "1165037377523306498", "tag" => "ruby"})

Parameters:

  • attrs (Hash{String, Symbol => Object}) —

    the attributes, which name the identifier as id, as an Integer or as the String the stream sends, and the tag as tag, unless the rule has none

Raises:

  • (ArgumentError) —

    if the attributes are not a Hash, the identifier is not one, or the tag is neither a String nor nil



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.

Examples:

Get the attributes

rule.attrs # => {"id" => "1165037377523306498", "tag" => "ruby"}

Returns:

  • (Hash{String => Object}) —

    the attributes, frozen



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

Examples:

Keep the posts that matched the rule tagged ruby

post.matching_rules.any? { |rule| rule in {tag: "ruby"} }

Parameters:

  • keys (Array<Symbol>, nil) —

    the keys the pattern asks for, or nil for both

Returns:

  • (Hash{Symbol => Integer, String, nil}) —

    the identifier and tag the pattern asks for



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.

Examples:

Get the identifier

rule.id # => 1165037377523306498

Returns:

  • (Integer) —

    the identifier

Raises:

  • (ArgumentError) —

    if the attributes hold no identifier, or one that is neither an Integer that is not negative nor a String of digits



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

Examples:

Inspect a rule

rule.inspect # => #<X::MatchingRule id=1165037377523306498 tag="ruby">

Returns:

  • (String) —

    the class name, identifier, and tag



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

Examples:

Get the tag

rule.tag # => "ruby"

Returns:

  • (String, nil) —

    the tag, or nil for a rule without one



80
# File 'x-objects/lib/x/objects/matching_rule.rb', line 80

def tag = attrs["tag"]