Class: Gumdrop::Content

Inherits:
Object
  • Object
show all
Includes:
Util::SiteAccess
Defined in:
lib/gumdrop/content.rb

Overview

All content (layouts, partials, images, html, js css, etc) found in the source directory are represented as a Content object in memory

Constant Summary collapse

KNOWN_BINARY =
%w(.jpe .jpeg .jpg .png .tiff .gif)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::SiteAccess

#parent, #site

Methods included from Util::Loggable

#log

Methods included from Util::Eventable

#clear_events, #event_block, #fire

Constructor Details

#initialize(source_path, generator = nil, &block) ⇒ Content

Returns a new instance of Content.



13
14
15
16
17
18
19
# File 'lib/gumdrop/content.rb', line 13

def initialize(source_path, generator=nil, &block)
  @source_path= source_path
  @generator= generator
  @ignore= false
  @block= block
  @params= Util::HashObject.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/gumdrop/content.rb', line 140

def method_missing(sym, *args, &block)
  if params.has_key? sym
    params[sym]
  else
    super
  end
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



11
12
13
# File 'lib/gumdrop/content.rb', line 11

def params
  @params
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



11
12
13
# File 'lib/gumdrop/content.rb', line 11

def source_path
  @source_path
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gumdrop/content.rb', line 74

def binary?
  @is_binary ||= begin
    if generated? or has_block? or missing?
      false
    elsif KNOWN_BINARY.include? ext or site.unrenderable? path
      true
    else
      # from ptools
      s = (File.read(source_path, File.stat(source_path).blksize) || "").split(//)
      ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
    end
  end
end

#bodyObject

Memoize this?



117
118
119
120
121
122
123
124
125
126
# File 'lib/gumdrop/content.rb', line 117

def body # Memoize this?
  @body ||= case
    when has_block?
      @block.call
    when missing?, binary?
      nil
    else
      File.read @source_path
    end
end

#clean_uriObject



70
71
72
# File 'lib/gumdrop/content.rb', line 70

def clean_uri
  @clean_uri ||= uri.gsub(ext, '')
end

#dirnameObject



50
51
52
# File 'lib/gumdrop/content.rb', line 50

def dirname
  @dirname ||= File.dirname source_path
end

#exists?Boolean

Can I call #body() ?

Returns:

  • (Boolean)


89
90
91
# File 'lib/gumdrop/content.rb', line 89

def exists?
  has_block? or File.exists? @source_path
end

#extObject



58
59
60
# File 'lib/gumdrop/content.rb', line 58

def ext
  @ext ||= File.extname filename
end

#filenameObject



46
47
48
# File 'lib/gumdrop/content.rb', line 46

def filename
  @filename ||= _target_filename
end

#generated?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/gumdrop/content.rb', line 97

def generated?
  !@generator.nil?
end

#generator?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/gumdrop/content.rb', line 113

def generator?
  ext == '.generator'
end

#has_block?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/gumdrop/content.rb', line 101

def has_block?
  !@block.nil?
end

#layout?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/gumdrop/content.rb', line 109

def layout?
  ext == '.layout'
end

#levelObject



38
39
40
# File 'lib/gumdrop/content.rb', line 38

def level
  @level ||= (path.split('/').length - 1)
end

#missing?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/gumdrop/content.rb', line 93

def missing?
  !exists?
end

#mtimeObject



128
129
130
131
132
133
134
# File 'lib/gumdrop/content.rb', line 128

def mtime
  @mtime ||= if exists? and !generated?
      File.mtime @source_path
    else
      Time.now
    end
end

#partial?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/gumdrop/content.rb', line 105

def partial?
  source_filename[0] == "_"
end

#pathObject



34
35
36
# File 'lib/gumdrop/content.rb', line 34

def path
  @path ||= _source_path
end

#slugObject



29
30
31
32
# File 'lib/gumdrop/content.rb', line 29

def slug
  @slug ||= uri.gsub('/', '-').gsub(ext, '')
  @params.slug || @slug
end

#source_filenameObject



42
43
44
# File 'lib/gumdrop/content.rb', line 42

def source_filename
  @source_filename ||= File.basename source_path
end

#to_sObject



136
137
138
# File 'lib/gumdrop/content.rb', line 136

def to_s
  uri
end

#typeObject



54
55
56
# File 'lib/gumdrop/content.rb', line 54

def type
  @type ||= File.extname source_filename
end

#uriObject



62
63
64
# File 'lib/gumdrop/content.rb', line 62

def uri
  @uri ||= _uri
end

#uripathObject



66
67
68
# File 'lib/gumdrop/content.rb', line 66

def uripath
  @uripath ||= File.dirname uri
end