Class: InspectorGadget::GoogleGadgetSpec

Inherits:
Object
  • Object
show all
Extended by:
DslAttrAccessor
Defined in:
lib/inspectorgadget/google_gadget_spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DslAttrAccessor

dsl_attr_accessor

Constructor Details

#initialize(&block) ⇒ GoogleGadgetSpec

Returns a new instance of GoogleGadgetSpec.



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

def initialize(&block)
  @module_prefs = ModulePreferences.new
  @user_prefs = []
  @content_type = :html
  instance_eval(&block) if block_given?
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/inspectorgadget/google_gadget_spec.rb', line 40

def method_missing(name, *args)
  begin
    @module_prefs.send(name, *args)
  rescue NoMethodError
    super.send(name, *args)
  end
end

Instance Attribute Details

#user_prefsObject

Returns the value of attribute user_prefs.



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

def user_prefs
  @user_prefs
end

Instance Method Details

#content(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/inspectorgadget/google_gadget_spec.rb', line 19

def content(*args)
  return @content if args.empty?
  value = args.pop
  if value.is_a?(Hash) && value.key?(:url)
    @content_type, @content_href = :url, value[:url]
  else
    @content = value
  end
end

#module_prefs {|@module_prefs| ... } ⇒ Object

Yields:



29
30
31
32
# File 'lib/inspectorgadget/google_gadget_spec.rb', line 29

def module_prefs(&block)
  yield @module_prefs if block_given?
  @module_prefs
end

#to_xml(target = '') ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inspectorgadget/google_gadget_spec.rb', line 48

def to_xml(target = '')
  xml = Builder::XmlMarkup.new(:target => target, :indent => 2)
  xml.instruct!
  xml.Module do
    @module_prefs.to_xml(xml)
    @user_prefs.each {|user_pref| user_pref.to_xml(xml)}
    puts "Warning: Google will reject your gadget spec's content section because you specified a URL content type" if @content_type == :url && @content
    content_attrs = {}
    content_attrs[:type] = @content_type if @content_type
    content_attrs[:href] = @content_href if @content_href
    xml.Content content_attrs do
      xml.cdata! @content.is_a?(IO) ? @content.readlines.join : @content.to_s
    end
  end
  target
end

#user_pref(*args, &block) ⇒ Object



34
35
36
37
38
# File 'lib/inspectorgadget/google_gadget_spec.rb', line 34

def user_pref(*args, &block)
  up = UserPreference.new(*args)
  up.instance_eval(&block) if block_given?
  @user_prefs << up
end