Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Refactor FileModule into File.Module
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Oct 12, 2017
1 parent e344287 commit 4a36bc7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 31 deletions.
3 changes: 1 addition & 2 deletions lib/software/internal/file.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Helix.Software.Internal.File do

alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule
alias Helix.Software.Model.Storage
alias Helix.Software.Repo

Expand Down Expand Up @@ -121,7 +120,7 @@ defmodule Helix.Software.Internal.File do
update(file, params)
end

@spec encrypt(File.t, FileModule.version) ::
@spec encrypt(File.t, File.Module.version) ::
{:ok, File.changeset}
| {:error, File.changeset}
def encrypt(file = %File{}, version) when version >= 1 do
Expand Down
22 changes: 11 additions & 11 deletions lib/software/model/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ defmodule Helix.Software.Model.File do

alias Ecto.Changeset
alias HELL.Constant
alias Helix.Software.Model.FileModule
alias Helix.Software.Model.Software
alias Helix.Software.Model.Storage
alias __MODULE__, as: File

@type t :: t_of_type(Software.type)

Expand All @@ -26,7 +26,7 @@ defmodule Helix.Software.Model.File do
storage: term,
inserted_at: NaiveDateTime.t,
updated_at: NaiveDateTime.t,
modules: modules | FileModule.schema,
modules: modules | File.Module.schema,
crypto_version: crypto_version
}

Expand All @@ -37,7 +37,7 @@ defmodule Helix.Software.Model.File do
@type size :: pos_integer
@type type :: Software.type
@type crypto_version :: nil | pos_integer
@type modules :: FileModule.t
@type modules :: File.Module.t

@type changeset :: %Changeset{data: %__MODULE__{}}

Expand All @@ -49,7 +49,7 @@ defmodule Helix.Software.Model.File do
storage_id: Storage.idtb
}

@type module_params :: {FileModule.name, FileModule.Data.t}
@type module_params :: {File.Module.name, File.Module.Data.t}

