Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor to use ActiveAttr for ActiveModel support

  • Loading branch information...
commit 22c663360e592455e675df736302254bf3820ca5 1 parent c4c93e6
Keith Gaddis authored
1  commando.gemspec
@@ -21,4 +21,5 @@ Gem::Specification.new do |s|
21 21 # specify any dependencies here; for example:
22 22 # s.add_development_dependency "rspec"
23 23 # s.add_runtime_dependency "rest-client"
  24 + s.add_runtime_dependency "active_attr"
24 25 end
1  lib/commando.rb
... ... @@ -1,3 +1,4 @@
  1 +require 'active_attr'
1 2 require "commando/version"
2 3 require 'commando/invalid_command_error'
3 4 require 'commando/command'
51 lib/commando/command.rb
... ... @@ -1,13 +1,13 @@
1 1 class Commando::Command
2   - include ActiveModel::Naming
3   - include ActiveModel::Validations
4   - include ActiveModel::Conversion
5   - include ActiveModel::Serialization
6   - include ActiveModel::Serializers::JSON
  2 + include ActiveAttr::Model
  3 + extend ActiveModel::Callbacks
7 4
  5 + define_model_callbacks :create, :perform, :initialize
8 6
9 7 cattr_accessor :commit_mode
10   - attr_accessor :attributes
  8 + attribute :id
  9 +
  10 + after_initialize :set_uuid
11 11
12 12 class << self
13 13 attr_accessor :perform_block
@@ -17,26 +17,6 @@ def self.perform(&block)
17 17 @perform_block = block
18 18 end
19 19
20   - def self.property(property_name)
21   -
22   - define_method(property_name) do
23   - @attributes[property_name]
24   - end
25   - define_method("#{property_name}=") do |val|
26   - @attributes[property_name] = val
27   - end
28   - end
29   -
30   - def read_attribute_for_validation(key)
31   - @attributes[key]
32   - end
33   -
34   -
35   - def initialize(params={})
36   - @attributes = HashWithIndifferentAccess.new params.dup
37   - @attributes[:id] = UUIDTools::UUID.timestamp_create.to_s if @attributes[:id].nil?
38   - end
39   -
40 20 alias_method :params, :attributes
41 21
42 22 def as_json
@@ -57,6 +37,12 @@ def commit
57 37 self.perform
58 38 end
59 39
  40 + def initialize(*)
  41 + run_callbacks :initialize do
  42 + super
  43 + end
  44 + end
  45 +
60 46 def dump
61 47 attributes.to_json
62 48 end
@@ -76,11 +62,9 @@ def perform!
76 62
77 63 def perform
78 64 raise "You need to define the perform block for #{self.class.name}" unless self.class.perform_block
79   - self.instance_exec(&self.class.perform_block)
80   - end
81   -
82   - def id
83   - params[:id]
  65 + run_callbacks :perform do
  66 + self.instance_exec(&self.class.perform_block)
  67 + end
84 68 end
85 69
86 70 def method_missing(method, *args)
@@ -93,4 +77,9 @@ def method_missing(method, *args)
93 77 super
94 78 end
95 79 end
  80 +
  81 + private
  82 + def set_uuid
  83 + self.id = UUIDTools::UUID.timestamp_create.to_s if self.id.nil?
  84 + end
96 85 end

0 comments on commit 22c6633

Please sign in to comment.
Something went wrong with that request. Please try again.