Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
C
CommonsPub Federated Server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
10
Issues
10
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
1
Merge Requests
1
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Test Cases
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bonfire
CommonsPub Federated Server
Commits
e5ee33bf
Commit
e5ee33bf
authored
Nov 20, 2020
by
Mayel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve modularity hacks
parent
6c0b5a87
Pipeline
#218991387
failed with stage
in 1 minute and 15 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
95 additions
and
56 deletions
+95
-56
lib/mixology/modularity/module_extend.ex
lib/mixology/modularity/module_extend.ex
+21
-11
lib/mixology/modularity/module_override.ex
lib/mixology/modularity/module_override.ex
+35
-5
lib/mixology/modularity/sample_extend.ex
lib/mixology/modularity/sample_extend.ex
+11
-0
lib/mixology/modularity/sample_override.ex
lib/mixology/modularity/sample_override.ex
+28
-0
lib/mixology/simulation/simulation_custom.ex
lib/mixology/simulation/simulation_custom.ex
+0
-6
lib/mixology/simulation/simulation_override_attempt1.ex
lib/mixology/simulation/simulation_override_attempt1.ex
+0
-34
No files found.
lib/mixology/modularity/module_extend.ex
View file @
e5ee33bf
defmodule
Modul
e
Extend
do
defmodule
Modul
arity
.
Module
.
Extend
do
defmacro
extends
(
module
)
do
require
Logger
module
=
Macro
.
expand
(
module
,
__CALLER__
)
Logger
.
info
(
"[Modularity.Module.Extend] Extending module
#{
inspect
(
module
)
}
"
)
functions
=
module
.
__info__
(
:functions
)
signatures
=
Enum
.
map
functions
,
fn
{
name
,
arity
}
->
args
=
if
arity
==
0
do
[]
else
Enum
.
map
1
..
arity
,
fn
(
i
)
->
{
String
.
to_atom
(
<<
?x
,
?A
+
i
-
1
>>
),
[],
nil
}
end
end
{
name
,
[],
args
}
end
signatures
=
Enum
.
map
(
functions
,
fn
{
name
,
arity
}
->
args
=
if
arity
==
0
do
[]
else
Enum
.
map
(
1
..
arity
,
fn
i
->
{
String
.
to_atom
(
<<
?x
,
?A
+
i
-
1
>>
),
[],
nil
}
end
)
end
{
name
,
[],
args
}
end
)
zipped
=
List
.
zip
([
signatures
,
functions
])
for
sig_func
<-
zipped
do
quote
do
defdelegate
unquote
(
elem
(
sig_func
,
0
)),
to:
unquote
(
module
)
...
...
lib/mixology/modularity/module_override.ex
View file @
e5ee33bf
defmodule
ModuleOverride
do
defmodule
Modularity
.
Module
.
Override
do
@moduledoc
"""
Utility to clone a module under a new name
"""
require
Logger
@doc
"""
Clone the existing module under a new name
"""
def
archive_module
(
module_source_file
,
extending_module
)
do
Code
.
ensure_compiled
(
extending_module
)
def
clone
(
old_module
,
new_module
)
when
is_atom
(
old_module
)
do
Logger
.
info
(
"[Modularity.Module.Override] Cloning module
#{
module_name_string
(
old_module
)
}
as
#{
module_name_string
(
new_module
)
}
"
)
with
{
:ok
,
f
}
<-
File
.
read
(
module_source_file
)
do
Code
.
eval_string
(
String
.
replace
(
f
,
"defmodule "
,
"defmodule ModuleOverride."
))
with
{
:module
,
_module
}
<-
Code
.
ensure_compiled
(
old_module
),
module_source_file
=
old_module
.
module_info
()[
:compile
][
:source
],
{
:ok
,
f
}
<-
File
.
read
(
module_source_file
)
do
Code
.
eval_string
(
String
.
replace
(
f
,
"defmodule
#{
module_name_string
(
old_module
)
}
"
,
"defmodule
#{
module_name_string
(
new_module
)
}
"
)
)
# returns name of archived module
_new_module
=
String
.
to_existing_atom
(
"Elixir.
#{
module_name_string
(
new_module
)
}
"
)
else
e
->
Logger
.
error
(
"Could not find source of module
#{
old_module
}
:
#{
inspect
(
e
)
}
"
)
nil
end
end
def
clone
(
old_module
,
prefix
)
when
is_binary
(
old_module
),
do
:
clone
(
String
.
to_existing_atom
(
old_module
),
prefix
)
def
module_name_string
(
module
),
do
:
String
.
replace
(
"
#{
module
}
"
,
"Elixir."
,
""
)
end
lib/mixology/modularity/sample_extend.ex
0 → 100644
View file @
e5ee33bf
defmodule
CommonsPub
.
Utils
.
TrendyExtended
do
require
Modularity
.
Module
.
Extend
Modularity
.
Module
.
Extend
.
extends
CommonsPub
.
Utils
.
Trendy
def
some
(
count_or_range
\\
1
,
fun
)
do
require
Logger
Logger
.
info
(
"Selecting
#{
count_or_range
}
random thing(s) returned by function
#{
inspect
fun
}
"
)
# call function from original module:
super
(
count_or_range
,
fun
)
end
end
lib/mixology/
simulation/simulation
_override.ex
→
lib/mixology/
modularity/sample
_override.ex
View file @
e5ee33bf
alias
CommonsPub
.
Utils
.
Simulation
,
as:
ExtendingModule
alias
ModuleOverride
.
CommonsPub
.
Utils
.
Simulation
,
as:
ArchiveModule
# module that we will override
alias
CommonsPub
.
Utils
.
Simulation
,
as:
NewModule
# new name for the old module
alias
Original
.
CommonsPub
.
Utils
.
Simulation
,
as:
ArchiveModule
ModuleOverride
.
archive_module
(
Path
.
dirname
(
__ENV__
.
file
)
<>
"/simulation.ex"
,
ExtendingModule
)
# archive the old module
Modularity
.
Module
.
Override
.
clone
(
NewModule
,
ArchiveModule
)
defmodule
ExtendingModule
do
require
ModuleExtend
ModuleExtend
.
extends
(
ArchiveModule
)
defmodule
NewModule
do
require
Modularity
.
Module
.
Extend
@moduledoc
"""
(Re)define new or existing functions
"""
# extend the archived module
Modularity
.
Module
.
Extend
.
extends
ArchiveModule
####
# (Re)define new or existing functions
####
# example of straight up replacing a function
def
name
(),
do
:
Faker
.
Person
.
last_name
()
...
...
lib/mixology/simulation/simulation_custom.ex
deleted
100644 → 0
View file @
6c0b5a87
# defmodule CommonsPub.Utils.SimulationCustom do
# require ExtendModule
# ExtendModule.extends CommonsPub.Utils.Simulation
# def location(), do: Faker.Address.country()
# end
lib/mixology/simulation/simulation_override_attempt1.ex
deleted
100644 → 0
View file @
6c0b5a87
# require Logger
# alias CommonsPub.Utils.Simulation, as: Mod
# defmodule ModuleOrigin.Simulation do
# @moduledoc """
# Clone the existing module under a new name
# """
# Code.ensure_compiled(Mod)
# for {func, arity} <- Mod.__info__(:functions) do
# args = Macro.generate_arguments(arity, __MODULE__)
# def unquote(func)(unquote_splicing(args)) do
# Mod.unquote(func)(unquote_splicing(args))
# rescue
# Mod.Error -> handle_error()
# end
# end
# def handle_error(), do: Logger.info("Error while overiding a module")
# end
# defmodule CommonsPub.Utils.Simulation do
# @moduledoc """
# (Re)define functions
# """
# require ModuleExtend
# ModuleExtend.extends ModuleOrigin.Simulation
# def location(), do: Faker.Address.country()
# end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment