mbrao 1.4.4 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +29 -0
  3. data/.travis.yml +3 -4
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +4 -4
  6. data/doc/ActionView.html +125 -0
  7. data/doc/ActionView/Template.html +140 -0
  8. data/doc/ActionView/Template/Handlers.html +3 -3
  9. data/doc/ActionView/Template/Handlers/MbraoTemplate.html +26 -26
  10. data/doc/HTML.html +2 -2
  11. data/doc/HTML/Pipeline.html +2 -2
  12. data/doc/HTML/Pipeline/KramdownFilter.html +4 -4
  13. data/doc/Mbrao.html +3 -3
  14. data/doc/Mbrao/Author.html +29 -29
  15. data/doc/Mbrao/Content.html +1977 -3644
  16. data/doc/Mbrao/ContentInterface.html +817 -0
  17. data/doc/Mbrao/ContentInterface/ClassMethods.html +388 -0
  18. data/doc/Mbrao/Exceptions.html +1 -1
  19. data/doc/Mbrao/Exceptions/InvalidDate.html +1 -1
  20. data/doc/Mbrao/Exceptions/InvalidMetadata.html +1 -1
  21. data/doc/Mbrao/Exceptions/Parsing.html +1 -1
  22. data/doc/Mbrao/Exceptions/Rendering.html +1 -1
  23. data/doc/Mbrao/Exceptions/UnavailableLocalization.html +1 -1
  24. data/doc/Mbrao/Exceptions/Unimplemented.html +1 -1
  25. data/doc/Mbrao/Exceptions/UnknownEngine.html +1 -1
  26. data/doc/Mbrao/Parser.html +12 -12
  27. data/doc/Mbrao/ParserInterface.html +134 -0
  28. data/doc/Mbrao/ParserInterface/ClassMethods.html +1724 -0
  29. data/doc/Mbrao/ParserValidations.html +134 -0
  30. data/doc/Mbrao/ParserValidations/ClassMethods.html +348 -0
  31. data/doc/Mbrao/ParsingEngines.html +1 -1
  32. data/doc/Mbrao/ParsingEngines/Base.html +4 -4
  33. data/doc/Mbrao/ParsingEngines/PlainText.html +25 -15
  34. data/doc/Mbrao/RenderingEngines.html +1 -1
  35. data/doc/Mbrao/RenderingEngines/Base.html +2 -2
  36. data/doc/Mbrao/RenderingEngines/HtmlPipeline.html +173 -169
  37. data/doc/Mbrao/Version.html +3 -3
  38. data/doc/_index.html +51 -24
  39. data/doc/class_list.html +1 -1
  40. data/doc/file.README.html +1 -1
  41. data/doc/index.html +1 -1
  42. data/doc/method_list.html +63 -69
  43. data/doc/top-level-namespace.html +2 -2
  44. data/lib/mbrao.rb +7 -2
  45. data/lib/mbrao/author.rb +5 -5
  46. data/lib/mbrao/content.rb +110 -256
  47. data/lib/mbrao/content_interface.rb +146 -0
  48. data/lib/mbrao/exceptions.rb +1 -1
  49. data/lib/mbrao/integrations/rails.rb +48 -42
  50. data/lib/mbrao/parser.rb +24 -176
  51. data/lib/mbrao/parser_interface.rb +143 -0
  52. data/lib/mbrao/parser_validations.rb +41 -0
  53. data/lib/mbrao/parsing_engines/base.rb +4 -4
  54. data/lib/mbrao/parsing_engines/plain_text.rb +136 -121
  55. data/lib/mbrao/rendering_engines/base.rb +2 -2
  56. data/lib/mbrao/rendering_engines/html_pipeline.rb +52 -77
  57. data/lib/mbrao/rendering_engines/html_pipeline/kramdown_filter.rb +31 -0
  58. data/lib/mbrao/version.rb +2 -2
  59. data/mbrao.gemspec +3 -3
  60. data/spec/mbrao/author_spec.rb +1 -1
  61. data/spec/mbrao/content_spec.rb +1 -1
  62. data/spec/mbrao/parser_spec.rb +16 -16
  63. data/spec/mbrao/rendering_engines/html_pipeline/kramdown_filter_spec.rb +28 -0
  64. data/spec/mbrao/rendering_engines/html_pipeline_spec.rb +0 -21
  65. metadata +23 -8
@@ -85,7 +85,7 @@
85
85
  <p class="children">
86
86
 
87
87
 
