require 'fiona7/attribute_writers/binary_as_binary' require 'fiona7/attribute_writers/binary_as_linklist' require 'fiona7/attribute_writers/html_as_html' require 'fiona7/attribute_writers/link_as_linklist' require 'fiona7/attribute_writers/linklist_as_linklist' require 'fiona7/attribute_writers/multienum_as_multienum' require 'fiona7/attribute_writers/multienum_as_text' require 'fiona7/attribute_writers/number_as_string' require 'fiona7/attribute_writers/reference_as_linklist' require 'fiona7/attribute_writers/reference_as_string' require 'fiona7/attribute_writers/referencelist_as_linklist' require 'fiona7/attribute_writers/referencelist_as_text' require 'fiona7/attribute_writers/simple' require 'fiona7/attribute_writers/date_as_date' require 'fiona7/attribute_writers/stringlist_as_text' require 'fiona7/attribute_writers/widgetlist_as_linklist' module Fiona7 module AttributeWriters class Factory def initialize(obj, obj_class, klass, widget_map) self.obj = obj self.obj_class = obj_class self.klass = klass self.widget_map = widget_map self.type_def = Fiona7::TypeRegister.instance.write(self.obj_class) || raise(Fiona7::TypeSystemError.new("Obj class definition not found for #{self.obj_class}")) end def call(attribute) attr_def = self.type_def.find_attribute(attribute) || raise(Fiona7::TypeSystemError.new("Attribute #{attribute} not found in #{self.obj_class}")) virtual = attr_def.type.to_sym real = attr_def.real_type.to_sym worker = case [virtual, real] when [:linklist, :linklist] Fiona7::AttributeWriters::LinklistAsLinklist when [:link, :linklist] Fiona7::AttributeWriters::LinkAsLinklist when [:reference, :linklist] Fiona7::AttributeWriters::ReferenceAsLinklist when [:reference, :string], [:reference, :text] Fiona7::AttributeWriters::ReferenceAsString when [:referencelist, :linklist] Fiona7::AttributeWriters::ReferencelistAsLinklist when [:referencelist, :text], [:referencelist, :string] Fiona7::AttributeWriters::ReferencelistAsText when [:widgetlist, :linklist] Fiona7::AttributeWriters::WidgetlistAsLinklist when [:multienum, :text], [:multienum, :string] Fiona7::AttributeWriters::MultienumAsText when [:multienum, :multienum] Fiona7::AttributeWriters::MultienumAsMultienum when [:number, :string], [:number, :text] Fiona7::AttributeWriters::NumberAsString when [:integer, :string], [:integer, :text] Fiona7::AttributeWriters::NumberAsString when [:float, :string], [:float, :text] Fiona7::AttributeWriters::NumberAsString when [:html, :html] Fiona7::AttributeWriters::HtmlAsHtml when [:binary, :linklist] Fiona7::AttributeWriters::BinaryAsLinklist when [:binary, :binary] Fiona7::AttributeWriters::BinaryAsBinary when [:stringlist, :text], [:stringlist, :string] Fiona7::AttributeWriters::StringlistAsText when [:stringlist, :multienum] Fiona7::AttributeWriters::Simple when [:stringlist, :stringlist] # NOTE: this is practically only used for "channels" Fiona7::AttributeWriters::Simple else case virtual when :text, :string, :enum, :markdown Fiona7::AttributeWriters::Simple when :date Fiona7::AttributeWriters::DateAsDate else raise(Fiona7::TypeSystemError.new("Unable to write #{attribute} in #{self.obj_class} with typing #{virtual}:#{real}")) end end worker.new(self.obj, attr_def.real_name.to_s, self.obj_class, self.klass, self.widget_map) end protected attr_accessor :obj, :obj_class, :klass, :widget_map, :type_def end end end