Class: MTIF::Post
- Inherits:
-
Object
show all
- Defined in:
- lib/mtif/posts.rb
Constant Summary
collapse
- SINGLE_VALUE_KEYS =
%w(author title status basename date unique_url body extended_body excerpt
keywords allow_comments allow_pings convert_breaks no_entry primary_category).map(&:to_sym)
- MULTILINE_KEYS =
%w(body extended_body excerpt keywords comment ping).map(&:to_sym)
- MULTIVALUE_KEYS =
%w(category tags comment ping).map(&:to_sym)
- CSV_KEYS =
%w(tags).map(&:to_sym)
- VALID_KEYS =
(SINGLE_VALUE_KEYS + MULTILINE_KEYS + MULTIVALUE_KEYS).sort.uniq
- DATE_FORMAT =
"%m/%d/%Y %I:%M:%S %p"
- FIELD_SEPARATOR =
'-----'
- POST_SEPARATOR =
'--------'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(content) ⇒ Post
Returns a new instance of Post.
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/mtif/posts.rb', line 44
def initialize(content)
@source = content
@data = {}
MULTIVALUE_KEYS.each do |key|
@data[key] = []
end
parse_source
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/mtif/posts.rb', line 106
def method_missing(method, *args, &block)
key = method.to_s.chomp('=').to_sym
if valid_key?(key)
if key == method
data[key]
else
data[key] = args.first
end
else
super
end
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5
6
7
|
# File 'lib/mtif/posts.rb', line 5
def data
@data
end
|
#source ⇒ Object
Returns the value of attribute source.
5
6
7
|
# File 'lib/mtif/posts.rb', line 5
def source
@source
end
|
Instance Method Details
#multiline_multivalue_keys ⇒ Object
#multiline_single_value_keys ⇒ Object
#single_line_multivalue_keys ⇒ Object
#single_line_single_value_keys ⇒ Object
#to_mtif ⇒ Object
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
|
# File 'lib/mtif/posts.rb', line 55
def to_mtif
result = []
single_line_single_value_keys.each do |key|
value = self.send(key)
next if value.nil? || (value.respond_to?(:empty) && value.empty?)
result << "#{mtif_key(key)}: #{mtif_value(value)}"
end
single_line_multivalue_keys.each do |key|
values = self.send(key)
next if values.nil? || (values.respond_to?(:empty) && values.empty?)
if CSV_KEYS.include?(key)
values = [
values.map{|v|
v.include?("\s") ? "\"#{v}\"" : v
}.join(',')
]
end
values.each do |value|
result << "#{mtif_key(key)}: #{mtif_value(value)}"
end
end
multiline_single_value_keys.each do |key|
value = self.send(key)
next if value.nil? || (value.respond_to?(:empty) && value.empty?)
result << FIELD_SEPARATOR
result << "#{mtif_key(key)}:\n#{mtif_value(value)}"
end
multiline_multivalue_keys.each do |key|
values = self.send(key)
next if values.nil? || (values.respond_to?(:empty) && values.empty?)
values.each do |value|
result << FIELD_SEPARATOR
result << "#{mtif_key(key)}:\n#{mtif_value(value)}"
end
end
result << FIELD_SEPARATOR unless result.last == FIELD_SEPARATOR result << POST_SEPARATOR result.join("\n") + "\n"
end
|
#valid_key?(key) ⇒ Boolean
24
25
26
|
# File 'lib/mtif/posts.rb', line 24
def valid_key?(key)
valid_keys.include?(key.to_sym)
end
|
#valid_keys ⇒ Object
20
21
22
|
# File 'lib/mtif/posts.rb', line 20
def valid_keys
VALID_KEYS
end
|