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

author[RW]
body[RW]
created_at[RW]
locales[RW]
metadata[RW]
more[RW]
summary[RW]
tags[RW]
title[RW]
uid[RW]
updated_at[RW]

Public Class Methods

assign_metadata(content, metadata) click to toggle source

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
extract_locales(locales) click to toggle source

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
new(uid = nil) click to toggle source

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
normalize_locales(locales) click to toggle source

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

author=(new_author) click to toggle source

Sets the `author` attribute.

@param new_author [Author|Hash|Object|NilClass] The new value for the attribute.

# File lib/mbrao/content.rb, line 104
def author=(new_author)
  @author =
    if new_author.is_a?(Mbrao::Author)
      new_author
    elsif hash?(new_author)
      Mbrao::Author.create(new_author.ensure_hash(accesses: :indifferent))
    else
      new_author ? Mbrao::Author.new(new_author.ensure_string) : nil
    end
end
body=(value) click to toggle source

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
created_at=(value) click to toggle source

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
locales=(value) click to toggle source

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
metadata=(new_metadata) click to toggle source

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
more=(new_more) click to toggle source

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
summary=(new_summary) click to toggle source

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
tags=(new_tags) click to toggle source

Sets the `tags` attribute.

@param new_tags [Array|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. Tags can also be comma-separated.
# File lib/mbrao/content.rb, line 89
def tags=(new_tags)
  @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
end
title=(new_title) click to toggle source

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
updated_at=(value) click to toggle source

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