Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit bc7ac694ffd66a37270f1ef0d165cbe88871fed9 @karmajunkie karmajunkie committed Nov 28, 2011
Showing with 122 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +1 −0 Rakefile
  4. +24 −0 commando.gemspec
  5. +7 −0 lib/commando.rb
  6. +76 −0 lib/commando/command.rb
  7. +3 −0 lib/commando/invalid_command_error.rb
  8. +3 −0 lib/commando/version.rb
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in commando.gemspec
+gemspec
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "commando/version"
+
+Gem::Specification.new do |s|
+ s.name = "commando"
+ s.version = Commando::VERSION
+ s.authors = ["karmajunkie"]
+ s.email = ["keith.gaddis@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "commando"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ # s.add_development_dependency "rspec"
+ # s.add_runtime_dependency "rest-client"
+end
@@ -0,0 +1,7 @@
+require "commando/version"
+require 'commando/invalid_command_error'
+require 'commando/command'
+
+module Commando
+ # Your code goes here...
+end
@@ -0,0 +1,76 @@
+class Commando::Command
+ include ActiveModel::Naming
+ include ActiveModel::Validations
+ include ActiveModel::Conversion
+ include ActiveModel::Serialization
+ include ActiveModel::Serializers::JSON
+
+
+ attr_accessor :params
+ cattr_accessor :commit_mode
+
+ def initialize(params={})
+ self.params = HashWithIndifferentAccess.new params.dup
+ self.params[:id] = UUIDTools::UUID.timestamp_create.to_s if self.params[:id].nil?
+ end
+
+ def valid?
+ true
+ end
+
+ def attributes
+ params
+ end
+
+ def persisted?
+ false
+ end
+
+ def commit!
+ raise Commando::InvalidCommandError.new "Command was invalid" unless valid?
+ self.commit
+ end
+
+ def commit
+ #TODO: background code for this
+ self.perform
+ end
+
+ def dump
+ params.to_json
+ end
+
+ def self.load(command_string)
+ self.new(JSON.parse(command_string))
+ end
+
+ def load(command_string)
+ self.params = HashWithIndifferentAccess.new(JSON.parse(command_string))
+ end
+
+ def perform!
+ raise InvalidCommandError.new "Command was invalid" unless valid?
+ self.perform
+ end
+
+ def perform
+ raise "You need to override #perform in #{self.class.name}"
+ end
+
+ def id
+ params[:id]
+ end
+
+ def method_missing(method, *args)
+ method_root = method.to_s.gsub(/=$/, "")
+ if params.has_key?(method_root)
+ if method.to_s[/=$/]
+ self.params[method_root] = args.first
+ else
+ self.params[method]
+ end
+ else
+ super
+ end
+ end
+end
@@ -0,0 +1,3 @@
+class InvalidCommandError < ArgumentError
+
+end
@@ -0,0 +1,3 @@
+module Commando
+ VERSION = "0.0.1"
+end

0 comments on commit bc7ac69

Please sign in to comment.