Class: Milk::Component

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

Constant Summary collapse

@@local_flag =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.add_field(klass, field, label, options = {}) ⇒ Object

Metaclass black magic to simulate appending items to a list This works by getting the old result of the fields class method and stores it in a closure, and then redefines the method, but with a new item appended.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/milk/component.rb', line 65

def self.add_field(klass, field, label, options={})

  # Merge in assumes options
  options[:field] = field
  options[:label] = label
  
  # Fill in blanks with defaults
  defaults.each do |k, v|
    options[k] ||= v
  end
  
  field = klass.new(options)
  
  newfields = self.fields + [field]
  meta_def("fields") do
    newfields
  end
end

.defaultsObject

Assume no defaults



57
58
59
# File 'lib/milk/component.rb', line 57

def self.defaults
  {}
end

.fieldsObject

All Components start out with default of no fields



52
53
54
# File 'lib/milk/component.rb', line 52

def self.fields
  []
end

.global_properties(*props) ⇒ Object



40
41
42
43
44
45
# File 'lib/milk/component.rb', line 40

def self.global_properties(*props)
  globals = props.collect{|name|"@#{name}".to_sym}
  class_def :global_properties do
    globals
  end
end

.local_properties(*props) ⇒ Object



31
32
# File 'lib/milk/component.rb', line 31

def self.local_properties(*props)
end

.method_missing(method, *args) ⇒ Object



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

def self.method_missing(method, *args)
  raise "Missing '#{method}' method" unless File.file? LIB_DIR+"/fields/#{method}.rb"
  klass = eval("Fields::" + method.to_s.gsub(/(^|_)(.)/) { $2.upcase })
  add_field(klass, *args)
end

.requires(*resources) ⇒ Object



34
35
36
37
38
# File 'lib/milk/component.rb', line 34

def self.requires(*resources)
  class_def :requirements do
    resources
  end
end

Instance Method Details

#appObject



19
20
21
# File 'lib/milk/component.rb', line 19

def app
  page.app
end

#edit(prefix) ⇒ Object



117
118
119
120
# File 'lib/milk/component.rb', line 117

def edit(prefix)
  @prefix = prefix
  haml("edit.component")
end

#load_settingsObject



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

def load_settings
  yaml_file = Milk::DATA_DIR + "/global/#{system_name}.yaml"
  if File.file? yaml_file
    YAML.load_file(yaml_file).each_pair do |key, value|
      instance_variable_set("@#{key}".to_sym, value)
    end
  end
end

#nameObject



47
48
49
# File 'lib/milk/component.rb', line 47

def name
  self.class.to_s.rpartition('::').last
end

#pageObject



23
24
25
26
27
28
29
# File 'lib/milk/component.rb', line 23

def page
  if @parent.class == Milk::Page
    @parent
  else
    @parent.page
  end
end

#save_settingsObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/milk/component.rb', line 90

def save_settings
  return unless respond_to? :global_properties
  yaml_file = Milk::DATA_DIR + "/global/#{system_name}.yaml"
  data = {}
  global_properties.each do |name|
    data[name.to_s.sub('@','')] = instance_variable_get(name)
  end
  
  File.open(yaml_file, "w") do |file|
    file.write(YAML.dump(data))
  end
end

#system_nameObject



103
104
105
# File 'lib/milk/component.rb', line 103

def system_name
  self.class.to_s.class_to_require.rpartition('/').last
end

#to_yaml_propertiesObject

Don’t store global properties or backreferences to the parent



11
12
13
14
15
16
17
# File 'lib/milk/component.rb', line 11

def to_yaml_properties
  (if respond_to? :global_properties
    instance_variables.reject { |name| global_properties.member?(name) }
  else
    instance_variables
  end).reject { |name| name == :@parent }
end

#viewObject



122
123
124
# File 'lib/milk/component.rb', line 122

def view
  haml("components/#{system_name}")
end