88
- <strong class="modules">Modules:</strong> <span class='object_link'><a href="HTML.html" title="HTML (module)">HTML</a></span>, <span class='object_link'><a href="Mbrao.html" title="Mbrao (module)">Mbrao</a></span>
88
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="ActionView.html" title="ActionView (module)">ActionView</a></span>, <span class='object_link'><a href="HTML.html" title="HTML (module)">HTML</a></span>, <span class='object_link'><a href="Mbrao.html" title="Mbrao (module)">Mbrao</a></span>
89
89
 
90
90
 
91
91
 
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Wed Apr 9 21:36:00 2014 by
106
+ Generated on Sun May 11 18:17:16 2014 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.7.4 (ruby-2.1.0).
109
109
  </div>
data/lib/mbrao.rb CHANGED
@@ -9,16 +9,21 @@ require "html/pipeline"
9
9
  require "slim"
10
10
  require "kramdown"
11
11
  require "yaml"
12
+ require "active_support/json/encoding"
12
13
 
13
- require "mbrao/version" if !defined?(Mbrao::Version)
14
+ require "mbrao/version" unless defined?(Mbrao::Version)
14
15
  require "mbrao/exceptions"
16
+ require "mbrao/content_interface"
15
17
  require "mbrao/content"
16
18
  require "mbrao/author"
19
+ require "mbrao/parser_interface"
20
+ require "mbrao/parser_validations"
17
21
  require "mbrao/parser"
18
22
  require "mbrao/parsing_engines/base"
19
23
  require "mbrao/parsing_engines/plain_text"
24
+ require "mbrao/rendering_engines/html_pipeline/kramdown_filter"
20
25
  require "mbrao/rendering_engines/base"
21
26
  require "mbrao/rendering_engines/html_pipeline"
22
27
  require "mbrao/integrations/rails" if defined?(ActionView)
23
28
 
24
- Lazier.load!(:object, :hash)
29
+ Lazier.load!(:object, :hash, :boolean)
data/lib/mbrao/author.rb CHANGED
@@ -36,9 +36,9 @@ module Mbrao
36
36
  # @param metadata [HashWithIndifferentAccess] The full list of metadata of this author.
37
37
  def initialize(name, email = nil, website = nil, image = nil, metadata = nil)
38
38
  @name = name.ensure_string
39
- @email = Mbrao::Parser.is_email?(email) ? email : nil
40
- @website = Mbrao::Parser.is_url?(website) ? website : nil
41
- @image = Mbrao::Parser.is_url?(image) ? image : nil
39
+ @email = Mbrao::Parser.email?(email) ? email : nil
40
+ @website = Mbrao::Parser.url?(website) ? website : nil
41
+ @image = Mbrao::Parser.url?(image) ? image : nil
42
42
  @metadata = metadata.ensure_hash(:indifferent)
43
43
  end
44
44
 
@@ -60,7 +60,7 @@ module Mbrao
60
60
  # @param data [Hash] The data of the author
61
61
  # @return [Author] A new author.
62
62
  def self.create(data)
63
- if data.is_a?(Hash) then
63
+ if data.is_a?(Hash)
64
64
  data = HashWithIndifferentAccess.new(data)
65
65
  uid = data.delete(:uid)
66
66
  metadata = data.delete(:metadata) || {}
@@ -72,4 +72,4 @@ module Mbrao
72
72
  end
73
73
  end
74
74
  end
75
- end
75
+ end
data/lib/mbrao/content.rb CHANGED
@@ -5,89 +5,6 @@
5
5
  #
6
6
 
7
7
  module Mbrao
