Class: Mbrao::Content
- Inherits:
-
Object
- Object
- Mbrao::Content
- Includes:
- ContentPublicInterface
- Defined in:
- lib/mbrao/content.rb
Overview
Represents a parsed content, with its metadata.
Constant Summary
- ALLOWED_DATETIME_FORMATS =
The allowed string format for a datetime.
[ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ]
Instance Attribute Summary (collapse)
-
- (Author) author
The post author.
-
- (String) body
The content’s body.
-
- (DateTime) created_at
The post creation date and time.
-
- (Array) locales
A list of locales for this content should be visible.
-
- (Object) metadata
Gets metadata attribute.
-
- (Object) more
Returns the value of attribute more.
-
- (String|HashWithIndifferentAccess) summary
The content’s summary.
-
- (Array|HashWithIndifferentAccess) tags
The tags associated with the content.
-
- (String|HashWithIndifferentAccess) title
The content’s title.
-
- (String) uid
A unique ID for this post.
-
- (DateTime) updated_at
The post creation date and time.
Class Method Summary (collapse)
-
+ (Content) create(metadata, body)
Creates a content with metadata and body.
-
+ (Array) validate_locales(locales, content = nil)
Validates locales for attribute retrieval.
Instance Method Summary (collapse)
-
- (Hash) as_json(options = {})
Returns the content as an Hash.
-
- (Content) initialize(uid = nil)
constructor
Creates a new content.
Methods included from ContentPublicInterface
#enabled_for_locales?, #get_body, #get_more, #get_tags, #get_title
Constructor Details
- (Content) initialize(uid = nil)
Creates a new content.
149 150 151 |
# File 'lib/mbrao/content.rb', line 149 def initialize(uid = nil) @uid = uid end |
Instance Attribute Details
- (Author) author
Returns The post author.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_hash?(value) value.is_a?(Hash) end end |
- (String) body
Returns The content’s body.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_hash?(value) value.is_a?(Hash) end end |
- (DateTime) created_at
Returns The post creation date and time. The timezone is always UTC.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_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.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_hash?(value) value.is_a?(Hash) end end |
- (Object) metadata
Gets metadata attribute.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_hash?(value) value.is_a?(Hash) end end |
- (Object) more
Returns the value of attribute more
128 129 130 |
# File 'lib/mbrao/content.rb', line 128 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.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_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.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_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.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_hash?(value) value.is_a?(Hash) end end |
- (String) uid
Returns A unique ID for this post. This is only for client uses.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_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.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/mbrao/content.rb', line 119 class Content include Mbrao::ContentPublicInterface attr_accessor :uid attr_accessor :locales attr_accessor :title attr_accessor :summary attr_accessor :body attr_accessor :tags attr_accessor :more attr_accessor :author attr_accessor :created_at attr_accessor :updated_at attr_accessor :metadata # The allowed string format for a datetime. ALLOWED_DATETIME_FORMATS = [ "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z", "%FT%T.%L%z", "%FT%T.%L%Z", "%FT%T%z", "%FT%T%Z", "%F %T %z", "%F %T %Z", "%F %T.%L %z", "%F %T.%L %Z", "%F %T.%L", "%F %T", "%F %H:%M", "%F", "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y" ] # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid 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(nil, true, true, 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 = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :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 = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :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 = if is_hash?() then .ensure_hash(:indifferent) { |v| (v) } else () end 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 = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :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 () if .is_a?(Mbrao::Author) then @author = elsif is_hash?() then = .ensure_hash(:indifferent) @author = Mbrao::Author.create() else @author = ? Mbrao::Author.new(.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) @updated_at = @created_at if !@updated_at end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () if is_hash?() then @metadata = .ensure_hash(:indifferent) else @metadata = HashWithIndifferentAccess.new({raw: }) end end # Returns the content as an Hash. # # @param options [Hash] Options to modify behavior of the serialization. # The only supported value are: # # * `:exclude`, an array of attributes to skip. # * `:exclude_empty`, if to exclude nil values. Default is `false`. # @return [Hash] An hash with all attributes. def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end # Validates locales for attribute retrieval. # # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be # used. # @param content [Content|nil] An optional content to check for availability # @return [Array] The validated list of locales. def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end # Creates a content with metadata and body. # # @param metadata [Hash] The metadata. # @param body [String] The body of the content. # @return [Content] A new content. def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end private # 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, ) content.uid = .delete(:uid) content.title = .delete(:title) content.summary = .delete(:summary) content. = Mbrao::Author.create(.delete(:author)) content. = .delete(:tags) content.more = .delete(:more) content.created_at = .delete(:created_at) content.updated_at = .delete(:updated_at) content.locales = extract_locales() content. = content end # Extracts locales from metadata. # # @param metadata [Hash] The metadata that contains the locales. # @return [Array] The locales. def self.extract_locales() locales = .delete(:locales) locales = locales.ensure_string.split(/\s*,\s*/) if !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 # Extracts a date and time from a value. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def extract_datetime(value) begin value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate.new end end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new) else parse_datetime_string(value) end end # Parses a datetime string. # # @param value [String] The value to parse. # @return [DateTime] The extracted value. def parse_datetime_string(value) value = value.ensure_string catch(:parsed) do ALLOWED_DATETIME_FORMATS.each do |format| rv = DateTime.strptime(value, format) rescue nil throw(:parsed, rv) if rv end raise(ArgumentError.new) end end # Extracts tags from an array, making sure all the comma separated strings are evaluated. # # @param value [String|Array] The string or array to parse. # @return [Array] The list of tags. def (value) value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) } end # Check if value is an Hash. # # @param value [Object] The object to check. # @return [Boolean] `true` if value is an Hash, `false` otherwise def is_hash?(value) value.is_a?(Hash) end end |
Class Method Details
+ (Content) create(metadata, body)
Creates a content with metadata and body.
281 282 283 284 285 286 |
# File 'lib/mbrao/content.rb', line 281 def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end |
+ (Array) validate_locales(locales, content = nil)
Validates locales for attribute retrieval.
269 270 271 272 273 274 |
# File 'lib/mbrao/content.rb', line 269 def self.validate_locales(locales, content = nil) locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip } locales = (locales.empty? ? [Mbrao::Parser.locale] : locales) raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales) locales end |
Instance Method Details
- (Hash) as_json(options = {})
Returns the content as an Hash.
258 259 260 261 |
# File 'lib/mbrao/content.rb', line 258 def as_json( = {}) keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata] ::Mbrao::Parser.as_json(self, keys, ) end |