Skip to content

Commit

Permalink
Add property class method and perform blocks instead of overriding
Browse files Browse the repository at this point in the history
perform
  • Loading branch information
karmajunkie committed Jan 17, 2012
1 parent 423dfb5 commit e4ee82e
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions lib/commando/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,41 @@ class Commando::Command
include ActiveModel::Serializers::JSON


attr_accessor :params
cattr_accessor :commit_mode
attr_accessor :attributes

class << self
attr_accessor :perform_block
end

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={})
self.params = HashWithIndifferentAccess.new params.dup
self.params[:id] = UUIDTools::UUID.timestamp_create.to_s if self.params[:id].nil?
@attributes = HashWithIndifferentAccess.new params.dup
@attributes[:id] = UUIDTools::UUID.timestamp_create.to_s if @attributes[:id].nil?
end

def attributes
params
alias_method :params, :attributes

def as_json
attributes.as_json
end

def persisted?
Expand All @@ -33,15 +58,15 @@ def commit
end

def dump
params.to_json
attributes.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))
self.attributes = HashWithIndifferentAccess.new(JSON.parse(command_string))
end

def perform!
Expand All @@ -50,23 +75,24 @@ def perform!
end

def perform
raise "You need to override #perform in #{self.class.name}"
raise "You need to define the perform block for #{self.class.name}" unless self.class.perform_block
debugger
self.instance_exec(&self.class.perform_block)
end

def id
params[:id]
end

def method_missing(method, *args)
puts "method missing"
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
if method.to_s[/=$/]
self.attributes[method_root] = args.first
elsif attributes.has_key?(method_root)
self.attributes[method]
else
super
#super
end
end
end

0 comments on commit e4ee82e

Please sign in to comment.