Class: Mbrao::Content
- Inherits:
-
Object
- Object
- Mbrao::Content
- Includes:
- ContentInterface
- Defined in:
- lib/mbrao/content.rb
Overview
Represents a parsed content, with its metadata.
Constant Summary
Constants included from ContentInterface
Mbrao::ContentInterface::ALLOWED_DATETIME_FORMATS
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.
Instance Method Summary collapse
-
#initialize(uid = nil) ⇒ Content
constructor
Creates a new content.
Methods included from ContentInterface
#as_json, #enabled_for_locales?, #get_body, #get_more, #get_tags, #get_title
Constructor Details
#initialize(uid = nil) ⇒ Content
Creates a new content.
44 45 46 |
# File 'lib/mbrao/content.rb', line 44 def initialize(uid = nil) @uid = uid end |
Instance Attribute Details
#author ⇒ Author
Returns The post author.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#body ⇒ String
Returns The content's body.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#created_at ⇒ DateTime
Returns The post creation date and time. The timezone is always UTC.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#locales ⇒ Array
Returns A list of locales for this content should be visible. An empty list means that there are no limitations.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#metadata ⇒ Object
Gets metadata attribute.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#more ⇒ Object
Returns the value of attribute more.
37 38 39 |
# File 'lib/mbrao/content.rb', line 37 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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end 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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#title ⇒ String|HashWithIndifferentAccess
Returns The content's title. Can be a String or an HashWithIndifferentAccess, if multiple titles are specified for
multiple locales.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#uid ⇒ String
Returns A unique ID for this post. This is only for client uses.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |
#updated_at ⇒ DateTime
Returns The post creation date and time. Defaults to the creation date. The timezone is always UTC.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mbrao/content.rb', line 36 class Content attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata include Mbrao::ContentInterface # Creates a new content. # # @param uid [String] The UID for this content. def initialize(uid = nil) @uid = uid end # Gets metadata attribute. # # @return The metadata attribute. def @metadata ||= HashWithIndifferentAccess.new end # Sets the `locales` attribute. # # @param value [Array] The new value for the attribute. A empty or "*" will be the default value. def locales=(value) @locales = value.ensure_array(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 = 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 = 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 = hash?() ? .ensure_hash(:indifferent) { |v| (v) } : () end # Sets the `more` attribute. # # @param new_more [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. # A empty or "*" will be the default value. def more=(new_more) @more = hash?(new_more) ? new_more.ensure_hash(: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 () @author = if .is_a?(Mbrao::Author) elsif hash?() then Mbrao::Author.create(.ensure_hash(:indifferent)) else ? 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) || @created_at end # Sets the `metadata` attribute. # # @param new_metadata [Hash] The new value for the attribute. def () @metadata = hash?() ? .ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: }) end private # 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) value = parse_datetime(value) if value value ? value.utc : nil rescue ArgumentError raise Mbrao::Exceptions::InvalidDate end # Parses a datetime. # # @param value [String|DateTime|Fixnum] The value to parse. # @return [DateTime] The extracted value. def parse_datetime(value) rv = case value.class.to_s when "DateTime" then value when "Date", "Time" then value.to_datetime when "Float", "Fixnum" then parse_datetime_number(value) else parse_datetime_string(value) end raise ArgumentError unless rv rv end # Parses a datetime from a number. # # @param value [String] The value to parse. # @return [Float|Fixnum] The extracted value. def parse_datetime_number(value) number = value.to_float number > 0 ? Time.at(number).to_datetime : nil end # Parses a datetime from a 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.find do |format| begin throw(:parsed, DateTime.strptime(value, format)) rescue nil end end 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 hash?(value) value.is_a?(Hash) end # Assigns metadata to a content # # @param content [Content] The content to manipulate. # @param metadata [Hash] The metadata to assign. # @return [Content] The content with metadata. def self.(content, ) [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field| content.send("#{field}=", .delete(field)) end content. = Mbrao::Author.create(.delete(:author)) content.locales = extract_locales(.delete(:locales)) content. = end # Extracts locales from metadata. # # @param locales [String] The list of locales. # @return [Array] The locales. def self.extract_locales(locales) locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array) normalize_locales(locales) end # Normalizes locales for further usage. # # @param locales [Array] The locales to normalize. # @return [Array] The normalized locales. def self.normalize_locales(locales) locales.flatten.map(&:ensure_string).map(&:strip).uniq end end |