Skip to content
Browse files

Merge branch 'virtus-spike' of https://github.com/statianzo/imperator

…into virtus
  • Loading branch information...
2 parents 24243a1 + e2a6cc9 commit 73f78393ec193c03a5e0ea87fe8eb2285f85b935 Keith Gaddis committed
Showing with 26 additions and 31 deletions.
  1. +2 −1 imperator.gemspec
  2. +0 −1 lib/imperator.rb
  3. +15 −27 lib/imperator/command.rb
  4. +9 −2 spec/imperator/command_spec.rb
View
3 imperator.gemspec
@@ -23,5 +23,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "pry"
# s.add_runtime_dependency "rest-client"
s.add_runtime_dependency "uuidtools"
- s.add_runtime_dependency "active_attr"
+ s.add_runtime_dependency "activemodel"
+ s.add_runtime_dependency "virtus"
end
View
1 lib/imperator.rb
@@ -1,4 +1,3 @@
-require 'active_attr'
require "imperator/version"
require 'imperator/invalid_command_error'
require 'imperator/command'
View
42 lib/imperator/command.rb
@@ -1,18 +1,22 @@
require 'uuidtools'
+require 'active_model'
+require 'virtus'
class Imperator::Command
- include ActiveAttr::Model
+ include ActiveModel::Validations
extend ActiveModel::Callbacks
+ include Virtus
+
+ if defined? ActiveModel::Serializable
+ include ActiveModel::Serializable::JSON
+ include ActiveModel::Serializable::XML
+ else
+ include ActiveModel::Serializers::JSON
+ include ActiveModel::Serializers::Xml
+ end
define_model_callbacks :create, :perform, :initialize
- cattr_accessor :commit_mode
- attribute :id
-
- after_initialize :set_uuid
-
- class << self
- attr_accessor :perform_block
- end
+ attribute :id, String, :default => proc { UUIDTools::UUID.timestamp_create.to_s }
def self.action(&block)
define_method(:action, &block)
@@ -20,8 +24,8 @@ def self.action(&block)
alias_method :params, :attributes
- def as_json
- attributes.as_json
+ def as_json(*args)
+ attributes.as_json(*args)
end
def persisted?
@@ -69,20 +73,4 @@ def action
def perform
run_callbacks(:perform) { action }
end
-
- def method_missing(method, *args)
- method_root = method.to_s.gsub(/=$/, "")
- if method.to_s[/=$/]
- self.attributes[method_root] = args.first
- elsif attributes.has_key?(method_root)
- self.attributes[method]
- else
- super
- end
- end
-
- private
- def set_uuid
- self.id = UUIDTools::UUID.timestamp_create.to_s if self.id.nil?
- end
end
View
11 spec/imperator/command_spec.rb
@@ -41,8 +41,8 @@ class ActionDSLExampleCommand < Imperator::Command
describe "attributes" do
class AttributeCommand < Imperator::Command
- attribute :gets_default, :default => "foo"
- attribute :declared_attr
+ attribute :gets_default, String, :default => "foo"
+ attribute :declared_attr, String
end
it "throws away undeclared attributes in mass assignment" do
@@ -63,6 +63,13 @@ class AttributeCommand < Imperator::Command
command = AttributeCommand.new :gets_default => "bar"
command.gets_default.should == "bar"
end
+
+ it "will create attributes as json" do
+ command = AttributeCommand.new
+ command.as_json.should == {"id" => command.id,
+ "gets_default" => "foo",
+ "declared_attr" => nil}
+ end
end
end

0 comments on commit 73f7839

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