Class: Mbrao::Content

Inherits:
Object
  • Object
show all
Includes:
ContentInterface
Defined in:
lib/mbrao/content.rb

Overview

Represents a parsed content, with its metadata.

Constant Summary

Constant Summary

Constants included from ContentInterface

Mbrao::ContentInterface::ALLOWED_DATETIME_FORMATS

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ContentInterface

#as_json, #enabled_for_locales?, #get_body, #get_more, #get_tags, #get_title

Constructor Details

- (Content) initialize(uid = nil)

Creates a new content.

Parameters:

  • uid (String) (defaults to: nil)

    The UID for this content.



44
45
46
# File 'lib/mbrao/content.rb', line 44

def initialize(uid = nil)
  @uid = uid
end

Instance Attribute Details

- (Author) author

Returns The post author.

Returns:

  • (Author)

    The post author.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (String) body

Returns The content’s body.

Returns:

  • (String)

    The content’s body.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (DateTime) created_at

Returns The post creation date and time. The timezone is always UTC.

Returns:

  • (DateTime)

    The post creation date and time. The timezone is always UTC.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (Array) locales

Returns A list of locales for this content should be visible. An empty list means that there are no limitations.

Returns:

  • (Array)

    A list of locales for this content should be visible. An empty list means that there are no limitations.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (Object) metadata

Gets metadata attribute.

Returns:

  • The metadata attribute.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (Object) more

Returns the value of attribute more



37
38
39
# File 'lib/mbrao/content.rb', line 37

def more
  @more
end

- (String|HashWithIndifferentAccess) summary

Returns The content’s summary. Can be a String or an HashWithIndifferentAccess, if multiple summaries are specified for multiple locales.

Returns:

  • (String|HashWithIndifferentAccess)

    The content’s summary. Can be a String or an HashWithIndifferentAccess, if multiple summaries are specified for multiple locales.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (Array|HashWithIndifferentAccess) tags

Returns The tags associated with the content. Can be an Array or an HashWithIndifferentAccess, if multiple tags set are specified for multiple locales.

Returns:

  • (Array|HashWithIndifferentAccess)

    The tags associated with the content. Can be an Array or an HashWithIndifferentAccess, if multiple tags set are specified for multiple locales.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (String|HashWithIndifferentAccess) title

Returns The content’s title. Can be a String or an HashWithIndifferentAccess, if multiple titles are specified for multiple locales.

Returns:

  • (String|HashWithIndifferentAccess)

    The content’s title. Can be a String or an HashWithIndifferentAccess, if multiple titles are specified for multiple locales.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (String) uid

Returns A unique ID for this post. This is only for client uses.

Returns:

  • (String)

    A unique ID for this post. This is only for client uses.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

- (DateTime) updated_at

Returns The post creation date and time. Defaults to the creation date. The timezone is always UTC.

Returns:

  • (DateTime)

    The post creation date and time. Defaults to the creation date. The timezone is always UTC.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mbrao/content.rb', line 36

class Content
  attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata

  include Mbrao::ContentInterface

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata ||= HashWithIndifferentAccess.new
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |l| l.ensure_string.strip }
  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.
  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 `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.
  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 `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.
  def body=(value)
    @body = value.ensure_string
  end

  # 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.
  def tags=(new_tags)
    @tags = hash?(new_tags) ? new_tags.ensure_hash(accesses: :indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
  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.
  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 `author` attribute.
  #
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
  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

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value) || @created_at
  end

  # Sets the `metadata` attribute.
  #
  # @param new_metadata [Hash] The new value for the attribute.
  def metadata=()
    @metadata = hash?() ? .ensure_hash(accesses: :indifferent) : @metadata = HashWithIndifferentAccess.new({raw: })
  end

  # 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.
  def self.(content, )
    [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
      content.send("#{field}=", .delete(field))
    end

    content.author = Mbrao::Author.create(.delete(:author))
    content.locales = extract_locales(.delete(:locales))
    content. = 
  end

  # Extracts locales from metadata.
  #
  # @param locales [String] The list of locales.
  # @return [Array] The locales.
  def self.extract_locales(locales)
    locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
    normalize_locales(locales)
  end

  # Normalizes locales for further usage.
  #
  # @param locales [Array] The locales to normalize.
  # @return [Array] The normalized locales.
  def self.normalize_locales(locales)
    locales.flatten.map(&:ensure_string).map(&:strip).uniq
  end

  private

  # :nodoc:
  def extract_datetime(value)
    value = parse_datetime(value) if value
    value ? value.utc : nil
  rescue ArgumentError
    raise Mbrao::Exceptions::InvalidDate
  end

  # :nodoc:
  def parse_datetime(value)
    rv =
      case value.class.to_s
      when "DateTime" then value
      when "Date", "Time" then value.to_datetime
      when "Float", "Fixnum" then parse_datetime_number(value)
      else parse_datetime_string(value)
      end

    raise ArgumentError unless rv
    rv
  end

  # :nodoc:
  def parse_datetime_number(value)
    number = value.to_float
    number > 0 ? Time.at(number).to_datetime : nil
  end

  # :nodoc:
  def parse_datetime_string(value)
    value = value.ensure_string

    catch(:parsed) do
      ALLOWED_DATETIME_FORMATS.find do |format|
        begin
          throw(:parsed, DateTime.strptime(value, format))
        rescue
          nil
        end
      end
    end
  end

  # :nodoc:
  def parse_tags(value)
    value.ensure_array(no_duplicates: true, compact: true, flatten: true) { |v| v.ensure_string.split(/\s*,\s*/) }
  end

  # :nodoc:
  def hash?(value)
    value.is_a?(Hash)
  end
end

Class Method Details

+ (Content) assign_metadata(content, metadata)

Assigns metadata to a content

Parameters:

  • content (Content)

    The content to manipulate.

  • metadata (Hash)

    The metadata to assign.

Returns:

  • (Content)

    The content with metadata.



141
142
143
144
145
146
147
148
149
# File 'lib/mbrao/content.rb', line 141

def self.(content, )
  [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
    content.send("#{field}=", .delete(field))
  end

  content.author = Mbrao::Author.create(.delete(:author))
  content.locales = extract_locales(.delete(:locales))
  content. = 
end

+ (Array) extract_locales(locales)

Extracts locales from metadata.

Parameters:

  • locales (String)

    The list of locales.

Returns:

  • (Array)

    The locales.



155
156
157
158
# 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

+ (Array) normalize_locales(locales)

Normalizes locales for further usage.

Parameters:

  • locales (Array)

    The locales to normalize.

Returns:

  • (Array)

    The normalized locales.



164
165
166
# File 'lib/mbrao/content.rb', line 164

def self.normalize_locales(locales)
  locales.flatten.map(&:ensure_string).map(&:strip).uniq
end