lib/scrivito/obj_data.rb in scrivito_sdk-0.17.0 vs lib/scrivito/obj_data.rb in scrivito_sdk-0.18.0
- old
+ new
@@ -1,10 +1,8 @@
module Scrivito
class ObjData
- MissingAttribute = Class.new
-
internal_key_list = %w[
last_changed
modification
permalink
text_links
@@ -19,12 +17,10 @@
widget_pool
]
INTERNAL_COMPARISON_KEYS = Set.new(internal_comparison_key_list.map { |name| "_#{name}" })
- SPECIAL_KEYS = Set.new(%w[ body title blob ])
-
ATTRIBUTE_DEFAULT_VALUES = {
"string" => "",
"text" => "",
"html" => "",
"multienum" => [],
@@ -44,40 +40,40 @@
def type_of(attribute_name)
value_and_type_of(attribute_name).second
end
- def unchecked_value_of(attribute_name)
- unchecked_value_and_type_of(attribute_name).first
+ def value_without_default_of(attribute_name)
+ value_and_type_without_default_of(attribute_name).first
end
def value_and_type_of(attribute_name)
- value_and_type = internal_value_and_type_of(attribute_name)
-
- if value_and_type.first.is_a?(MissingAttribute)
- raise ScrivitoError.new("Illegal attribute name #{attribute_name}")
+ if internal_attribute?(attribute_name)
+ internal_value_and_type(attribute_name)
else
- value_and_type
+ custom_value_and_type_with_default(attribute_name)
end
end
- def unchecked_value_and_type_of(attribute_name)
- value_and_type = internal_value_and_type_of(attribute_name)
-
- if value_and_type.first.is_a?(MissingAttribute)
- [nil, nil]
+ def value_and_type_without_default_of(attribute_name)
+ if internal_attribute?(attribute_name)
+ internal_value_and_type(attribute_name)
else
- value_and_type
+ raw_value_and_type_of(attribute_name)
end
end
def all_custom_attributes
@all_custom_attributes ||= all_attributes.select do |attribute|
has_custom_attribute?(attribute)
end
end
+ def to_h
+ raise NotImplementedError, 'implement in subclass'
+ end
+
def raw_value_and_type_of(attribute_name)
raise NotImplementedError, "implement in subclass"
end
def has_custom_attribute?(name)
@@ -93,11 +89,11 @@
(all_attributes | other.all_attributes).each do |attr|
attr = attr.to_s
next if attr.start_with?('_') && !INTERNAL_COMPARISON_KEYS.include?(attr)
- if unchecked_value_of(attr) != other.unchecked_value_of(attr)
+ if value_without_default_of(attr) != other.value_without_default_of(attr)
return false
end
end
return true
@@ -106,54 +102,36 @@
false
end
private
- def internal_value_and_type_of(attribute_name)
- value_and_type = raw_value_and_type_of(attribute_name)
+ def custom_value_and_type_with_default(attribute_name)
+ value, type = raw_value_and_type_of(attribute_name)
+ value = ATTRIBUTE_DEFAULT_VALUES[type] if value.nil?
+ [value, type]
+ end
- if value_and_type.blank?
- if INTERNAL_KEYS.include?(attribute_name) || SPECIAL_KEYS.include?(attribute_name)
- type = type_of_internal(attribute_name)
- [default_attribute_value(attribute_name), type]
- else
- [ObjData::MissingAttribute.new, nil]
- end
- else
- value, type = value_and_type
- if value.nil? && has_custom_attribute?(attribute_name)
- value = ATTRIBUTE_DEFAULT_VALUES[type]
- end
+ def internal_value_and_type(attribute_name)
+ value, type = raw_value_and_type_of(attribute_name)
+ type = type_of_internal(attribute_name)
+ [value, type]
+ end
- [value, type]
- end
+ def internal_attribute?(attribute_name)
+ attribute_name.starts_with?('_')
end
def type_of_internal(key)
case key
when "_permalink"
"string"
when "_text_links"
"linklist"
when "_last_changed"
"date"
- when "title", "body"
- "html"
- when "blob"
- "binary"
else
nil
end
end
-
- def default_attribute_value(attribute_name)
- case attribute_name
- when "_text_links"
- []
- else
- nil
- end
- end
-
end
end