DevJoy.API.Item (DevJoy v2.0.0)

View Source

API for generating an implementation AST for the get_part_acc/3 and get_part_item/3 callbacks.

Item structs

defmodule MyLib.Person do
  use DevJoy.Scene

  defstruct ~w[:age, :name]a
end

defmodule MyLib.Item do
  use DevJoy.Scene

  defstruct ~w[:name]a
end

Item DSL

defmodule MyLib.Scene do
  defmacro person(name, age, do: items_block) do
    fields = [age: age, name: name]
    %DevJoy.API.Item.generate(MyLib.Person, fields, __CALLER__, ~w[name]a, items_block)
  end

  defmacro item(name) do
    fields = [name: name]
    %DevJoy.API.Item.generate(MyLib.Item, fields, __CALLER__, ~w[name]a)
  end
end

Item usage

defmodule MyApp.SceneName do
  use DevJoy.Scene

  import MyLib.Scene

  part :main do
    accumulate MyLib.Item, {nil, true, true}

    person "John Doe", 30 do
      item "ID card"
    end
  end
end

Item usage formatting

To ensure that the formatter would not add parentheses for your DSL add it to the locals_without_parens root option in the .formatter.exs file located in the project root. When creating a library please make sure to also export the same information

dsl = [item: :1, person: :3]

[
  export: [locals_without_parens: dsl],
  locals_without_parens: dsl
]

so it's possible to import it by:

[import_deps: [:my_lib]]

Item fetching

person = MyApp.SceneName.get_part_item(:main, 1)
acc = MyApp.SceneName.get_part_acc(:main, 1)
items = Map.get(acc, {MyLib.Item, nil}, [])

is_struct(person, MyLib.Person) and is_list(items) and length(list) == 1
# => true
person.name == "John Doe" and person.age == 30
# => true

item = hd(items)
is_struct(item, MyLib.Item) and item.name == "ID card"
# => true

Summary

Types

The type representing the item's fields that would be cast into the struct.

The type representing the list of the item's fields that would be translated.

The type representing the item's index in scene part (1-based).

The type representing the item's module name.

t()

The type representing the item's structure.

The type representing the item's structure to accumulate.

Functions

Uses get_quoted/3 to generate a compile-time escaped AST that is used to fetch the character at run-time.

Generates a compile-time escaped AST of the run-time call. The generated AST could be added to the fields/0 in the generate/5 call.

Types

fields()

@type fields() :: Keyword.t(DevJoy.Scene.macro_input(any()))

The type representing the item's fields that would be cast into the struct.

fields_to_translate()

@type fields_to_translate() :: [atom()]

The type representing the list of the item's fields that would be translated.

index()

@type index() :: pos_integer()

The type representing the item's index in scene part (1-based).

struct_module()

@type struct_module() ::
  DevJoy.Scene.Asset
  | DevJoy.Scene.Challenge
  | DevJoy.Scene.Dialog
  | DevJoy.Scene.Menu
  | DevJoy.Scene.Note
  | module()

The type representing the item's module name.

t()

The type representing the item's structure.

t_acc()

@type t_acc() :: t() | DevJoy.Scene.Choice.t()

The type representing the item's structure to accumulate.

Same as t/0 except that the DevJoy.Scene.Choice is always accumulated.

Functions

generate(struct_module, dsl_name, fields, caller, fields_to_translate \\ [], block \\ nil)

@spec generate(
  struct_module(),
  atom(),
  fields(),
  Macro.Env.t(),
  fields_to_translate(),
  Macro.input() | nil
) :: Macro.output()

Generates the implementation AST for the get_part_acc/3 and get_part_item/3 callbacks.

Usage

defmacro my_macro(value1, value2, do: block) do
  fields = [key1: value1, key2: value2]
  %DevJoy.API.Item.generate(ItemModuleNam, fields, __CALLER__, ~w[key1]a, block)
end

get_character(character_id, position \\ :left)

Uses get_quoted/3 to generate a compile-time escaped AST that is used to fetch the character at run-time.

Usage

position = :right
DevJoy.API.Item.get_character(:john_doe, position)

get_quoted(module, function_name, args)

@spec get_quoted(module(), atom(), [any(), ...]) :: Macro.output()

Generates a compile-time escaped AST of the run-time call. The generated AST could be added to the fields/0 in the generate/5 call.

Usage

defmacro my_macro do
  ast = DevJoy.API.Item.get_quoted(MyApp, :func_name, [])
  %DevJoy.API.Item.generate(ItemModuleNam, [key: ast], __CALLER__)
end

# generates
def get_part_item(:part_name, 1, "en") do
  %ItemModuleNam{
    key: MyApp.func_name()
  }
end