module RailsConnector class ObjDataFromDatabase < ObjData def initialize(data, revision) @rtc_ref = data["rtc_ref"] @values = data["values"] @ext_ref = data["ext_ref"] @revision = revision end def value_and_type_of(attribute_name) if internal_attribute?(attribute_name) attribute_type = type_of_internal(attribute_name) attribute_value = read_value(attribute_name) if attribute_name == '_text_links' attribute_value = attribute_value && attribute_value.values elsif attribute_name == '_path' attribute_value = PathConversion.path_from_list(attribute_value) end else attribute_type = current_attribute_type(attribute_name) if legacy_attribute_type(attribute_name) == attribute_type attribute_value = read_value(attribute_name) else attribute_value = default_attribute_value(attribute_type) end end [attribute_value, attribute_type] end def has_custom_attribute?(name) obj_class_attributes.include?(name) end def all_custom_attributes obj_class_attributes end private attr_reader :values, :rtc_ref, :revision def current_attribute_type(name) revision.attributes[name]["type"] end def obj_class_attributes @obj_class_attributes ||= begin name = values["_obj_class"] if data = revision.obj_classes[name] Set.new(data["attributes"]) || Set.new else raise "Could not find ObjClass with name #{name} in Revision #{revision.id}!" end end end def read_value(attribute_name) return values[attribute_name] if values.key?(attribute_name) if @ext_ref && (attribute_name == "_text_links" || attribute_name[0] != ?_) extend_values_with_dict_storage_values end values[attribute_name] end def extend_values_with_dict_storage_values return unless @ext_ref # may raise Kvom::Storage::NotFound ext_values = DictStorage.get(@ext_ref) values.reverse_merge!(ext_values) @ext_ref = nil end SPECIAL_INTERNAL_ATTRIBUTES = Set.new(%w[title body blob]) def internal_attribute?(attribute_name) ?_ == attribute_name[0] || SPECIAL_INTERNAL_ATTRIBUTES.include?(attribute_name) end def legacy_attribute_type(name) if attr_def = legacy_attributes[name] attr_def["type"] end end def legacy_attributes @attributes ||= begin rtc = DictStorage.get(rtc_ref) if rtc['obj_classes'] && rtc['attributes'] && oc = rtc['obj_classes'][values['_obj_class']] rtc['attributes'].inject({}) do |attrs, (name, value)| attrs[name] = value if oc['attributes'] && oc['attributes'].include?(name) attrs end else {} end end end end end