Class: Milk::Page

Inherits:
Object show all
Defined in:
lib/milk/page.rb

Constant Summary collapse

PATH_TO_NAME_REGEXP =
Regexp.new("#{Milk::DATA_DIR}\/pages\/(.*)\.yaml")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



4
5
6
# File 'lib/milk/page.rb', line 4

def components
  @components
end

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/milk/page.rb', line 6

def description
  @description
end

#pagenameObject (readonly)

Returns the value of attribute pagename.



7
8
9
# File 'lib/milk/page.rb', line 7

def pagename
  @pagename
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/milk/page.rb', line 8

def parent
  @parent
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/milk/page.rb', line 5

def title
  @title
end

Class Method Details

.each_pageObject



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

def self.each_page
  Dir.glob(Milk::DATA_DIR + "/pages/**/*.yaml").each do |yaml_file|
    p = load_file(yaml_file)
    Milk::Application.join_tree(p, nil)
    yield p
  end
end

.find(pagename) ⇒ Object

Raises:



29
30
31
32
33
# File 'lib/milk/page.rb', line 29

def self.find(pagename)
  yaml_file = Milk::DATA_DIR + "/pages/" + pagename + ".yaml"
  raise PageNotFoundError unless File.readable? yaml_file
  load_file(yaml_file, pagename)
end

.json_unserialize(data, pagename = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/milk/page.rb', line 53

def self.json_unserialize(data, pagename=nil)  
  class_name = data.delete('class')
  obj = class_name.constantize.allocate
  data.each do |key, value|
    if value.class == Array
      value.collect! { |item| json_unserialize(item) }
    end
    obj.instance_variable_set("@#{key}", value)
  end
  obj.instance_variable_set("@pagename", pagename) if obj.class == Milk::Page
  obj
end

.load_file(yaml_file, pagename = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/milk/page.rb', line 21

def self.load_file(yaml_file, pagename=nil)
  pagename ||= PATH_TO_NAME_REGEXP.match(yaml_file)[1]
  page = YAML.load_file(yaml_file)
  page.instance_variable_set('@pagename', pagename)
  page.load_settings
  page
end

Instance Method Details

#appObject



39
40
41
# File 'lib/milk/page.rb', line 39

def app
  @parent
end

#editObject



90
91
92
# File 'lib/milk/page.rb', line 90

def edit
  haml("edit")
end


108
109
110
111
112
113
114
# File 'lib/milk/page.rb', line 108

def link_to
  if @pagename == "Home"
    "/"
  else
    "/#{@pagename}"
  end
end

Returns:

  • (Boolean)


116
117
118
# File 'lib/milk/page.rb', line 116

def link_to?(url)
  (@pagename == "Home" && url == '/') || url == "/#{@pagename}"
end

#load_settingsObject



78
79
80
81
82
# File 'lib/milk/page.rb', line 78

def load_settings
  @components.each do |component|
    component.load_settings
  end
end

#previewObject



94
95
96
97
98
99
100
# File 'lib/milk/page.rb', line 94

def preview
  haml("view.page") do 
    (@components.collect do |component|
      component.view
    end).join("")
  end
end

#render_dependenciesObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/milk/page.rb', line 66

def render_dependencies
  deps = []
  @components.each do |component|
    if component.respond_to? :requirements
      component.requirements.each do |dep|
        deps << dep
      end
    end
  end
  app.render_dependencies deps
end

#saveObject



43
44
45
46
47
48
49
50
51
# File 'lib/milk/page.rb', line 43

def save
  save_settings
  yaml_file = Milk::DATA_DIR + "/pages/" + @pagename + ".yaml"
  data = YAML.dump(self)
  File.open(yaml_file, "w") do |file|
    file.write(data)
  end
  data
end

#save_settingsObject



84
85
86
87
88
# File 'lib/milk/page.rb', line 84

def save_settings
  @components.each do |component|
    component.save_settings
  end
end

#save_to_cache(html = nil) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/milk/page.rb', line 120

def save_to_cache(html=nil)
  html ||= view
  folder =  Milk::PUBLIC_DIR + "/cache/" + @pagename
  cache_file = folder + "/index.html"
  # Make the folder if it doesn't exist
  FileUtils.mkdir_p folder
  open(cache_file, "w") { |file| file.write html }
end

#to_yaml_propertiesObject



35
36
37
# File 'lib/milk/page.rb', line 35

def to_yaml_properties
  [:@components, :@title, :@keywords, :@description]
end

#viewObject



102
103
104
105
106
# File 'lib/milk/page.rb', line 102

def view
  haml("view") do 
    preview
  end
end