Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial version

  • Loading branch information...
commit ad0e9c3e9223723bf1494ca8ac7ddbc6d3e07d8c 0 parents
Jack Christensen authored
17 .gitignore
... ... @@ -0,0 +1,17 @@
  1 +*.gem
  2 +*.rbc
  3 +.bundle
  4 +.config
  5 +.yardoc
  6 +Gemfile.lock
  7 +InstalledFiles
  8 +_yardoc
  9 +coverage
  10 +doc/
  11 +lib/bundler/man
  12 +pkg
  13 +rdoc
  14 +spec/reports
  15 +test/tmp
  16 +test/version_tmp
  17 +tmp
1  .rspec
... ... @@ -0,0 +1 @@
  1 +--colour
4 Gemfile
... ... @@ -0,0 +1,4 @@
  1 +source 'https://rubygems.org'
  2 +
  3 +# Specify your gem's dependencies in command_model.gemspec
  4 +gemspec
9 Guardfile
... ... @@ -0,0 +1,9 @@
  1 +# A sample Guardfile
  2 +# More info at https://github.com/guard/guard#readme
  3 +
  4 +guard 'rspec', :version => 2 do
  5 + watch(%r{^spec/.+_spec\.rb$})
  6 + watch(%r{^lib/command_model/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
  7 + watch('spec/spec_helper.rb') { "spec" }
  8 +end
  9 +
22 LICENSE
... ... @@ -0,0 +1,22 @@
  1 +Copyright (c) 2012 Jack Christensen
  2 +
  3 +MIT License
  4 +
  5 +Permission is hereby granted, free of charge, to any person obtaining
  6 +a copy of this software and associated documentation files (the
  7 +"Software"), to deal in the Software without restriction, including
  8 +without limitation the rights to use, copy, modify, merge, publish,
  9 +distribute, sublicense, and/or sell copies of the Software, and to
  10 +permit persons to whom the Software is furnished to do so, subject to
  11 +the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be
  14 +included in all copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18 +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19 +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20 +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21 +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22 +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 README.md
Source Rendered
... ... @@ -0,0 +1,32 @@
  1 +# CommandModel
  2 +
  3 +CommandModel is a model implementation using ActiveModel to represent commands
  4 +and the results of their execution.
  5 +
  6 +## Installation
  7 +
  8 +Add this line to your application's Gemfile:
  9 +
  10 + gem 'command_model'
  11 +
  12 +And then execute:
  13 +
  14 + $ bundle
  15 +
  16 +Or install it yourself as:
  17 +
  18 + $ gem install command_model
  19 +
  20 +## Usage
  21 +
  22 +TODO: Write usage instructions here
  23 +
  24 +Example(s) User#change_login
  25 +
  26 +## Contributing
  27 +
  28 +1. Fork it
  29 +2. Create your feature branch (`git checkout -b my-new-feature`)
  30 +3. Commit your changes (`git commit -am 'Added some feature'`)
  31 +4. Push to the branch (`git push origin my-new-feature`)
  32 +5. Create new Pull Request
2  Rakefile
... ... @@ -0,0 +1,2 @@
  1 +#!/usr/bin/env rake
  2 +require "bundler/gem_tasks"
23 command_model.gemspec
... ... @@ -0,0 +1,23 @@
  1 +# -*- encoding: utf-8 -*-
  2 +require File.expand_path('../lib/command_model/version', __FILE__)
  3 +
  4 +Gem::Specification.new do |gem|
  5 + gem.authors = ["Jack Christensen"]
  6 + gem.email = ["jack@jackchristensen.com"]
  7 + gem.description = %q{CommandModel integrates Rails validations with command objects}
  8 + gem.summary = %q{CommandModel integrates Rails validations with command objects. This allows errors from command execution to easily be handled with the familiar Rails validation system.}
  9 + gem.homepage = "https://github.com/JackC/command_model"
  10 +
  11 + gem.files = `git ls-files`.split($\)
  12 + gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
  13 + gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
  14 + gem.name = "command_model"
  15 + gem.require_paths = ["lib"]
  16 + gem.version = CommandModel::VERSION
  17 +
  18 + gem.add_dependency 'activemodel', "~> 3.2"
  19 +
  20 + gem.add_development_dependency 'rspec', "~> 2.9.0"
  21 + gem.add_development_dependency 'guard', "~> 1.0.0"
  22 + gem.add_development_dependency 'guard-rspec', "~> 0.7.0"
  23 +end
7 lib/command_model.rb
... ... @@ -0,0 +1,7 @@
  1 +require "active_model"
  2 +
  3 +require "command_model/version"
  4 +require "command_model/model"
  5 +
  6 +module CommandModel
  7 +end
39 lib/command_model/model.rb
... ... @@ -0,0 +1,39 @@
  1 +module CommandModel
  2 + class Model
  3 + include ActiveModel::Validations
  4 +
  5 + def self.execute(attributes_or_command)
  6 + command = if attributes_or_command.kind_of? self
  7 + attributes_or_command
  8 + else
  9 + new(attributes_or_command)
  10 + end
  11 +
  12 + yield command if command.valid?
  13 + command.execution_attempted!
  14 + command
  15 + end
  16 +
  17 + def initialize(attributes={})
  18 + attributes.each do |k,v|
  19 + send "#{k}=", v
  20 + end
  21 + end
  22 +
  23 + # Record that an attempt was made to execute this command whether or not
  24 + # it was successful.
  25 + def execution_attempted!
  26 + @execution_attempted = true
  27 + end
  28 +
  29 + # True if execution has been attempted on this command
  30 + def execution_attempted?
  31 + @execution_attempted
  32 + end
  33 +
  34 + # Command has been executed without errors
  35 + def success?
  36 + execution_attempted? && errors.empty?
  37 + end
  38 + end
  39 +end
3  lib/command_model/version.rb
... ... @@ -0,0 +1,3 @@
  1 +module CommandModel
  2 + VERSION = "0.0.1"
  3 +end
85 spec/model_spec.rb
... ... @@ -0,0 +1,85 @@
  1 +require 'spec_helper'
  2 +
  3 +class ExampleCommand < CommandModel::Model
  4 + attr_accessor :name
  5 +
  6 + validates_presence_of :name
  7 +end
  8 +
  9 +describe CommandModel::Model do
  10 + let(:example_command) { ExampleCommand.new :name => "John" }
  11 + let(:invalid_example_command) { ExampleCommand.new }
  12 +
  13 + describe "self.execute" do
  14 + it "accepts object of same kind and returns it" do
  15 + ExampleCommand.execute(example_command) {}.should eq(example_command)
  16 + end
  17 +
  18 + it "accepts attributes, creates object, and returns it" do
  19 + c = ExampleCommand.execute(:name => "John") {}
  20 + c.should be_kind_of(ExampleCommand)
  21 + c.name.should eq("John")
  22 + end
  23 +
  24 + it "calls passed block when there are no validation errors on Model" do
  25 + block_ran = false
  26 + ExampleCommand.execute(example_command) { block_ran = true }
  27 + block_ran.should eq(true)
  28 + end
  29 +
  30 + it "does not call passed block when there are validation errors on Model" do
  31 + block_ran = false
  32 + ExampleCommand.execute(invalid_example_command) { block_ran = true }
  33 + block_ran.should eq(false)
  34 + end
  35 +
  36 + it "records execution attempt when there not no validation errors on Model" do
  37 + ExampleCommand.execute(example_command) {}
  38 + example_command.execution_attempted?.should eq(true)
  39 + end
  40 +
  41 + it "records execution attempt when there are validation errors on Model" do
  42 + ExampleCommand.execute(invalid_example_command) {}
  43 + invalid_example_command.execution_attempted?.should eq(true)
  44 + end
  45 +
  46 + it "is not successful if block adds error to Model" do
  47 + ExampleCommand.execute(example_command) do |command|
  48 + command.errors.add :base, "foo"
  49 + end
  50 +
  51 + example_command.should_not be_success
  52 + end
  53 + end
  54 +
  55 + describe "initialize" do
  56 + it "assigns attributes" do
  57 + m = ExampleCommand.new :name => "John"
  58 + m.name.should eq("John")
  59 + end
  60 + end
  61 +
  62 + describe "execution_attempted!" do
  63 + it "sets execution_attempted? to true" do
  64 + example_command.execution_attempted!
  65 + example_command.execution_attempted?.should eq(true)
  66 + end
  67 + end
  68 +
  69 + describe "success?" do
  70 + it "is false before execution" do
  71 + example_command.should_not be_success
  72 + end
  73 +
  74 + it "is false after execution with errors" do
  75 + example_command.execution_attempted!
  76 + example_command.errors.add :base, "foo"
  77 + example_command.success?.should eq(false)
  78 + end
  79 +
  80 + it "is true after execution without errors" do
  81 + example_command.execution_attempted!
  82 + example_command.success?.should eq(true)
  83 + end
  84 + end
  85 +end
2  spec/spec_helper.rb
... ... @@ -0,0 +1,2 @@
  1 +require 'command_model'
  2 +require 'rspec'

0 comments on commit ad0e9c3

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