Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate code for Struct/Data hash/== #110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions lib/literal/data_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def self.from_pack(payload)
object
end

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

alias eql? ==

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

def [](key)
instance_variable_get(:"@#{key}")
end
Expand Down Expand Up @@ -60,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
Loading