Skip to content

Commit

Permalink
Added ability to rename classes and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadeweka committed Feb 26, 2021
1 parent bf56c9d commit 355908b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ The term 'anyoli' means 'green' in the Maasai language, thus naming 'anyolite'.
#### Features

* [X] Support for enums
* [ ] Ability to rename classes and modules
* [X] Ability to rename classes and modules

#### Usability

Expand Down
4 changes: 2 additions & 2 deletions examples/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
a.x = 123
puts "Value getter returns #{a.x}"

ts = TestModule::TestStruct.new
ts = TestModule::TestStructRenamed.new
puts "Struct value: #{ts.value}"
puts "Struct test: #{ts.test.x}"

Expand All @@ -37,7 +37,7 @@
puts TestModule::Test.without_keywords(12)

# The absolute, ultimate and ridiculously complicated nesting test
TestModule::Test::UnderTest::DeepUnderTest::VeryDeepUnderTest.new.nested_test
TestModule::Test::UnderTestRenamed::DeepUnderTest::VeryDeepUnderTest.new.nested_test

struct_test_var = TestModule::Test::DeepTestStruct.new
puts "Struct test var: #{struct_test_var}"
Expand Down
22 changes: 20 additions & 2 deletions src/MrbWrap.cr
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ module MrbWrap
# to the second argument if wrapped.
annotation RenameConstant; end

# Renames the class to the first argument if wrapped.
annotation RenameClass; end

# Renames the module to the first argument if wrapped.
annotation RenameModule; end

# Wraps all arguments of the function to positional arguments.
annotation WrapWithoutKeywords; end

Expand Down Expand Up @@ -287,7 +293,13 @@ module MrbWrap
{% puts ">>> Going into class #{crystal_class} under #{under}\n\n" %}
{% end %}

MrbWrap.wrap_class({{mrb_state}}, {{crystal_class.resolve}}, "{{crystal_class.names.last}}", under: {{under}})
{% if crystal_class.resolve.annotation(MrbWrap::RenameClass) %}
{% actual_name = crystal_class.resolve.annotation(MrbWrap::RenameClass)[0] %}
{% else %}
{% actual_name = crystal_class.names.last.stringify %}
{% end %}

MrbWrap.wrap_class({{mrb_state}}, {{crystal_class.resolve}}, {{actual_name}}, under: {{under}})

MrbMacro.wrap_all_instance_methods({{mrb_state}}, {{crystal_class}}, {{instance_method_exclusions}}, {{verbose}}, context: {{under}}, use_enum_constructor: {{use_enum_constructor}})
MrbMacro.wrap_all_class_methods({{mrb_state}}, {{crystal_class}}, {{class_method_exclusions}}, {{verbose}}, context: {{under}})
Expand All @@ -312,7 +324,13 @@ module MrbWrap
{% puts ">>> Going into module #{crystal_module} under #{under}\n\n" %}
{% end %}

MrbWrap.wrap_module({{mrb_state}}, {{crystal_module.resolve}}, "{{crystal_module.names.last}}", under: {{under}})
{% if crystal_module.resolve.annotation(MrbWrap::RenameModule) %}
{% actual_name = crystal_module.resolve.annotation(MrbWrap::RenameModule)[0] %}
{% else %}
{% actual_name = crystal_module.names.last.stringify %}
{% end %}

MrbWrap.wrap_module({{mrb_state}}, {{crystal_module.resolve}}, {{actual_name}}, under: {{under}})
MrbMacro.wrap_all_class_methods({{mrb_state}}, {{crystal_module}}, {{class_method_exclusions}}, {{verbose}}, context: {{under}})
MrbMacro.wrap_all_constants({{mrb_state}}, {{crystal_module}}, {{constant_exclusions}}, {{verbose}})
end
Expand Down
2 changes: 2 additions & 0 deletions test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module SomeModule
puts "#{str} and #{int}"
end

@[MrbWrap::RenameClass("TestStructRenamed")]
struct TestStruct
property value : Int32 = -123
property test : Test = Test.new(-234)
Expand All @@ -23,6 +24,7 @@ module SomeModule
@[MrbWrap::SpecializeInstanceMethod(method_with_various_args, nil)]
class Test

@[MrbWrap::RenameClass("UnderTestRenamed")]
class UnderTest
module DeepUnderTest
class VeryDeepUnderTest
Expand Down

0 comments on commit 355908b

Please sign in to comment.