Class: Template

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/template.rb

Direct Known Subclasses

Fiona::SettingTemplate, SettingsTemplate

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/template.rb', line 16

def method_missing(method, *args, &block)
  if method.to_s =~ /=$/
    method = method.to_s.gsub(/=$/, '').to_sym
    processed_attributes[method] = args[0]
    return args[1]
  else
    method = method.to_sym
    if processed_attributes.keys.include?(method)
      return processed_attributes[method]
    elsif default_attributes.keys.include?(method)
      return default_attributes[method]
    end
  end

  super
end

Class Method Details

.deserialize_attribute(value) ⇒ Object



12
13
14
# File 'lib/template.rb', line 12

def self.deserialize_attribute(value)
  return ActiveSupport::JSON.decode(value)['json']
end

.serialize_attribute(value) ⇒ Object



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

def self.serialize_attribute(value)
  return ActiveSupport::JSON.encode({:json => value})
end

Instance Method Details

#all_attributesObject



78
79
80
# File 'lib/template.rb', line 78

def all_attributes
  return default_attributes.dup.merge(processed_attributes)
end

#default_attributesObject



61
62
63
# File 'lib/template.rb', line 61

def default_attributes
  return @default_attributes || {}
end

#default_attributes=(defaults) ⇒ Object



65
66
67
# File 'lib/template.rb', line 65

def default_attributes=(defaults)
  @default_attributes = defaults
end

#delete_attribute(attribute) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/template.rb', line 69

def delete_attribute(attribute)
  if processed_attributes.include?(attribute)
    raw_attributes.where(:key => attribute).map{ |a| a.destroy }
    return processed_attributes.delete(attribute)
  else
    return nil
  end
end

#processed_attributesObject



46
47
48
49
# File 'lib/template.rb', line 46

def processed_attributes
  return @processed_attributes if @processed_attributes
  return rebuild_processed_attributes
end

#rebuild_processed_attributesObject



51
52
53
54
55
56
57
58
59
# File 'lib/template.rb', line 51

def rebuild_processed_attributes
  @processed_attributes = {}

  raw_attributes.each do |ra|
    @processed_attributes[ra.key.to_sym] = Template.deserialize_attribute(ra.value)
  end

  return @processed_attributes
end

#save_attributesObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/template.rb', line 33

def save_attributes
  return unless @processed_attributes
  raise 'You must first save the template.' unless id

  @processed_attributes.each do |key, val|
    TemplateAttribute.transaction do
      raw_attr = TemplateAttribute.find_or_initialize_by_template_id_and_key(id, key)
      raw_attr.value = Template.serialize_attribute(val)
      raw_attr.save!
    end
  end
end