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

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.



10
11
12
13
14
15
16
# File 'lib/gumdrop/content.rb', line 10

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



122
123
124
125
126
127
128
# File 'lib/gumdrop/content.rb', line 122

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.



8
9
10
# File 'lib/gumdrop/content.rb', line 8

def params
  @params
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



8
9
10
# File 'lib/gumdrop/content.rb', line 8

def source_path
  @source_path
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gumdrop/content.rb', line 58

def binary?
  @is_binary ||= begin
    if generated? or has_block? or missing?
      false
    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?



99
100
101
102
103
104
105
106
107
108
# File 'lib/gumdrop/content.rb', line 99

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

#exists?Boolean

Can I call #body() ?

Returns:

  • (Boolean)


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

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

#extObject



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

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

#filenameObject



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

def filename
  @filename ||= _target_filename
end

#generated?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gumdrop/content.rb', line 79

def generated?
  !@generator.nil?
end

#generator?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/gumdrop/content.rb', line 95

def generator?
  ext == '.generator'
end

#has_block?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/gumdrop/content.rb', line 83

def has_block?
  !@block.nil?
end

#layout?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/gumdrop/content.rb', line 91

def layout?
  ext == '.layout'
end

#levelObject



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

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

#missing?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/gumdrop/content.rb', line 75

def missing?
  !exists?
end

#mtimeObject



110
111
112
113
114
115
116
# File 'lib/gumdrop/content.rb', line 110

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

#partial?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/gumdrop/content.rb', line 87

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

#pathObject



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

def path
  @path ||= _source_path
end

#slugObject



26
27
28
# File 'lib/gumdrop/content.rb', line 26

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

#source_filenameObject



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

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

#to_sObject



118
119
120
# File 'lib/gumdrop/content.rb', line 118

def to_s
  uri
end

#typeObject



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

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

#uriObject



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

def uri
  @uri ||= _uri
end