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