8
- # Setter methods for the {Content Content} class.
9
- module ContentPublicInterface
10
- # Checks if the content is available for at least one of the provided locales.
11
- #
12
- # @param locales [Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
13
- # @return [Boolean] `true` if the content is available for at least one of the desired locales, `false` otherwise.
14
- def enabled_for_locales?(*locales)
15
- locales = locales.flatten.ensure_array(nil, false, false, true) {|l| l.ensure_string.strip }.reject {|l| l == "*" }
16
- @locales.blank? || locales.blank? || (@locales & locales).present?
17
- end
18
-
19
- # Gets the title of the content in the desired locales.
20
- #
21
- # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
22
- # @return [String|HashWithIndifferentAccess] Return the title of the content in the desired locales. If only one locale is required, then a `String`
23
- # is returned, else a `HashWithIndifferentAccess` with locales as keys.
24
- def get_title(locales = [])
25
- filter_attribute_for_locales(@title, locales)
26
- end
27
-
28
- # Gets the body returning only the portion which are available for the given locales.
29
- #
30
- # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
31
- # @param engine [String|Symbol|Object] The engine to use to filter contents.
32
- # @return [String|HashWithIndifferentAccess] Return the body of the content in the desired locales. If only one locale is required, then a `String`
33
- # is returned, else a `HashWithIndifferentAccess` with locales as keys.
34
- def get_body(locales = [], engine = :plain_text)
35
- Mbrao::Parser.create_engine(engine).filter_content(self, locales)
36
- end
37
-
38
- # Gets the tags of the content in the desired locales.
39
- #
40
- # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
41
- # @return [Array|HashWithIndifferentAccess] Return the title of the content in the desired locales. If only one locale is required, then a `Array`
42
- # is returned, else a `HashWithIndifferentAccess` with locales as keys.
43
- def get_tags(locales = [])
44
- filter_attribute_for_locales(@tags, locales)
45
- end
46
-
47
- # Gets the "more link" text of the content in the desired locales.
48
- #
49
- # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
50
- # @return [String|HashWithIndifferentAccess] Return the label of the "more link" of the content in the desired locales. If only one locale is required,
51
- # then a `String` is returned, else a `HashWithIndifferentAccess` with locales as keys.
52
- def get_more(locales = [])
53
- filter_attribute_for_locales(@more, locales)
54
- end
55
-
56
- private
57
- # Filters an attribute basing a set of locales.
58
- #
59
- # @param attribute [Object|HashWithIndifferentAccess] The desired attribute.
60
- # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
61
- # @return [String|HashWithIndifferentAccess] Return the object for desired locales. If only one locale is available, then only a object is returned,
62
- # else a `HashWithIndifferentAccess` with locales as keys.
63
- def filter_attribute_for_locales(attribute, locales)
64
- locales = ::Mbrao::Content.validate_locales(locales, self)
65
-
66
- if attribute.is_a?(HashWithIndifferentAccess) then
67
- rv = attribute.reduce(HashWithIndifferentAccess.new) { |hash, pair| append_value_for_locale(hash, pair[0], locales, pair[1]) }
68
- rv.keys.length == 1 ? rv.values.first : rv
69
- else
70
- attribute
71
- end
72
- end
73
-
74
- # Adds an value on a hash if enable for requested locales.
75
- #
76
- # @param hash [Hash] The hash to handle.
77
- # @param locale [String] The list of locale (separated by commas) for which the value is available.
78
- # @param locales [Array] The list of locale for which this value is requested. Can include `*` to match all. If none are specified, the default mbrao
79
- # locale will be used.
80
- # @param value [Object] The value to add.
81
- # @return [Hash] Return the original hash.
82
- def append_value_for_locale(hash, locale, locales, value)
83
- locale.split(/\s*,\s*/).map(&:strip).each do |l|
84
- hash[l] = value if locales.include?("*") || locales.include?(l)
85
- end
86
-
87
- hash
88
- end
89
- end
90
-
91
8
  # Represents a parsed content, with its metadata.
92
9
  #
93
10
  # @attribute uid
@@ -117,31 +34,9 @@ module Mbrao
117
34
  # @attribute metadata
118
35
  # @return [Hash] The full list of metadata of this content.
119
36
  class Content
120
- include Mbrao::ContentPublicInterface
121
-
122
- attr_accessor :uid
123
- attr_accessor :locales
124
- attr_accessor :title
125
- attr_accessor :summary
126
- attr_accessor :body
127
- attr_accessor :tags
128
- attr_accessor :more
129
- attr_accessor :author
130
- attr_accessor :created_at
131
- attr_accessor :updated_at
132
- attr_accessor :metadata
37
+ attr_accessor :uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata
133
38
 
134
- # The allowed string format for a datetime.
135
- ALLOWED_DATETIME_FORMATS = [
136
- "%Y%m%dT%H%M%S%z", "%Y%m%dT%H%M%S%Z",
137
- "%FT%T.%L%z", "%FT%T.%L%Z",
138
- "%FT%T%z", "%FT%T%Z",
139
- "%F %T %z", "%F %T %Z",
140
- "%F %T.%L %z", "%F %T.%L %Z",
141
-
142
- "%F %T.%L", "%F %T", "%F %H:%M", "%F",
143
- "%d/%m/%Y %T.%L", "%d/%m/%Y %T", "%d/%m/%Y %H:%M", "%d/%m/%Y"
144
- ]
39
+ include Mbrao::ContentInterface
145
40
 
