Module: StdCmd::Set

Defined in:
lib/command-set/standard-commands.rb

Constant Summary collapse

@@set =
nil

Class Method Summary collapse

Class Method Details

.define_commandsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/command-set/standard-commands.rb', line 154

def self.define_commands
  return @@set ||= Command::CommandSet.define_commands do
    command(:set) do
      optional.multiword_argument(:address) do |terms, subject|
        last_word = terms.pop
        hash = value_set(terms, subject)
        if(Hash === hash)
          return hash.keys.grep(%r{^#{last_word}.*})
        else
          return []
        end
      end

      optional.argument(:value, "VALUE")

      def value_set(terms, subject)
        knobs = subject.knobs
        return [] if knobs.nil?

        terms.each do |term|
          unless knobs.respond_to? :[]
            raise Command::CommandError, "can't find settings under #{term}" 
          end
          knobs = knobs[term]
        end

        return knobs
      end

      define_method :value_set do |terms, subject|
        self.class.value_set(terms, subject)
      end

      subject_methods :knobs

      #TODO: This is a big mess in here
      #AFAICT, there should be an address, followed by an optional name
      #followed by an optional value...

      action do
        if address.nil?
          dont_undo
          puts subject.knobs.keys.sort.join("\n")
          return
        else
          @knob_name = address.pop 
          @knobs = value_set(address, subject)
        end

        if Hash === @knobs[@knob_name]
          dont_undo 
          puts @knobs[@knob_name].keys.sort.join("\n")
          return
        elsif @knobs.has_key? @knob_name
          if value.nil?
            dont_undo
            puts "#@knob_name: #{@knobs[@knob_name].inspect}"
          else
            @original_value = @knobs[@knob_name]
            @knobs[@knob_name]=value
            #I'm only keeping this because I trust you meant something
            #@knobs[@knob_name]=convert_value(@original_value, value)
          end
        end
      end 

      undo do
        @knobs[@knob_name] = @original_value
      end

      document <<-EOH
    Without arugments, lists the available variables.

    With one argument, prints the value of the variable <name>.

    Sets <name> to <value>.  Most settings should be obvious.  But 
    some important ones probably won't be.  
    EOH
    end
  end  
end