Class: Middleman::Webcomic::Comic

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-webcomic/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, app) ⇒ Comic

Returns a new instance of Comic.



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
# File 'lib/middleman-webcomic/data.rb', line 97

def initialize(path, app)
  @app= app
  settings= app.settings
  @path= path
  @source= ""
  @content= ""
  @next= nil
  @prev= nil
  @first= nil
  @last= nil
  @metadata= ::Thor::CoreExt::HashWithIndifferentAccess.new({})
  @metadata[:ext]= File.extname(path)
  @metadata[:publish_date]= Date.today #Time.now.strftime "%Y-%m-%d"
  @metadata[:slug]= File.basename(path).gsub( @metadata[:ext], '').gsub('.comic','')

  if /^([\d]{2})([\d]{2})([\d]{2})\-(.*)(\.comic#{@metadata[:ext]})$/ =~ File.basename(path)
    year= "20#{ $1 }"
    month= $2
    day= $3
    @metadata[:publish_date]= Date.parse "#{year}-#{month}-#{day}"
    @metadata[:slug]= $4
  end

  @metadata[:title]= @metadata[:slug].titleize rescue @metadata[:slug]
  @metadata[:slug]= @metadata[:slug].downcase

  # Parse yaml header...
  begin
    read_yaml()
  rescue
    puts "Error reading YAML! #{self.slug}"
  end

  if self.filename.nil?
    throw "Filename Missing: Comics must define a filename! (#{self.slug})"
  end

  if publishable?
    # Dont' waste time if it's not a publishable comic
    engine= Tilt[path].new { @source }
    @content= engine.render
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, value = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/middleman-webcomic/data.rb', line 149

def method_missing(key, value=nil)
  if @metadata.has_key? key
    @metadata[key]
  elsif @metadata.has_key? key.to_s
    @metadata[key.to_s]
  else
    #super Should it throw an error?
    nil
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def content
  @content
end

#firstObject

Returns the value of attribute first.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def first
  @first
end

#lastObject

Returns the value of attribute last.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def last
  @last
end

#metadataObject

Returns the value of attribute metadata.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def 
  @metadata
end

#nextObject

Returns the value of attribute next.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def next
  @next
end

#pathObject

Returns the value of attribute path.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def path
  @path
end

#prevObject

Returns the value of attribute prev.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def prev
  @prev
end

#sourceObject

Returns the value of attribute source.



95
96
97
# File 'lib/middleman-webcomic/data.rb', line 95

def source
  @source
end

Instance Method Details

#[](key) ⇒ Object



141
142
143
# File 'lib/middleman-webcomic/data.rb', line 141

def [](key)
  @metadata[key]
end

#[]=(key, value) ⇒ Object



144
145
146
147
# File 'lib/middleman-webcomic/data.rb', line 144

def []=(key, value)
  #puts "Setting #{key} to #{value}"
  @metadata[key]= value
end

#pub_dateObject



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/middleman-webcomic/data.rb', line 186

def pub_date
  date= self.publish_on || self.publish_date || self.date
  case date
  when String
    Date.parse(date)
  when Time
    Date.parse(date.strftime('%Y-%m-%d'))
  when Date
    date
  else
    Date.today
  end
end

#publishable?Boolean

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/middleman-webcomic/data.rb', line 165

def publishable?
  @is_publishable ||= begin
    now= Time.now
    now_string = now.strftime "%Y-%m-%d"
    today= Date.parse(now_string)
    pubdate= self.pub_date
    case pubdate
    when String
      self['publish_date']= Date.parse(pubdate)
      pubdate <= now_string
    when Time
      self['publish_date']= Date.parse(pubdate.strftime('%Y-%m-%d'))
      pubdate <= now
    when Date
      pubdate <= today
    else
      true
    end
  end
end

#to_json(*a) ⇒ Object



160
161
162
163
# File 'lib/middleman-webcomic/data.rb', line 160

def to_json *a
  data= @metadata.reject {|k,v| k == :ext or k == "ext" }
  data.to_json *a
end