146
41
  # Creates a new content.
147
42
  #
@@ -150,11 +45,18 @@ module Mbrao
150
45
  @uid = uid
151
46
  end
152
47
 
48
+ # Gets metadata attribute.
49
+ #
50
+ # @return The metadata attribute.
51
+ def metadata
52
+ @metadata ||= HashWithIndifferentAccess.new
53
+ end
54
+
153
55
  # Sets the `locales` attribute.
154
56
  #
155
57
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
156
58
  def locales=(value)
157
- @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
59
+ @locales = value.ensure_array(nil, true, true, true) { |l| l.ensure_string.strip }
158
60
  end
159
61
 
160
62
  # Sets the `title` attribute.
@@ -162,7 +64,7 @@ module Mbrao
162
64
  # @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.
163
65
  # A empty or "*" will be the default value.
164
66
  def title=(new_title)
165
- @title = is_hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :ensure_string) : new_title.ensure_string
67
+ @title = hash?(new_title) ? new_title.ensure_hash(:indifferent, nil, :ensure_string) : new_title.ensure_string
166
68
  end
167
69
 
168
70
  # Sets the `summary` attribute.
@@ -170,7 +72,7 @@ module Mbrao
170
72
  # @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.
171
73
  # A empty or "*" will be the default value.
172
74
  def summary=(new_summary)
173
- @summary = is_hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :ensure_string) : new_summary.ensure_string
75
+ @summary = hash?(new_summary) ? new_summary.ensure_hash(:indifferent, nil, :ensure_string) : new_summary.ensure_string
174
76
  end
175
77
 
176
78
  # Sets the `body` attribute.
@@ -185,11 +87,7 @@ module Mbrao
185
87
  # @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.
186
88
  # A empty or "*" will be the default value. Tags can also be comma-separated.
187
89
  def tags=(new_tags)
188
- @tags = if is_hash?(new_tags) then
189
- new_tags.ensure_hash(:indifferent) { |v| parse_tags(v) }
190
- else
191
- parse_tags(new_tags)
192
- end
90
+ @tags = hash?(new_tags) ? new_tags.ensure_hash(:indifferent) { |v| parse_tags(v) } : parse_tags(new_tags)
193
91
  end
194
92
 
195
93
  # Sets the `more` attribute.
@@ -197,21 +95,21 @@ module Mbrao
197
95
  # @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.
198
96
  # A empty or "*" will be the default value.
199
97
  def more=(new_more)
200
- @more = is_hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :ensure_string) : new_more.ensure_string
98
+ @more = hash?(new_more) ? new_more.ensure_hash(:indifferent, nil, :ensure_string) : new_more.ensure_string
201
99
  end
202
100
 
203
101
  # Sets the `author` attribute.
204
102
  #
205
103
  # @param new_author [Author|Hash|Object|NilClass] The new value for the attribute.
206
104
  def author=(new_author)
207
- if new_author.is_a?(Mbrao::Author) then
208
- @author = new_author
209
- elsif is_hash?(new_author) then
210
- new_author = new_author.ensure_hash(:indifferent)
211
- @author = Mbrao::Author.create(new_author)
212
- else
213
- @author = new_author ? Mbrao::Author.new(new_author.ensure_string) : nil
214
- end
105
+ @author =
106
+ if new_author.is_a?(Mbrao::Author)
107
+ new_author
108
+ elsif hash?(new_author) then
109
+ Mbrao::Author.create(new_author.ensure_hash(:indifferent))
110
+ else
111
+ new_author ? Mbrao::Author.new(new_author.ensure_string) : nil
112
+ end
215
113
  end
216
114
 
217
115
  # Sets the `created_at` attribute.
@@ -225,163 +123,119 @@ module Mbrao
225
123
  #
226
124
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
227
125
  def updated_at=(value)
228
- @updated_at = extract_datetime(value)
229
- @updated_at = @created_at if !@updated_at
230
- end
231
-
232
- # Gets metadata attribute.
233
- #
234
- # @return The metadata attribute.
235
- def metadata
236
- @metadata ||= HashWithIndifferentAccess.new
126
+ @updated_at = extract_datetime(value) || @created_at
237
127
  end
238
128
 
239
129
  # Sets the `metadata` attribute.
240
130
  #
241
131
  # @param new_metadata [Hash] The new value for the attribute.
242
132
  def metadata=(new_metadata)
