class Ably::Models::Stats::StatsStruct
StatsStruct
is a basic Struct like class that allows methods to be defined on the class that will be retuned co-erced objects from the underlying hash used to initialize the object.
This class provides a concise way to create classes that have fixed attributes and types
@example
class MessageCount < StatsStruct coerce_attributes :count, :data, into: Integer end
@api private
Attributes
Public Class Methods
Source
# File lib/ably/models/stats_types.rb, line 18 def coerce_attributes(*attributes) options = attributes.pop raise ArgumentError, 'Expected attribute into: within options hash' unless options.kind_of?(Hash) && options[:into] @type_klass = options[:into] setup_attribute_methods attributes end
Source
# File lib/ably/models/stats_types.rb, line 46 def initialize(hash) @hash = hash || {} end
Private Class Methods
Source
# File lib/ably/models/stats_types.rb, line 31 def setup_attribute_methods(attributes) attributes.each do |attr| define_method(attr) do # Lazy load the co-erced value only when accessed unless instance_variable_defined?("@#{attr}") instance_variable_set "@#{attr}", self.class.type_klass.new(hash[attr.to_sym]) end instance_variable_get("@#{attr}") end end end