Class: Dynasnip

Inherits:
Vanilla::Renderers::Base show all
Defined in:
lib/vanilla/dynasnip.rb

Instance Attribute Summary

Attributes inherited from Vanilla::Renderers::Base

#app

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vanilla::Renderers::Base

escape_curly_braces, #include_snips, #initialize, #prepare, #process_text, #raw_content, render, #render, #render_without_including_snips, snip_regexp

Constructor Details

This class inherits a constructor from Vanilla::Renderers::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/vanilla/dynasnip.rb', line 71

def method_missing(method, *args)
  if snip = Vanilla.snip(snip_name)
    snip.get_value(method)
  elsif part = self.class.attribute(method)
    part
  else
    super
  end
end

Class Method Details

.allObject



6
7
8
# File 'lib/vanilla/dynasnip.rb', line 6

def self.all
  ObjectSpace.enum_for(:each_object, class << self; self; end).to_a - [self]
end

.attribute(attribute_name, attribute_value = nil) ⇒ Object



22
23
24
25
26
# File 'lib/vanilla/dynasnip.rb', line 22

def self.attribute(attribute_name, attribute_value=nil)
  @attributes ||= {}
  @attributes[attribute_name.to_sym] = attribute_value if attribute_value
  @attributes[attribute_name.to_sym]
end

.build_snipObject



38
39
40
# File 'lib/vanilla/dynasnip.rb', line 38

def self.build_snip
  Snip.new(snip_attributes)
end

.persist!(overwrite = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vanilla/dynasnip.rb', line 47

def self.persist!(overwrite=false)
  if overwrite
    snip = Soup[snip_name]
    if snip
      if snip.is_a?(Array)
        snip.each { |s| s.destroy }
      else
        snip.destroy
      end
    end
  end
  snip = Soup[snip_name]
  snip = snip[0] if snip.is_a?(Array)    
  if snip
    snip_attributes.each do |name, value|
      snip.set_value(name, value)
    end
  else
    snip = build_snip
  end
  snip.save
  snip
end

.persist_all!(overwrite = false) ⇒ Object



32
33
34
35
36
# File 'lib/vanilla/dynasnip.rb', line 32

def self.persist_all!(overwrite=false)
  all.each do |dynasnip|
    dynasnip.persist!(overwrite)
  end
end

.snip_attributesObject



42
43
44
45
# File 'lib/vanilla/dynasnip.rb', line 42

def self.snip_attributes
  full_snip_attributes = {:name => snip_name, :content => self.name, :render_as => "Ruby"}
  @attributes ? full_snip_attributes.merge!(@attributes) : full_snip_attributes
end

.snip_name(new_name = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vanilla/dynasnip.rb', line 10

def self.snip_name(new_name=nil)
  if new_name
    @snip_name = new_name.to_s
  else
    # borrowed from ActiveSupport
    @snip_name ||= self.name.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
  end
end

.usage(str) ⇒ Object



28
29
30
# File 'lib/vanilla/dynasnip.rb', line 28

def self.usage(str)
  attribute :usage, escape_curly_braces(str).strip
end