@type update_params :: %{
optional(:name) => name,
Expand Down Expand Up @@ -88,7 +88,7 @@ defmodule Helix.Software.Model.File do
references: :storage_id,
define_field: false

has_many :modules, FileModule,
has_many :modules, File.Module,
foreign_key: :file_id,
references: :file_id,
on_replace: :delete
Expand Down Expand Up @@ -119,7 +119,7 @@ defmodule Helix.Software.Model.File do
def format(file) do
formatted_modules =
Enum.reduce(file.modules, %{}, fn module, acc ->
module = FileModule.format(module)
module = File.Module.format(module)
Map.merge(acc, module)
end)

Expand Down Expand Up @@ -158,17 +158,17 @@ defmodule Helix.Software.Model.File do
end

@spec create_module_assoc(module_params) ::
FileModule.changeset
File.Module.changeset
docp """
Helper/wrapper to `FileModule.create_changeset/1`
Helper/wrapper to `File.Module.create_changeset/1`
"""
defp create_module_assoc({name, data}) do
params = %{
name: name,
version: data.version
}

FileModule.create_changeset(params)
File.Module.create_changeset(params)
end

docp """
Expand Down Expand Up @@ -251,13 +251,13 @@ defmodule Helix.Software.Model.File do
do: where(query, [f], is_nil(f.crypto_version))

defp join_modules(query),
do: join(query, :left, [f], fm in FileModule, fm.file_id == f.file_id)
do: join(query, :left, [f], fm in File.Module, fm.file_id == f.file_id)

defp join_assoc_modules(query),
do: join(query, :left, [f], fm in assoc(f, :modules))

docp """
Preloads FileModules into the schema
Preloads File.Modules into the schema
"""
defp preload_modules(query),
do: preload(query, [..., m], [modules: m])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Helix.Software.Model.FileModule do
defmodule Helix.Software.Model.File.Module do
@moduledoc """
A FileModule is a component of a File responsible for doing something. It
A File.Module is a component of a File responsible for doing something. It
contains a version, which is a representation of how powerful that module is.
For example, take the Cracker. It may have Overflow and Bruteforce modules.
Expand All @@ -18,11 +18,11 @@ defmodule Helix.Software.Model.FileModule do
alias Ecto.Changeset
alias HELL.Constant
alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule.Data, as: FileModuleData
alias Helix.Software.Model.Software
alias __MODULE__, as: Module

@type t :: %{
name => FileModuleData.t
name => Module.Data.t
}

@type schema :: %__MODULE__{
Expand Down Expand Up @@ -96,7 +96,7 @@ defmodule Helix.Software.Model.FileModule do
Formats a FileModule
"""
def format(module = %__MODULE__{}) do
data = FileModuleData.new(module)
data = Module.Data.new(module)

Map.put(%{}, module.name, data)
end
Expand All @@ -106,17 +106,17 @@ defmodule Helix.Software.Model.FileModule do
FileModuleData contains information about the corresponding module.
"""

alias Helix.Software.Model.FileModule
alias Helix.Software.Model.File

@type t ::
%__MODULE__{
version: FileModule.version
version: File.Module.version
}

@enforce_keys [:version]
defstruct [:version]

@spec new(FileModule.schema) ::
@spec new(File.Module.schema) ::
t
def new(%{version: version}) do
%__MODULE__{
Expand All @@ -132,16 +132,15 @@ defmodule Helix.Software.Model.FileModule do
alias Ecto.Queryable
alias HELL.Constant
alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule

@spec by_file(Queryable.t, File.idtb) ::
Queryable.t
def by_file(query \\ FileModule, id),
def by_file(query \\ File.Module, id),
do: where(query, [fm], fm.file_id == ^id)

@spec by_name(Queryable.t, Constant.t) ::
Queryable.t
def by_name(query \\ FileModule, name),
def by_name(query \\ File.Module, name),
do: where(query, [fm], fm.name == ^name)
end
end
5 changes: 2 additions & 3 deletions lib/software/query/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Helix.Software.Query.File do
alias Helix.Cache.Query.Cache, as: CacheQuery
alias Helix.Server.Model.Server
alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule
alias Helix.Software.Model.Storage
alias Helix.Software.Internal.File, as: FileInternal

Expand All @@ -13,7 +12,7 @@ defmodule Helix.Software.Query.File do
defdelegate fetch(file_id),
to: FileInternal

@spec fetch_best(Server.id, FileModule.name) ::
@spec fetch_best(Server.id, File.Module.name) ::
File.t
| nil
@doc """
Expand All @@ -28,7 +27,7 @@ defmodule Helix.Software.Query.File do
fetch_best(List.first(storages), module)
end

@spec fetch_best(Storage.t, FileModule.name) ::
@spec fetch_best(Storage.t, File.Module.name) ::
File.t
| nil
@doc """
Expand Down
3 changes: 1 addition & 2 deletions lib/software/software.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ defmodule Helix.Software do

alias HELL.Constant
alias Helix.Software.Model.File
alias Helix.Software.Model.FileModule

@type t :: %{
type: type,
Expand All @@ -65,7 +64,7 @@ defmodule Helix.Software do
@type modules :: Constant.t

@type extension :: File.extension
@type module_name :: FileModule.name
@type module_name :: File.Module.name

@spec all ::
[t]
Expand Down
4 changes: 2 additions & 2 deletions test/support/software/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Helix.Test.Software.Helper do

alias Helix.Cache.Query.Cache, as: CacheQuery
alias Helix.Server.Model.Server
alias Helix.Software.Model.FileModule
alias Helix.Software.Model.File
alias Helix.Software.Model.Software
alias Helix.Software.Model.Storage
alias Helix.Software.Query.Storage, as: StorageQuery
Expand Down Expand Up @@ -67,7 +67,7 @@ defmodule Helix.Test.Software.Helper do
end

defp generate_file_module(module, version) do
data = FileModule.Data.new(%{version: version})
data = File.Module.Data.new(%{version: version})

Map.put(%{}, module, data)
end
Expand Down

0 comments on commit 4a36bc7

Please sign in to comment.