mbrao 1.4.4 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +29 -0
- data/.travis.yml +3 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -4
- data/doc/ActionView.html +125 -0
- data/doc/ActionView/Template.html +140 -0
- data/doc/ActionView/Template/Handlers.html +3 -3
- data/doc/ActionView/Template/Handlers/MbraoTemplate.html +26 -26
- data/doc/HTML.html +2 -2
- data/doc/HTML/Pipeline.html +2 -2
- data/doc/HTML/Pipeline/KramdownFilter.html +4 -4
- data/doc/Mbrao.html +3 -3
- data/doc/Mbrao/Author.html +29 -29
- data/doc/Mbrao/Content.html +1977 -3644
- data/doc/Mbrao/ContentInterface.html +817 -0
- data/doc/Mbrao/ContentInterface/ClassMethods.html +388 -0
- data/doc/Mbrao/Exceptions.html +1 -1
- data/doc/Mbrao/Exceptions/InvalidDate.html +1 -1
- data/doc/Mbrao/Exceptions/InvalidMetadata.html +1 -1
- data/doc/Mbrao/Exceptions/Parsing.html +1 -1
- data/doc/Mbrao/Exceptions/Rendering.html +1 -1
- data/doc/Mbrao/Exceptions/UnavailableLocalization.html +1 -1
- data/doc/Mbrao/Exceptions/Unimplemented.html +1 -1
- data/doc/Mbrao/Exceptions/UnknownEngine.html +1 -1
- data/doc/Mbrao/Parser.html +12 -12
- data/doc/Mbrao/ParserInterface.html +134 -0
- data/doc/Mbrao/ParserInterface/ClassMethods.html +1724 -0
- data/doc/Mbrao/ParserValidations.html +134 -0
- data/doc/Mbrao/ParserValidations/ClassMethods.html +348 -0
- data/doc/Mbrao/ParsingEngines.html +1 -1
- data/doc/Mbrao/ParsingEngines/Base.html +4 -4
- data/doc/Mbrao/ParsingEngines/PlainText.html +25 -15
- data/doc/Mbrao/RenderingEngines.html +1 -1
- data/doc/Mbrao/RenderingEngines/Base.html +2 -2
- data/doc/Mbrao/RenderingEngines/HtmlPipeline.html +173 -169
- data/doc/Mbrao/Version.html +3 -3
- data/doc/_index.html +51 -24
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/method_list.html +63 -69
- data/doc/top-level-namespace.html +2 -2
- data/lib/mbrao.rb +7 -2
- data/lib/mbrao/author.rb +5 -5
- data/lib/mbrao/content.rb +110 -256
- data/lib/mbrao/content_interface.rb +146 -0
- data/lib/mbrao/exceptions.rb +1 -1
- data/lib/mbrao/integrations/rails.rb +48 -42
- data/lib/mbrao/parser.rb +24 -176
- data/lib/mbrao/parser_interface.rb +143 -0
- data/lib/mbrao/parser_validations.rb +41 -0
- data/lib/mbrao/parsing_engines/base.rb +4 -4
- data/lib/mbrao/parsing_engines/plain_text.rb +136 -121
- data/lib/mbrao/rendering_engines/base.rb +2 -2
- data/lib/mbrao/rendering_engines/html_pipeline.rb +52 -77
- data/lib/mbrao/rendering_engines/html_pipeline/kramdown_filter.rb +31 -0
- data/lib/mbrao/version.rb +2 -2
- data/mbrao.gemspec +3 -3
- data/spec/mbrao/author_spec.rb +1 -1
- data/spec/mbrao/content_spec.rb +1 -1
- data/spec/mbrao/parser_spec.rb +16 -16
- data/spec/mbrao/rendering_engines/html_pipeline/kramdown_filter_spec.rb +28 -0
- data/spec/mbrao/rendering_engines/html_pipeline_spec.rb +0 -21
- 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
|
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"
|
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.
|
40
|
-
@website = Mbrao::Parser.
|
41
|
-
@image = Mbrao::Parser.
|
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)
|
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
|
-
|
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
|
-
|
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 =
|
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 =
|
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 =
|
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 =
|
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
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
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
|
-
|
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
|
-
|
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
|
-
#
|
138
|
+
# Extracts a date and time from a value.
|
264
139
|
#
|
265
|
-
# @param
|
266
|
-
#
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
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
|
-
#
|
149
|
+
# Parses a datetime.
|
277
150
|
#
|
278
|
-
# @param
|
279
|
-
# @
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
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
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
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
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
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
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
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
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
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
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
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
|
-
|
368
|
-
|
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
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
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
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
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
|