Current section
Files
Jump to
Current section
Files
lib/diffo/provider/resources/characteristic.ex
defmodule Diffo.Provider.Characteristic do
@moduledoc """
Diffo - TMF Service and Resource Management with a difference
Copyright Matt Beanland beanland@live.com.au
Characteristic - Ash Resource for a TMF Characteristic
"""
use Ash.Resource,
otp_app: :diffo,
domain: Diffo.Provider,
data_layer: AshNeo4j.DataLayer,
extensions: [AshOutstanding.Resource, AshJason.Resource]
neo4j do
label(:Characteristic)
relate([
{:instance, :CHARACTERISTIC_DEFINES_INSTANCE, :outgoing},
{:feature, :CHARACTERISTIC_DEFINES_FEATURE, :outgoing},
{:relationship, :CHARACTERISTIC_DEFINES_RELATIONSHIP, :outgoing}
])
guard([
{:CHARACTERISTIC_DEFINES_INSTANCE, :outgoing, :Instance},
{:CHARACTERISTIC_DEFINES_FEATURE, :outgoing, :Feature},
{:CHARACTERISTIC_DEFINES_RELATIONSHIP, :outgoing, :Relationship}
])
translate(id: :uuid)
skip([:instance_id, :feature_id, :relationship_id])
end
jason do
pick([:name, :value])
end
outstanding do
expect([:name, :value])
end
attributes do
uuid_primary_key :id do
description "a uuid4, unique to this characteristic, generated by default"
public? false
end
attribute :name, :atom do
description "the name of this characteristic"
allow_nil? false
public? true
end
attribute :value, :term do
description "the value of the characteristic"
allow_nil? true
public? true
end
attribute :type, :atom do
description "the type of the characteristic"
allow_nil? false
public? true
constraints one_of: [:instance, :feature, :relationship]
end
create_timestamp :inserted_at
update_timestamp :updated_at
end
relationships do
belongs_to :instance, Diffo.Provider.Instance do
description "the instance the characteristic defines"
allow_nil? true
public? true
end
belongs_to :feature, Diffo.Provider.Feature do
description "the feature the characteristic defines"
allow_nil? true
public? true
end
belongs_to :relationship, Diffo.Provider.Relationship do
description "the relationship the characteristic defines"
allow_nil? true
public? true
end
end
actions do
defaults [:destroy]
create :create do
description "creates a characteristic"
accept [:name, :value, :type]
end
read :read do
primary? true
end
read :find_by_name do
description "finds characteristics by name"
get? false
argument :query, :ci_string do
description "Return only characteristics with names including the given value."
end
filter expr(contains(name, ^arg(:query)))
end
read :list do
description "lists all characteristics"
end
update :update do
primary? true
description "updates the characteristic value or instance, feature or relationship"
accept [:value]
argument :instance_id, :uuid
argument :feature_id, :uuid
argument :relationship_id, :uuid
change manage_relationship(:instance_id, :instance, type: :append_and_remove)
change manage_relationship(:feature_id, :feature, type: :append_and_remove)
change manage_relationship(:relationship_id, :relationship, type: :append_and_remove)
end
end
identities do
identity :feature_characteristic_uniqueness, [:feature_id, :name] do
message "feature has duplicate characteristic"
pre_check? true
end
identity :instance_characteristic_uniqueness, [:instance_id, :name] do
message "instance has duplicate characteristic"
pre_check? true
end
identity :relationship_characteristic_uniqueness, [:relationship_id, :name] do
message "relationship has duplicate characteristic"
pre_check? true
end
end
preparations do
prepare build(sort: [name: :asc])
end
@doc """
Compares two characteristic, by ascending name
## Examples
iex> Diffo.Provider.Characteristic.compare(%{name: "a"}, %{name: "a"})
:eq
iex> Diffo.Provider.Characteristic.compare(%{name: "b"}, %{name: "a"})
:gt
iex> Diffo.Provider.Characteristic.compare(%{name: "a"}, %{name: "b"})
:lt
"""
def compare(%{name: name0}, %{name: name1}), do: Diffo.Util.compare(name0, name1)
end