hash = { foo: "bar" } serialized_hash = LiveComponent.serializer.serialize(hash) deserialized_hash = LiveComponent.serializer.deserialize(serialized_hash) deserialized_hash == hash # returns true
hash = { foo: "bar" } serialized_hash = LiveComponent.serializer.serialize(hash) deserialized_hash = LiveComponent.serializer.deserialize(serialized_hash) deserialized_hash == hash # returns true
class RangeSerializer < LiveComponent::ObjectSerializer private def object_to_hash(range) { "begin" => LiveComponent.serializer.serialize(range.begin), "end" => LiveComponent.serializer.serialize(range.end), "exclude_end" => range.exclude_end?, } end def hash_to_object(hash) Range.new( *LiveComponent.serializer.deserialize( [hash["begin"], hash["end"]] ), hash["exclude_end"] ) end end
class RangeSerializer < LiveComponent::ObjectSerializer private def object_to_hash(range) { "begin" => LiveComponent.serializer.serialize(range.begin), "end" => LiveComponent.serializer.serialize(range.end), "exclude_end" => range.exclude_end?, } end def hash_to_object(hash) Range.new( *LiveComponent.serializer.deserialize( [hash["begin"], hash["end"]] ), hash["exclude_end"] ) end end
LiveComponent.serializer.register(Range, RangeSerializer)
LiveComponent.serializer.register(Range, RangeSerializer)
LiveComponent.serializer.serialize(1..2) # returns {"_lc_ser" => "Range", "begin" => 1, "end" => 2, "exclude_end" => false}
LiveComponent.serializer.serialize(1..2) # returns {"_lc_ser" => "Range", "begin" => 1, "end" => 2, "exclude_end" => false}
class BooleanSerializer < LiveComponent::ObjectSerializer private def object_to_hash(object) { "value" => object ? "t" : "f" } end def hash_to_object(hash) hash["value"] == "t" end end LiveComponent.register_prop_serializer(:boolean_serializer, BooleanSerializer) class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :example_arg, with: :boolean_serializer def initialize(example_arg: false) @example_arg = example_arg end end
class BooleanSerializer < LiveComponent::ObjectSerializer private def object_to_hash(object) { "value" => object ? "t" : "f" } end def hash_to_object(hash) hash["value"] == "t" end end LiveComponent.register_prop_serializer(:boolean_serializer, BooleanSerializer) class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :example_arg, with: :boolean_serializer def initialize(example_arg: false) @example_arg = example_arg end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base # serializes only the foo and bar attributes serializes :model_object, with: :model_serializer, attributes: [:foo, :bar] # serializes all attributes (the default) serializes :model_object, with: :model_serializer, attributes: true # serializes no attributes serializes :model_object, with: :model_serializer, attributes: false def initialize(model_object:) @model_object = model_object end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base # serializes only the foo and bar attributes serializes :model_object, with: :model_serializer, attributes: [:foo, :bar] # serializes all attributes (the default) serializes :model_object, with: :model_serializer, attributes: true # serializes no attributes serializes :model_object, with: :model_serializer, attributes: false def initialize(model_object:) @model_object = model_object end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :model_object, with: :model_serializer, sign: false def initialize(model_object:) @model_object = model_object end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :model_object, with: :model_serializer, sign: false def initialize(model_object:) @model_object = model_object end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :model_object, with: :model_serializer, reload: true def initialize(model_object:) @model_object = model_object end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :model_object, with: :model_serializer, reload: true def initialize(model_object:) @model_object = model_object end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :example_arg do |serializer| serializer.serialize do |object| { "value" => object ? "t" : "f" } end serializer.deserialize do |hash| hash["value"] == "t" end end def initialize(example_arg: false) @example_arg = example_arg end end
class ExampleComponent < ApplicationComponent include LiveComponent::Base serializes :example_arg do |serializer| serializer.serialize do |object| { "value" => object ? "t" : "f" } end serializer.deserialize do |hash| hash["value"] == "t" end end def initialize(example_arg: false) @example_arg = example_arg end end
type RubySymbol = { value: string; _lc_sym: true; }
type RubySymbol = { value: string; _lc_sym: true; }
import { type RubySymbol, Ruby } from "@camertron/live-component"; // these are equivalent const sym1: RubySymbol = { _lc_sym: true, value: "foo" }; const sym2 = Ruby.make_symbol("foo");
import { type RubySymbol, Ruby } from "@camertron/live-component"; // these are equivalent const sym1: RubySymbol = { _lc_sym: true, value: "foo" }; const sym2 = Ruby.make_symbol("foo");
type RecordProxy<T> = { _lc_ar: { gid: string; // the record's global ID signed: boolean; // whether or not the global ID is cryptographically signed } } & T
type RecordProxy<T> = { _lc_ar: { gid: string; // the record's global ID signed: boolean; // whether or not the global ID is cryptographically signed } } & T
import { type RecordProxy, live, LiveController } from "@camertron/live-component"; export type TodoItem = RecordProxy<{ name: string; }> export type TodoItemComponentProps = { todo_item: TodoItem; // ...etc } @live("TodoItemComponent") export class TodoItemComponent extends LiveComponent<TodoItemComponentProps> { // ...etc }
import { type RecordProxy, live, LiveController } from "@camertron/live-component"; export type TodoItem = RecordProxy<{ name: string; }> export type TodoItemComponentProps = { todo_item: TodoItem; // ...etc } @live("TodoItemComponent") export class TodoItemComponent extends LiveComponent<TodoItemComponentProps> { // ...etc }
export type RubyHash<T extends Record<string, any>> = { _lc_symkeys: Array<string>; } & T export type RubySymbolHash<T extends Record<string, any>> = { _lc_symhash: true } & T export type RubyHashWithIndifferentAccess<T extends Record<string, any>> = RubyHash<T> & { _lc_hwia: true; }
export type RubyHash<T extends Record<string, any>> = { _lc_symkeys: Array<string>; } & T export type RubySymbolHash<T extends Record<string, any>> = { _lc_symhash: true } & T export type RubyHashWithIndifferentAccess<T extends Record<string, any>> = RubyHash<T> & { _lc_hwia: true; }
import { type RubyHash, type RubySymbolHash, type RubyHashWithIndifferentAccess, Ruby } from "@camertron/live-component"; // these are equivalent const hash1: RubyHash = { _lc_symkeys: [] }; const hash2 = Ruby.make_hash(); // these are equivalent const hash1: RubySymbolHash = { _lc_symhash: true }; const hash2 = Ruby.make_symbol_hash(); // these are equivalent const hash1: RubyHashWithIndifferentAccess = { _lc_symkeys: [], _lc_hwia: true }; const hash2 = Ruby.make_hash_with_indifferent_access();
import { type RubyHash, type RubySymbolHash, type RubyHashWithIndifferentAccess, Ruby } from "@camertron/live-component"; // these are equivalent const hash1: RubyHash = { _lc_symkeys: [] }; const hash2 = Ruby.make_hash(); // these are equivalent const hash1: RubySymbolHash = { _lc_symhash: true }; const hash2 = Ruby.make_symbol_hash(); // these are equivalent const hash1: RubyHashWithIndifferentAccess = { _lc_symkeys: [], _lc_hwia: true }; const hash2 = Ruby.make_hash_with_indifferent_access();
import { Ruby } from "@camertron/live-component"; const hash = Ruby.make_hash(); // set a value of "bar" at (string) key "foo" Ruby.hash_set(hash, "foo", "bar"); // set a value of "bar" at (symbol) key "foo" // these two are equivalent Ruby.hash_set(hash, Ruby.make_symbol("foo"), "bar"); Ruby.hash_set_symbol(hash, "foo", "bar");
import { Ruby } from "@camertron/live-component"; const hash = Ruby.make_hash(); // set a value of "bar" at (string) key "foo" Ruby.hash_set(hash, "foo", "bar"); // set a value of "bar" at (symbol) key "foo" // these two are equivalent Ruby.hash_set(hash, Ruby.make_symbol("foo"), "bar"); Ruby.hash_set_symbol(hash, "foo", "bar");
import { Ruby } from "@camertron/live-component"; const hash = Ruby.make_hash(); Ruby.hash_set_symbol(hash, "foo", "bar"); // returns undefined Ruby.hash_get(hash, "foo"); // returns "bar" Ruby.hash_get_symbol(hash, "foo");
import { Ruby } from "@camertron/live-component"; const hash = Ruby.make_hash(); Ruby.hash_set_symbol(hash, "foo", "bar"); // returns undefined Ruby.hash_get(hash, "foo"); // returns "bar" Ruby.hash_get_symbol(hash, "foo");
import { Ruby } from "@camertron/live-component"; const hash = Ruby.make_hash(); Ruby.hash_set_symbol(hash, "foo", "bar"); // returns undefined, does nothing Ruby.hash_delete(hash, "foo"); // returns "bar", deletes "foo" from the hash Ruby.hash_delete_symbol(hash, "foo");
import { Ruby } from "@camertron/live-component"; const hash = Ruby.make_hash(); Ruby.hash_set_symbol(hash, "foo", "bar"); // returns undefined, does nothing Ruby.hash_delete(hash, "foo"); // returns "bar", deletes "foo" from the hash Ruby.hash_delete_symbol(hash, "foo");
import { Ruby } from "@camertron/live-component"; // returns a hash where all keys are strings Ruby.object_to_hash({ foo: "bar" }); // returns a hash where all keys are symbols Ruby.object_to_symbol_hash({ foo: "bar" });
import { Ruby } from "@camertron/live-component"; // returns a hash where all keys are strings Ruby.object_to_hash({ foo: "bar" }); // returns a hash where all keys are symbols Ruby.object_to_symbol_hash({ foo: "bar" });