Permalink
Browse files

Refactor to use ActiveAttr for ActiveModel support

  • Loading branch information...
1 parent c4c93e6 commit 22c663360e592455e675df736302254bf3820ca5 @karmajunkie committed Feb 23, 2012
Showing with 22 additions and 31 deletions.
  1. +1 −0 commando.gemspec
  2. +1 −0 lib/commando.rb
  3. +20 −31 lib/commando/command.rb
View
@@ -21,4 +21,5 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
# s.add_runtime_dependency "rest-client"
+ s.add_runtime_dependency "active_attr"
end
View
@@ -1,3 +1,4 @@
+require 'active_attr'
require "commando/version"
require 'commando/invalid_command_error'
require 'commando/command'
View
@@ -1,13 +1,13 @@
class Commando::Command
- include ActiveModel::Naming
- include ActiveModel::Validations
- include ActiveModel::Conversion
- include ActiveModel::Serialization
- include ActiveModel::Serializers::JSON
+ include ActiveAttr::Model
+ extend ActiveModel::Callbacks
+ define_model_callbacks :create, :perform, :initialize
cattr_accessor :commit_mode
- attr_accessor :attributes
+ attribute :id
+
+ after_initialize :set_uuid
class << self
attr_accessor :perform_block
@@ -17,26 +17,6 @@ def self.perform(&block)
@perform_block = block
end
- def self.property(property_name)
-
- define_method(property_name) do
- @attributes[property_name]
- end
- define_method("#{property_name}=") do |val|
- @attributes[property_name] = val
- end
- end
-
- def read_attribute_for_validation(key)
- @attributes[key]
- end
-
-
- def initialize(params={})
- @attributes = HashWithIndifferentAccess.new params.dup
- @attributes[:id] = UUIDTools::UUID.timestamp_create.to_s if @attributes[:id].nil?
- end
-
alias_method :params, :attributes
def as_json
@@ -57,6 +37,12 @@ def commit
self.perform
end
+ def initialize(*)
+ run_callbacks :initialize do
+ super
+ end
+ end
+
def dump
attributes.to_json
end
@@ -76,11 +62,9 @@ def perform!
def perform
raise "You need to define the perform block for #{self.class.name}" unless self.class.perform_block
- self.instance_exec(&self.class.perform_block)
- end
-
- def id
- params[:id]
+ run_callbacks :perform do
+ self.instance_exec(&self.class.perform_block)
+ end
end
def method_missing(method, *args)
@@ -93,4 +77,9 @@ def method_missing(method, *args)
super
end
end
+
+ private
+ def set_uuid
+ self.id = UUIDTools::UUID.timestamp_create.to_s if self.id.nil?
+ end
end

0 comments on commit 22c6633

Please sign in to comment.