class Mbrao::Content
Represents a parsed content, with its metadata.
@attribute uid
@return [String] A unique ID for this post. This is only for client uses.
@attribute locales
@return [Array] A list of locales for this content should be visible. An empty list means that there are no limitations.
@attribute title
@return [String|HashWithIndifferentAccess] The content's title. Can be a `String` or an `HashWithIndifferentAccess`, if multiple titles are specified for multiple locales.
@attribute summary
@return [String|HashWithIndifferentAccess] The content's summary. Can be a `String` or an `HashWithIndifferentAccess`, if multiple summaries are specified for multiple locales.
@attribute body
@return [String] The content's body.
@attribute tags
@return [String|HashWithIndifferentAccess] The content's "more link" label. Can be a `String` or an `HashWithIndifferentAccess`, if multiple labels are specified for multiple locales.
@attribute tags
@return [Array|HashWithIndifferentAccess] The tags associated with the content. Can be an `Array` or an `HashWithIndifferentAccess`, if multiple tags set are specified for multiple locales.
@attribute author
@return [Author] The post author.
@attribute created_at
@return [DateTime] The post creation date and time. The timezone is always UTC.
@attribute updated_at
@return [DateTime] The post creation date and time. Defaults to the creation date. The timezone is always UTC.
@attribute metadata
@return [Hash] The full list of metadata of this content.
Attributes
Public Class Methods
Assigns metadata to a content
@param content [Content] The content to manipulate. @param metadata [Hash] The metadata to assign. @return [Content] The content with metadata.
# File lib/mbrao/content.rb, line 141 def self.assign_metadata(content, metadata) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", metadata.delete(field)) end content.author = Mbrao::Author.create(metadata.delete(:author)) content.locales = extract_locales(metadata.delete(:locales)) content.metadata = metadata end
Extracts locales from metadata.
@param locales [String] The list of locales. @return [Array] The locales.
# File lib/mbrao/content.rb, line 155 def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end
Creates a new content.
@param uid [String] The UID for this content.
# File lib/mbrao/content.rb, line 44 def initialize(uid = nil) @uid = uid end
Normalizes locales for further usage.
@param locales [Array] The locales to normalize. @return [Array] The normalized locales.
# File lib/mbrao/content.rb, line 164 def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end
Public Instance Methods
Sets the `body` attribute.
@param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using get_body
.
# File lib/mbrao/content.rb, line 81 def body=(value) @body = value.ensure_string end
Sets the `created_at` attribute.
@param value [String|DateTime|Fixnum] The new value for the attribute.
# File lib/mbrao/content.rb, line 118 def created_at=(value) @created_at = extract_datetime(value) end
Sets the `locales` attribute.
@param value [Array] The new value for the attribute. A empty or “*” will be the default value.
# File lib/mbrao/content.rb, line 58 def locales=(value) @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip } end
Sets the `metadata` attribute.
@param new_metadata [Hash] The new value for the attribute.
# File lib/mbrao/content.rb, line 132 def metadata=(new_metadata) @metadata = hash?(new_metadata) ? new_metadata.ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: new_metadata}) end
Sets the `more` attribute.
@param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas.
A empty or "*" will be the default value.
# File lib/mbrao/content.rb, line 97 def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(accesses: :indifferent, default: nil, sanitizer: :ensure_string) : new_more.ensure_string end
Sets the `summary` attribute.
@param new_summary [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas.
A empty or "*" will be the default value.
# File lib/mbrao/content.rb, line 74 def summary=(new_summary) @summary = hash?(new_summary) ? new_summary.ensure_hash(accesses: :indifferent, default: nil, sanitizer: :ensure_string) : new_summary.ensure_string end
Sets the `title` attribute.
@param new_title [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas.
A empty or "*" will be the default value.
# File lib/mbrao/content.rb, line 66 def title=(new_title) @title = hash?(new_title) ? new_title.ensure_hash(accesses: :indifferent, default: nil, sanitizer: :ensure_string) : new_title.ensure_string end
Sets the `updated_at` attribute.
@param value [String|DateTime|Fixnum] The new value for the attribute.
# File lib/mbrao/content.rb, line 125 def updated_at=(value) @updated_at = extract_datetime(value) || @created_at end