Module: Ruact::Serializable

Defined in:
lib/ruact/serializable.rb

Overview

Include this module in any Ruby object you want to pass as a prop to a client component. Declare which attributes are safe to serialize with rsc_props; only those attributes will be included in the wire payload.

Examples:

class Post
  include Ruact::Serializable
  attr_reader :id, :title, :secret
  rsc_props :id, :title   # :secret is never sent to the client
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
# File 'lib/ruact/serializable.rb', line 15

def self.included(base)
  base.extend(ClassMethods)
  base.instance_variable_set(:@rsc_props, [])
end

Instance Method Details

#rsc_serializeHash{String => Object}

Serialize only the attributes declared with rsc_props.

Returns:

  • (Hash{String => Object})


54
55
56
# File 'lib/ruact/serializable.rb', line 54

def rsc_serialize
  self.class.rsc_props_list.to_h { |attr| [attr.to_s, public_send(attr)] }
end