Skip to content

Commit

Permalink
Generate code for Struct/Data hash/==
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Giddins <segiddins@segiddins.me>
  • Loading branch information
segiddins committed Oct 13, 2024
1 parent 35b4442 commit c26d51a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
19 changes: 7 additions & 12 deletions lib/literal/data_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ def self.from_pack(payload)
object
end

def ==(other)
if Literal::DataStructure === other
to_h == other.to_h
else
false
end
end

def hash
[self.class, to_h].hash
end

def [](key)
instance_variable_get(:"@#{key}")
end
Expand Down Expand Up @@ -58,4 +46,11 @@ def marshal_load(payload)
def marshal_dump
[1, to_h, frozen?]
end

def self.__generate_literal_methods__(new_property, buffer = +"")
super
literal_properties.generate_hash(buffer)
literal_properties.generate_eq("Literal::DataStructure", buffer)
buffer
end
end
9 changes: 5 additions & 4 deletions lib/literal/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ def __literal_property_class__
end

def __define_literal_methods__(new_property)
__literal_extension__.module_eval(
__generate_literal_methods__(new_property),
)
code = __generate_literal_methods__(new_property)
__literal_extension__.module_eval(code)
end

def __literal_extension__
if defined?(@__literal_extension__)
@__literal_extension__
else
@__literal_extension__ = Module.new
@__literal_extension__ = Module.new do
set_temporary_name "Literal::Properties(Extension)"
end
end
end

Expand Down
7 changes: 3 additions & 4 deletions lib/literal/properties/data_schema.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# frozen_string_literal: true

class Literal::Properties::DataSchema < Literal::Properties::Schema
def generate_initializer_body(buffer = +"")
buffer << "properties = self.class.literal_properties.properties_index\n"
generate_initializer_handle_properties(@sorted_properties, buffer)
buffer << "after_initialize if respond_to?(:after_initialize)\nfreeze\n"
def generate_after_initializer(buffer = +"")
super
buffer << " freeze\n"
end
end
41 changes: 38 additions & 3 deletions lib/literal/properties/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ def generate_initializer(buffer = +"")
"rescue Literal::TypeError => error\n" \
" error.set_backtrace(caller(2))\n" \
" raise\n" \
"else\n" \
" after_initialize if respond_to?(:after_initialize)\n" \
"end\n"
"else\n"
generate_after_initializer(buffer)
buffer << "end\n"
end

def generate_after_initializer(buffer = +"")
buffer << " after_initialize if respond_to?(:after_initialize)\n"
end

def generate_to_h(buffer = +"")
Expand All @@ -68,6 +72,37 @@ def generate_to_h(buffer = +"")
buffer << " }\n" << "end\n"
end

def generate_hash(buffer = +"")
buffer << "def hash\n [self.class,\n"

sorted_properties = @sorted_properties
i, n = 0, sorted_properties.size
while i < n
property = sorted_properties[i]
buffer << " @" << property.name.name << ",\n"
i += 1
end

buffer << " ].hash\n" << "end\n"
end

def generate_eq(guard, buffer = +"")
buffer << "def ==(other)\n"
buffer << " return false unless #{guard} === other\n"

sorted_properties = @sorted_properties
i, n = 0, sorted_properties.size
while i < n
property = sorted_properties[i]
buffer << " @" << property.name.name << " == other.#{property.escaped_name}"
buffer << " &&\n " if i < n - 1
i += 1
end
buffer << " true" if n.zero?
buffer << "\nend\n"
buffer << "alias eql? ==\n"
end

private

def generate_initializer_params(buffer = +"")
Expand Down

0 comments on commit c26d51a

Please sign in to comment.