243
- if is_hash?(new_metadata) then
244
- @metadata = new_metadata.ensure_hash(:indifferent)
245
- else
246
- @metadata = HashWithIndifferentAccess.new({raw: new_metadata})
247
- end
133
+ @metadata = hash?(new_metadata) ? new_metadata.ensure_hash(:indifferent) : @metadata = HashWithIndifferentAccess.new({raw: new_metadata})
248
134
  end
249
135
 
250
- # Returns the content as an Hash.
251
- #
252
- # @param options [Hash] Options to modify behavior of the serialization.
253
- # The only supported value are:
254
- #
255
- # * `:exclude`, an array of attributes to skip.
256
- # * `:exclude_empty`, if to exclude nil values. Default is `false`.
257
- # @return [Hash] An hash with all attributes.
258
- def as_json(options = {})
259
- keys = [:uid, :locales, :title, :summary, :body, :tags, :more, :author, :created_at, :updated_at, :metadata]
260
- ::Mbrao::Parser.as_json(self, keys, options)
261
- end
136
+ private
262
137
 
263
- # Validates locales for attribute retrieval.
138
+ # Extracts a date and time from a value.
264
139
  #
265
- # @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
266
- # used.
267
- # @param content [Content|nil] An optional content to check for availability
268
- # @return [Array] The validated list of locales.
269
- def self.validate_locales(locales, content = nil)
270
- locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
271
- locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
272
- raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
273
- locales
140
+ # @param value [String|DateTime|Fixnum] The value to parse.
141
+ # @return [DateTime] The extracted value.
142
+ def extract_datetime(value)
143
+ value = parse_datetime(value) if value
144
+ value ? value.utc : nil
145
+ rescue ArgumentError
146
+ raise Mbrao::Exceptions::InvalidDate
274
147
  end
275
148
 
276
- # Creates a content with metadata and body.
149
+ # Parses a datetime.
277
150
  #
278
- # @param metadata [Hash] The metadata.
279
- # @param body [String] The body of the content.
280
- # @return [Content] A new content.
281
- def self.create(metadata, body)
282
- rv = Mbrao::Content.new
283
- rv.body = body.ensure_string.strip
284
- assign_metadata(rv, metadata.symbolize_keys) if metadata.is_a?(Hash)
151
+ # @param value [String|DateTime|Fixnum] The value to parse.
152
+ # @return [DateTime] The extracted value.
153
+ def parse_datetime(value)
154
+ rv =
155
+ case value.class.to_s
156
+ when "DateTime" then value
157
+ when "Date", "Time" then value.to_datetime
158
+ when "Float", "Fixnum" then parse_datetime_number(value)
159
+ else parse_datetime_string(value)
160
+ end
161
+
162
+ raise ArgumentError unless rv
285
163
  rv
286
164
  end
287
165
 
288
- private
289
- # Assigns metadata to a content
290
- #
291
- # @param content [Content] The content to manipulate.
292
- # @param metadata [Hash] The metadata to assign.
293
- # @return [Content] The content with metadata.
294
- def self.assign_metadata(content, metadata)
295
- content.uid = metadata.delete(:uid)
296
- content.title = metadata.delete(:title)
297
- content.summary = metadata.delete(:summary)
298
- content.author = Mbrao::Author.create(metadata.delete(:author))
299
- content.tags = metadata.delete(:tags)
300
- content.more = metadata.delete(:more)
301
- content.created_at = metadata.delete(:created_at)
302
- content.updated_at = metadata.delete(:updated_at)
303
- content.locales = extract_locales(metadata)
304
- content.metadata = metadata
305
-
306
- content
307
- end
308
-
309
- # Extracts locales from metadata.
310
- #
311
- # @param metadata [Hash] The metadata that contains the locales.
312
- # @return [Array] The locales.
313
- def self.extract_locales(metadata)
314
- locales = metadata.delete(:locales)
315
- locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
316
- normalize_locales(locales)
317
- end
318
-
319
- # Normalizes locales for further usage.
320
- #
321
- # @param locales [Array] The locales to normalize.
322
- # @return [Array] The normalized locales.
323
- def self.normalize_locales(locales)
324
- locales.flatten.map(&:ensure_string).map(&:strip).uniq
325
- end
166
+ # Parses a datetime from a number.
167
+ #
168
+ # @param value [String] The value to parse.
169
+ # @return [Float|Fixnum] The extracted value.
170
+ def parse_datetime_number(value)
171
+ number = value.to_float
172
+ number > 0 ? Time.at(number).to_datetime : nil
173
+ end
326
174
 
