Class: Brite::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/brite/model.rb

Overview

Base class for all site classes.

Direct Known Subclasses

Layout, Page, Part

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, file, data = {}) ⇒ Model

Returns a new instance of Model.



8
9
10
11
12
13
# File 'lib/brite/model.rb', line 8

def initialize(site, file, data={})
  @site  = site
  @files = file

  update(date)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *a, &b) ⇒ Object



69
70
71
# File 'lib/brite/model.rb', line 69

def method_missing(name, *a, &b)
  instance_variable_get("@#{name}")
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



17
18
19
# File 'lib/brite/model.rb', line 17

def file
  @file
end

#siteObject (readonly)

Returns the value of attribute site.



15
16
17
# File 'lib/brite/model.rb', line 15

def site
  @site
end

Instance Method Details

#[](k) ⇒ Object

Add entry to settings data.



50
51
52
# File 'lib/brite/model.rb', line 50

def [](k)
  instance_variable_get("@#{k}")
end

#[]=(k, v) ⇒ Object

Add entry to settings data.



55
56
57
58
59
60
61
# File 'lib/brite/model.rb', line 55

def []=(k,v)
  if respond_to?("#{k}=")
    __send__("#{k}=", v)
  else
    instance_variable_set("@#{k}", v)
  end
end

#configObject



20
21
22
# File 'lib/brite/model.rb', line 20

def config
  site.config
end

#part(path) ⇒ Object

Render partial template.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/brite/model.rb', line 27

def part(path)
  @part ||= (
    partial = site.lookup_partial(path.to_s)
    #if path
    #  partial = site.parts.find{ |part|
    #    part.name == path
    #  }
      raise "no such part -- #{path} from #{file}" unless partial
      partial.render
    #else
    #  Part::Manager.new(self)
    #end
  )
end

#rendering_fieldsObject

Returns an Array of attribute/method names to be visible to the page rendering.



90
91
92
93
94
95
96
97
98
# File 'lib/brite/model.rb', line 90

def rendering_fields
  list = []
  instance_variables.each do |iv|
    name = iv.to_s.sub('@','')
    next if name.start_with?('_')
    list << name
  end
  list
end

#to_binding(&block) ⇒ Object

Returns a Binding for this instance.



74
75
76
# File 'lib/brite/model.rb', line 74

def to_binding(&block)
  binding
end

#to_hObject

Returns a Hash of rendering fields.



79
80
81
82
83
84
85
86
# File 'lib/brite/model.rb', line 79

def to_h
  hash = {}
  fields = rendering_fields
  fields.each do |field|
    hash[field.to_s] = __send__(field)
  end
  hash
end

#update(data) ⇒ Object



43
44
45
46
47
# File 'lib/brite/model.rb', line 43

def update(data)
  data.each do |k,v|
    self[k] = v
  end
end