Class: Mbrao::Content
- Inherits:
-
Object
- Object
- Mbrao::Content
- Includes:
- ContentPublicInterface
- Defined in:
- lib/mbrao/content.rb
Overview
Represents a parsed content, with its metadata.
Instance Attribute Summary collapse
-
#author ⇒ Author
The post author.
-
#body ⇒ String
The content’s body.
-
#created_at ⇒ DateTime
The post creation date and time.
-
#locales ⇒ Array
A list of locales for this content should be visible.
-
#metadata ⇒ Object
Gets metadata attribute.
-
#more ⇒ Object
Returns the value of attribute more.
-
#summary ⇒ String|HashWithIndifferentAccess
The content’s summary.
-
#tags ⇒ Array|HashWithIndifferentAccess
The tags associated with the content.
-
#title ⇒ String|HashWithIndifferentAccess
The content’s title.
-
#uid ⇒ String
A unique ID for this post.
-
#updated_at ⇒ DateTime
The post creation date and time.
Class Method Summary collapse
-
.create(metadata, body) ⇒ Content
Creates a content with metadata and body.
-
.validate_locales(locales, content = nil) ⇒ Array
Validates locales for attribute retrieval.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Returns the content as an Hash.
-
#initialize(uid = nil) ⇒ Content
constructor
Creates a new content.
Methods included from ContentPublicInterface
#enabled_for_locales?, #get_body, #get_more, #get_tags, #get_title
Constructor Details
#initialize(uid = nil) ⇒ Content
Creates a new content.
137 138 139 |
# File 'lib/mbrao/content.rb', line 137 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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#body ⇒ String
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#created_at ⇒ DateTime
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#locales ⇒ Array
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#metadata ⇒ Object
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#more ⇒ Object
Returns the value of attribute more.
128 129 130 |
# File 'lib/mbrao/content.rb', line 128 def more @more end |
#summary ⇒ String|HashWithIndifferentAccess
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#tags ⇒ Array|HashWithIndifferentAccess
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#title ⇒ String|HashWithIndifferentAccess
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#uid ⇒ String
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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 |
#updated_at ⇒ DateTime
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 |
# 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 # 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 # Extract 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 # Normalize 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 # Parse 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 : nil else DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z") end end # Extract 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
.create(metadata, body) ⇒ Content
Creates a content with metadata and body.
269 270 271 272 273 274 |
# File 'lib/mbrao/content.rb', line 269 def self.create(, body) rv = Mbrao::Content.new rv.body = body.ensure_string.strip (rv, .symbolize_keys) if .is_a?(Hash) rv end |
.validate_locales(locales, content = nil) ⇒ Array
Validates locales for attribute retrieval.
257 258 259 260 261 262 |
# File 'lib/mbrao/content.rb', line 257 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 |