public
Description: Instrument is a simple library for producing dynamically generated "controls" with various templating languages.
Homepage: http://instrument.rubyforge.org/
Clone URL: git://github.com/sporkmonger/instrument.git
instrument / README
100644 98 lines (74 sloc) 2.488 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
== Instrument
 
Homepage:: instrument.rubyforge.org[http://instrument.rubyforge.org/]
Author:: Bob Aman (mailto:bob@sporkmonger.com)
Copyright:: Copyright © 2008 Day Automation Systems, Inc.
License:: MIT
 
== Description
 
Instrument is a simple library for producing dynamically generated "controls"
with various templating languages.
 
== Features
 
* Generate controls with Erb, Haml, Markaby, or XML Builder. Instrument
  doesn't care what template language you prefer.
* Output XHTML, XML, or JSON. Instrument doesn't care what your output
  format is.
 
== Example Usage
 
  select_control = SelectControl.new(:name => "base", :selections => [
    {:label => "One", :value => "1"},
    {:label => "Two", :value => "2"},
    {:label => "Three", :value => "3"},
    {:label => "Four", :value => "4"}
  ])
  xhtml_output = select_control.to_xhtml
 
or
 
  include Instrument::ControlBuilder
  
  select_control(:name => "base", :selections => [
    {:label => "One", :value => "1"},
    {:label => "Two", :value => "2"},
    {:label => "Three", :value => "3"},
    {:label => "Four", :value => "4"}
  ]).to_xhtml
 
select_control.rb:
 
  require "instrument"
 
  class SelectControl < Instrument::Control
    class Option
      def initialize(label, value)
        @label, @value = label, value
      end
  
      attr_accessor :label
      attr_accessor :value
    end
 
    def element_id
      return self.options[:id] || self.options[:name]
    end
 
    def element_name
      return self.options[:name]
    end
 
    def selections
      if !defined?(@selections) || @selections == nil
        @selections = []
        for selection in self.options[:selections]
          if selection.kind_of?(Hash)
            @selections << Option.new(selection[:label], selection[:value])
          else
            @selections << Option.new(selection, selection)
          end
        end
      end
      return @selections
    end
  end
  
select.xhtml.haml:
  
  %select{:id => element_id, :name => element_name}
    - for selection in selections
      %option{:value => selection.value}
        = selection.label
 
== Requirements
 
* Instrument has no explicit dependencies. If you want to output Haml, you
  will need the Haml library installed. Same goes for any of the other
  template languages.
 
== Install
 
* sudo gem install instrument
* sudo gem install haml (optional)
* sudo gem install erubis (optional)
* sudo gem install markaby (optional)
* sudo gem install builder (optional)