Module: X::Resources::ValueMarshalling

Included in:
MatchingRule, PersonalizedTrend, PostUsage, Trend
Defined in:
x-resources/lib/x/resources/value_marshalling.rb

Overview

The state Marshal writes and reads of what an API response held that is not a resource

A trend, the usage of a project, and a rule a post matched hold their attributes alone, so what is written is those attributes, led by the number of their format, as a resource is written, and what is read is built of them as the constructor builds it, frozen.

Internal to x-resources: the methods it gives X::Trend, X::PersonalizedTrend, X::PostUsage, and X::MatchingRule, marshal_dump, marshal_load, encode_with, and init_with, are public API, but the module is only how they are shared, and which classes include it can change within 1.x.

Instance Method Summary collapse

Instance Method Details

#encode_with(coder) ⇒ void

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.

Examples:

Write a value as YAML

YAML.dump(trend)

Parameters:

  • coder (Psych::Coder) —

    the coder YAML writes the value with



67
# File 'x-resources/lib/x/resources/value_marshalling.rb', line 67

def encode_with(coder) = YAML_KEYS.zip(marshal_dump) { |key, value| coder[key] = value }

#init_with(coder) ⇒ void

This method returns an undefined value.

Restore a value YAML read, frozen, as Marshal restores one

Examples:

Read a value written as YAML

YAML.unsafe_load(YAML.dump(trend)).name

Parameters:

  • coder (Psych::Coder) —

    the coder YAML read the value with

Raises:



77
# File 'x-resources/lib/x/resources/value_marshalling.rb', line 77

def init_with(coder) = marshal_load(coder.map.values_at(*YAML_KEYS))

#marshal_dump ⇒ Array(Integer, Hash{String => Object})

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.

Examples:

Cache the trends of a place

Rails.cache.write("trends", X::Trend.at(1, client: client))

Returns:

  • (Array(Integer, Hash{String => Object})) —

    the number of the format, then the attributes



40
# File 'x-resources/lib/x/resources/value_marshalling.rb', line 40

def marshal_dump = [MARSHAL_FORMAT, attrs]

#marshal_load(state) ⇒ void

This method returns an undefined value.

Restore a value Marshal read, frozen as the value that was written was

Examples:

Read cached trends

Marshal.load(Marshal.dump(trend)).name

Parameters:

  • state (Array) —

    the state Marshal wrote

Raises:



50
51
52
53
54
55
# File 'x-resources/lib/x/resources/value_marshalling.rb', line 50

def marshal_load(state)
  format, attrs = state
  raise UnsupportedFormat, "#{self.class} reads format #{MARSHAL_FORMAT} of Marshal, not #{format.inspect}" unless MARSHAL_FORMAT.eql?(format)

  restore(attrs)
end