Module: Textus::Store::Envelope::Meta
- Defined in:
- lib/textus/store/envelope/meta.rb
Constant Summary collapse
- NO_META_FORMATS =
%w[text].freeze
- FIELDS =
{ "uid" => { inject: lambda { |, content, , **_opts| m = .is_a?(Hash) ? .dup : {} existing = .is_a?(Hash) ? ["uid"] : nil m["uid"] = existing || Textus::Value::Uid.mint unless m["uid"].is_a?(String) && !m["uid"].empty? [m, content] }, }, "sources" => { inject: lambda { |, content, , etag_for: nil| m = .is_a?(Hash) ? .dup : {} existing = .is_a?(Hash) ? ["sources"] : nil if m.key?("sources") raise Textus::BadContent.new(nil, "_meta.sources must be an array") unless m["sources"].is_a?(Array) m["sources"] = m["sources"].map { |s| Meta.normalize_source!(s, etag_for) } elsif existing.is_a?(Array) && !existing.empty? m["sources"] = existing end [m, content] }, }, }.freeze
Class Method Summary collapse
- .inject_all(meta, content, existing_meta = {}, format: nil, etag_for: nil) ⇒ Object
- .normalize_source!(src, etag_for) ⇒ Object
Class Method Details
.inject_all(meta, content, existing_meta = {}, format: nil, etag_for: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/textus/store/envelope/meta.rb', line 36 def self.inject_all(, content, = {}, format: nil, etag_for: nil) return [, content] if NO_META_FORMATS.include?(format) FIELDS.each_value do |field| , content = field[:inject].call(, content, , etag_for: etag_for) end [, content] end |
.normalize_source!(src, etag_for) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/textus/store/envelope/meta.rb', line 46 def self.normalize_source!(src, etag_for) key = case src when String then src when Hash then src["key"] end raise Textus::BadContent.new(nil, "each source must be a string key or { key: } object") unless key.is_a?(String) raise Textus::BadContent.new(nil, "each source key must be a non-empty string") if key.empty? etag = etag_for&.call(key) etag ? { "key" => key, "etag" => etag } : { "key" => key } end |