327
- # Extracts a date and time from a value.
328
- #
329
- # @param value [String|DateTime|Fixnum] The value to parse.
330
- # @return [DateTime] The extracted value.
331
- def extract_datetime(value)
332
- begin
333
- value = parse_datetime(value) if value
334
- value ? value.utc : nil
335
- rescue ArgumentError
336
- raise Mbrao::Exceptions::InvalidDate.new
337
- end
338
- end
175
+ # Parses a datetime from a string.
176
+ #
177
+ # @param value [String] The value to parse.
178
+ # @return [DateTime] The extracted value.
179
+ def parse_datetime_string(value)
180
+ value = value.ensure_string
339
181
 
340
- # Parses a datetime.
341
- #
342
- # @param value [String|DateTime|Fixnum] The value to parse.
343
- # @return [DateTime] The extracted value.
344
- def parse_datetime(value)
345
- case value.class.to_s
346
- when "DateTime" then value
347
- when "Date", "Time" then value.to_datetime
348
- when "Float", "Fixnum" then
349
- value.to_float > 0 ? Time.at(value.to_float).to_datetime : raise(ArgumentError.new)
350
- else parse_datetime_string(value)
182
+ catch(:parsed) do
183
+ ALLOWED_DATETIME_FORMATS.find do |format|
184
+ begin
185
+ throw(:parsed, DateTime.strptime(value, format))
186
+ rescue
187
+ nil
188
+ end
351
189
  end
352
190
  end
191
+ end
353
192
 
354
- # Parses a datetime string.
355
- #
356
- # @param value [String] The value to parse.
357
- # @return [DateTime] The extracted value.
358
- def parse_datetime_string(value)
359
- value = value.ensure_string
193
+ # Extracts tags from an array, making sure all the comma separated strings are evaluated.
194
+ #
195
+ # @param value [String|Array] The string or array to parse.
196
+ # @return [Array] The list of tags.
197
+ def parse_tags(value)
198
+ value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) }
199
+ end
360
200
 
361
- catch(:parsed) do
362
- ALLOWED_DATETIME_FORMATS.each do |format|
363
- rv = DateTime.strptime(value, format) rescue nil
364
- throw(:parsed, rv) if rv
365
- end
201
+ # Check if value is an Hash.
202
+ #
203
+ # @param value [Object] The object to check.
204
+ # @return [Boolean] `true` if value is an Hash, `false` otherwise
205
+ def hash?(value)
206
+ value.is_a?(Hash)
207
+ end
366
208
 
367
- raise(ArgumentError.new)
368
- end
209
+ # Assigns metadata to a content
210
+ #
211
+ # @param content [Content] The content to manipulate.
212
+ # @param metadata [Hash] The metadata to assign.
213
+ # @return [Content] The content with metadata.
214
+ def self.assign_metadata(content, metadata)
215
+ [:uid, :title, :summary, :tags, :more, :created_at, :updated_at].each do |field|
216
+ content.send("#{field}=", metadata.delete(field))
369
217
  end
370
218
 
371
- # Extracts tags from an array, making sure all the comma separated strings are evaluated.
372
- #
373
- # @param value [String|Array] The string or array to parse.
374
- # @return [Array] The list of tags.
375
- def parse_tags(value)
376
- value.ensure_array(nil, true, true, true) { |v| v.ensure_string.split(/\s*,\s*/) }
377
- end
219
+ content.author = Mbrao::Author.create(metadata.delete(:author))
220
+ content.locales = extract_locales(metadata.delete(:locales))
221
+ content.metadata = metadata
222
+ end
378
223
 
379
- # Check if value is an Hash.
380
- #
381
- # @param value [Object] The object to check.
382
- # @return [Boolean] `true` if value is an Hash, `false` otherwise
383
- def is_hash?(value)
384
- value.is_a?(Hash)
385
- end
224
+ # Extracts locales from metadata.
225
+ #
226
+ # @param locales [String] The list of locales.
227
+ # @return [Array] The locales.
228
+ def self.extract_locales(locales)
229
+ locales = locales.ensure_string.split(/\s*,\s*/) unless locales.is_a?(::Array)
230
+ normalize_locales(locales)
231
+ end
232
+
233
+ # Normalizes locales for further usage.
234
+ #
235
+ # @param locales [Array] The locales to normalize.
236
+ # @return [Array] The normalized locales.
237
+ def self.normalize_locales(locales)
238
+ locales.flatten.map(&:ensure_string).map(&:strip).uniq
239
+ end
386
240
  end
387
241
  end