Records, encapsulation and optional elements

Alex Peake apeake@REDACTED
Tue May 21 07:03:37 CEST 2002


I am new to Erlang. Could someone please advise if the following is a
reasonable attempt at encapsulating the creation of records (with optional
elements)? Is there a more elegant way? It seems a shame that I can define
defaults (in the .hrl) and yet not use them (in an encapsulated way) in the
.erl.

Thanks,

Alex


%% File: attr.hrl

%%-----------------------------------------------------------
%% Data Type: attr
%% where:
%%    name:  A string (default is undefined).
%%    domain:  An atom (default is undefined).
%%    domLen:   An integer (default is undefined).
%%    domPrec:   An integer (default is undefined).
%%    nullable: A boolean (default is false).
%%    isPk: A boolean (default is false).
%%    pkPos: An integer (default is undefined).
%%    fkPos: An integer (default is undefined).
%%    fkRel:  A string (default is undefined).
%%    fkAttr:  A string (default is undefined).
%%    autonum: A boolean (default is false).
%%------------------------------------------------------------

-record(attr, {name, domain, domLen = 0, domPrec = 0, nullable = false, isPk
= false, pkPos, fkPos, fkRel, fkAttr, autonum = false}).






-module(attr).
-include("attr.hrl").
-export([make_attr/3, varName/1, label/1]).
-export([findNamed/2, findNamedC/1, findFromNameList/2]).

%%-----------------------------------------------------------
%% Data Type: attr
%% where:
%%    name:  A string (default is undefined).
%%    domain:  An atom (default is undefined).
%%    domLen:   An integer (default is undefined).
%%    domPrec:   An integer (default is undefined).
%%    nullable: A boolean (default is false).
%%    isPk: A boolean (default is false).
%%    pkPos: An integer (default is undefined).
%%    fkPos: An integer (default is undefined).
%%    fkRel:  A string (default is undefined).
%%    fkAttr:  A string (default is undefined).
%%    autonum: A boolean (default is false).
%%------------------------------------------------------------


%% Name and Domain are required arguments.
%% Options is a list of Tuples for the rest.
%% Example: [{nullable, true},{isPK, true},{pkPos,1}]
make_attr(Name, Domain, Options) ->
    DomLen = case lists:keysearch(domLen, 1, Options) of
		 {value,{domLen, Value}} ->
		     Value;
		 _ ->
		     undefined
	     end,
    DomPrec = case lists:keysearch(domPrec, 1, Options) of
		  {value,{domPrec, Value}} ->
		      Value;
		  _ ->
		      undefined
	      end,
    Nullable = case lists:keysearch(nullable, 1, Options) of
		   {value,{nullable, Value}} ->
		       Value;
		   _ ->
		       false
	       end,
    IsPk = case lists:keysearch(isPk, 1, Options) of
	       {value,{isPk, Value}} ->
		   Value;
	       _ ->
		   false
	   end,
    PkPos = case lists:keysearch(pkPos, 1, Options) of
		{value,{pkPos, Value}} ->
		    Value;
		_ ->
		    undefined
	    end,
    FkPos = case lists:keysearch(fkPos, 1, Options) of
		{value,{fkPos, Value}} ->
		    Value;
		_ ->
		    undefined
	    end,
    FkRel = case lists:keysearch(fkRel, 1, Options) of
		{value,{fkRel, Value}} ->
		    Value;
		_ ->
		    undefined
	    end,
    FkAttr = case lists:keysearch(fkAttr, 1, Options) of
		 {value,{fkAttr, Value}} ->
		     Value;
		 _ ->
		     undefined
	     end,
    Autonum = case lists:keysearch(autonum, 1, Options) of
		  {value,{autonum, Value}} ->
		      Value;
		  _ ->
		      false
	      end,
    #attr{name = Name, domain = Domain, domLen = DomLen, domPrec = DomPrec,
	  nullable = Nullable, isPk = IsPk, pkPos = PkPos, fkPos = FkPos,
	  fkRel = FkRel, fkAttr = FkAttr, autonum = Autonum}.





More information about the erlang-questions mailing list