require 'fiona7/attribute_readers/binary_as_binary' require 'fiona7/attribute_readers/binary_as_linklist' require 'fiona7/attribute_readers/date_as_date' require 'fiona7/attribute_readers/html_as_html' require 'fiona7/attribute_readers/link_as_linklist' require 'fiona7/attribute_readers/linklist_as_linklist' require 'fiona7/attribute_readers/multienum_as_multienum' require 'fiona7/attribute_readers/multienum_as_text' require 'fiona7/attribute_readers/number_as_string' require 'fiona7/attribute_readers/reference_as_linklist' require 'fiona7/attribute_readers/reference_as_string' require 'fiona7/attribute_readers/referencelist_as_linklist' require 'fiona7/attribute_readers/referencelist_as_text' require 'fiona7/attribute_readers/simple' require 'fiona7/attribute_readers/stringlist_as_text' require 'fiona7/attribute_readers/widgetlist_as_linklist' module Fiona7 module AttributeReaders class Factory def initialize(obj, type_def, widget_resolver) self.obj = obj self.type_def = type_def self.widget_resolver = widget_resolver end def call(attribute, virtual_type, real_type) virtual = virtual_type.to_sym real = real_type.to_sym worker = case [virtual, real] when [:linklist, :linklist] Fiona7::AttributeReaders::LinklistAsLinklist when [:link, :linklist] Fiona7::AttributeReaders::LinkAsLinklist when [:reference, :linklist] Fiona7::AttributeReaders::ReferenceAsLinklist when [:reference, :string], [:reference, :text] Fiona7::AttributeReaders::ReferenceAsString when [:referencelist, :linklist] Fiona7::AttributeReaders::ReferencelistAsLinklist when [:referencelist, :text], [:referencelist, :string] Fiona7::AttributeReaders::ReferencelistAsText when [:widgetlist, :linklist] Fiona7::AttributeReaders::WidgetlistAsLinklist when [:multienum, :text], [:multienum, :string] Fiona7::AttributeReaders::MultienumAsText when [:multienum, :multienum] Fiona7::AttributeReaders::MultienumAsMultienum when [:number, :string], [:number, :text] Fiona7::AttributeReaders::NumberAsString when [:integer, :string], [:integer, :text] Fiona7::AttributeReaders::NumberAsString when [:float, :string], [:float, :text] Fiona7::AttributeReaders::NumberAsString when [:html, :html] Fiona7::AttributeReaders::HtmlAsHtml when [:binary, :linklist] Fiona7::AttributeReaders::BinaryAsLinklist when [:binary, :binary] Fiona7::AttributeReaders::BinaryAsBinary when [:stringlist, :text], [:stringlist, :string] Fiona7::AttributeReaders::StringlistAsText when [:stringlist, :multienum] Fiona7::AttributeReaders::Simple when [:stringlist, :stringlist] # NOTE: this is practically only used for "channels" Fiona7::AttributeReaders::Simple else case virtual when :text, :string, :enum, :markdown Fiona7::AttributeReaders::Simple when :date Fiona7::AttributeReaders::DateAsDate else # It would perhaps be practical to raise this error here # but also risky: broken configurations may surface # (attributes which had their attribute types changed). # This would break some pages # # raise(Fiona7::TypeSystemError.new("Unable to read #{attribute} in #{self.type_def.name} with typing #{virtual}:#{real}")) # # So instead, it is better to "lose" some information. return nil end end worker.new(self.obj, attribute, self.widget_resolver) end protected attr_accessor :obj, :obj_class, :type_def, :widget_resolver end end end