command_model 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +9 -0
  5. data/LICENSE +22 -0
  6. data/README.md +99 -0
  7. data/Rakefile +2 -0
  8. data/command_model.gemspec +23 -0
  9. data/examples/bank/.gitignore +15 -0
  10. data/examples/bank/Gemfile +38 -0
  11. data/examples/bank/README.rdoc +261 -0
  12. data/examples/bank/Rakefile +7 -0
  13. data/examples/bank/app/assets/images/rails.png +0 -0
  14. data/examples/bank/app/assets/javascripts/accounts.js.coffee +3 -0
  15. data/examples/bank/app/assets/javascripts/application.js +15 -0
  16. data/examples/bank/app/assets/stylesheets/accounts.css.scss +3 -0
  17. data/examples/bank/app/assets/stylesheets/application.css +13 -0
  18. data/examples/bank/app/controllers/accounts_controller.rb +62 -0
  19. data/examples/bank/app/controllers/application_controller.rb +3 -0
  20. data/examples/bank/app/helpers/accounts_helper.rb +2 -0
  21. data/examples/bank/app/helpers/application_helper.rb +2 -0
  22. data/examples/bank/app/mailers/.gitkeep +0 -0
  23. data/examples/bank/app/models/.gitkeep +0 -0
  24. data/examples/bank/app/models/account.rb +65 -0
  25. data/examples/bank/app/views/accounts/deposit_form.html.erb +23 -0
  26. data/examples/bank/app/views/accounts/index.html.erb +22 -0
  27. data/examples/bank/app/views/accounts/transfer_form.html.erb +32 -0
  28. data/examples/bank/app/views/accounts/withdraw_form.html.erb +23 -0
  29. data/examples/bank/app/views/layouts/application.html.erb +15 -0
  30. data/examples/bank/config.ru +4 -0
  31. data/examples/bank/config/application.rb +65 -0
  32. data/examples/bank/config/boot.rb +6 -0
  33. data/examples/bank/config/environment.rb +5 -0
  34. data/examples/bank/config/environments/development.rb +31 -0
  35. data/examples/bank/config/environments/production.rb +64 -0
  36. data/examples/bank/config/environments/test.rb +35 -0
  37. data/examples/bank/config/initializers/accounts.rb +6 -0
  38. data/examples/bank/config/initializers/backtrace_silencers.rb +7 -0
  39. data/examples/bank/config/initializers/inflections.rb +15 -0
  40. data/examples/bank/config/initializers/mime_types.rb +5 -0
  41. data/examples/bank/config/initializers/secret_token.rb +7 -0
  42. data/examples/bank/config/initializers/session_store.rb +8 -0
  43. data/examples/bank/config/initializers/wrap_parameters.rb +10 -0
  44. data/examples/bank/config/locales/en.yml +5 -0
  45. data/examples/bank/config/routes.rb +12 -0
  46. data/examples/bank/db/seeds.rb +7 -0
  47. data/examples/bank/lib/assets/.gitkeep +0 -0
  48. data/examples/bank/lib/tasks/.gitkeep +0 -0
  49. data/examples/bank/log/.gitkeep +0 -0
  50. data/examples/bank/public/404.html +26 -0
  51. data/examples/bank/public/422.html +26 -0
  52. data/examples/bank/public/500.html +25 -0
  53. data/examples/bank/public/favicon.ico +0 -0
  54. data/examples/bank/public/robots.txt +5 -0
  55. data/examples/bank/script/rails +6 -0
  56. data/examples/bank/test/fixtures/.gitkeep +0 -0
  57. data/examples/bank/test/functional/.gitkeep +0 -0
  58. data/examples/bank/test/functional/accounts_controller_test.rb +19 -0
  59. data/examples/bank/test/integration/.gitkeep +0 -0
  60. data/examples/bank/test/performance/browsing_test.rb +12 -0
  61. data/examples/bank/test/test_helper.rb +7 -0
  62. data/examples/bank/test/unit/.gitkeep +0 -0
  63. data/examples/bank/test/unit/helpers/accounts_helper_test.rb +4 -0
  64. data/examples/bank/vendor/assets/javascripts/.gitkeep +0 -0
  65. data/examples/bank/vendor/assets/stylesheets/.gitkeep +0 -0
  66. data/examples/bank/vendor/plugins/.gitkeep +0 -0
  67. data/lib/command_model.rb +7 -0
  68. data/lib/command_model/model.rb +173 -0
  69. data/lib/command_model/version.rb +3 -0
  70. data/spec/model_spec.rb +222 -0
  71. data/spec/spec_helper.rb +2 -0
  72. metadata +164 -0
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Bank::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ # Place all the behaviors and hooks related to the matching controller here.
2
+ # All this logic will automatically be available in application.js.
3
+ # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,3 @@
1
+ // Place all the styles related to the accounts controller here.
2
+ // They will automatically be included in application.css.
3
+ // You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,62 @@
1
+ class AccountsController < ApplicationController
2
+ def index
3
+ @accounts = ACCOUNTS
4
+ end
5
+
6
+ def deposit_form
7
+ @account = find_account_by_name params[:id]
8
+ @deposit = Account::DepositCommand.new
9
+ end
10
+
11
+ def deposit
12
+ @account = find_account_by_name params[:id]
13
+ @deposit = @account.deposit params[:deposit]
14
+
15
+ if @deposit.success?
16
+ redirect_to root_path, :notice => "Deposited #{@deposit.amount} to #{@account.name}'s account."
17
+ else
18
+ render "deposit_form"
19
+ end
20
+ end
21
+
22
+ def withdraw_form
23
+ @account = find_account_by_name params[:id]
24
+ @withdraw = Account::WithdrawCommand.new
25
+ end
26
+
27
+ def withdraw
28
+ @account = find_account_by_name params[:id]
29
+ @withdraw = @account.withdraw params[:withdraw]
30
+
31
+ if @withdraw.success?
32
+ redirect_to root_path, :notice => "Withdrew #{@withdraw.amount} from #{@account.name}'s account."
33
+ else
34
+ render "withdraw_form"
35
+ end
36
+ end
37
+
38
+ def transfer_form
39
+ @accounts = ACCOUNTS
40
+ @transfer = Account::TransferCommand.new
41
+ end
42
+
43
+ def transfer
44
+ @accounts = ACCOUNTS
45
+ @transfer = Account::TransferCommand.new
46
+ @transfer.to = find_account_by_name params[:transfer][:to]
47
+ @transfer.from = find_account_by_name params[:transfer][:from]
48
+ @transfer.amount = params[:transfer][:amount]
49
+ Account.transfer @transfer
50
+
51
+ if @transfer.success?
52
+ redirect_to root_path, :notice => "Transferred #{@transfer.amount} from #{@transfer.from.name}'s account to #{@transfer.to.name}'s account."
53
+ else
54
+ render "transfer_form"
55
+ end
56
+ end
57
+
58
+ private
59
+ def find_account_by_name(name)
60
+ ACCOUNTS.find { |a| a.name == name }
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module AccountsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
@@ -0,0 +1,65 @@
1
+ class Account
2
+ class WithdrawCommand < CommandModel::Model
3
+ parameter :amount,
4
+ :typecast => :integer,
5
+ :presence => true,
6
+ :numericality => { :greater_than => 0, :less_than_or_equal_to => 500 }
7
+ end
8
+
9
+ class DepositCommand < CommandModel::Model
10
+ parameter :amount,
11
+ :typecast => :integer,
12
+ :presence => true,
13
+ :numericality => { :greater_than => 0 }
14
+ end
15
+
16
+ class TransferCommand < CommandModel::Model
17
+ parameter :from, :to, :presence => true
18
+ parameter :amount,
19
+ :typecast => :integer,
20
+ :presence => true,
21
+ :numericality => { :greater_than => 0 }
22
+
23
+ validate do |model|
24
+ errors.add :base, "From and to accounts cannot be the same" if model.from == model.to
25
+ end
26
+ end
27
+
28
+ attr_reader :name, :balance
29
+
30
+ def initialize(name, balance)
31
+ @name = name
32
+ @balance = balance
33
+ end
34
+
35
+ def withdraw(options)
36
+ WithdrawCommand.execute(options) do |command|
37
+ if balance >= command.amount
38
+ @balance -= command.amount
39
+ else
40
+ command.errors.add :amount, "is more than account balance"
41
+ end
42
+ end
43
+ end
44
+
45
+ def deposit(options)
46
+ DepositCommand.execute(options) do |command|
47
+ @balance += command.amount
48
+ end
49
+ end
50
+
51
+ def self.transfer(options)
52
+ TransferCommand.execute(options) do |command|
53
+ if command.from.balance >= command.amount
54
+ command.from.withdraw :amount => command.amount
55
+ command.to.deposit :amount => command.amount
56
+ else
57
+ command.errors.add :amount, "is more than account balance"
58
+ end
59
+ end
60
+ end
61
+
62
+ def to_param
63
+ name
64
+ end
65
+ end
@@ -0,0 +1,23 @@
1
+ <p><%= link_to "Back to all accounts", root_path %></p>
2
+
3
+ <h1>Deposit in <%= @account.name %>'s Account</h1>
4
+
5
+ <p>Balance: <%= @account.balance %></p>
6
+
7
+ <% if @deposit.errors.present? %>
8
+ <ul>
9
+ <% @deposit.errors.full_messages.each do |msg| %>
10
+ <li><%= msg %></li>
11
+ <% end %>
12
+ </ul>
13
+ <% end %>
14
+
15
+ <%= form_for @deposit, :as => :deposit, :url => account_deposit_path(@account) do |f| %>
16
+ <%= f.label :amount %>
17
+ <%= f.text_field :amount %>
18
+ <%= f.submit "Deposit" %>
19
+ <% end %>
20
+
21
+
22
+
23
+
@@ -0,0 +1,22 @@
1
+ <h1>Accounts</h1>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <th>Name</th>
7
+ <th>Balance</th>
8
+ <th></th>
9
+ <th></th>
10
+ </tr>
11
+ </thead>
12
+ <% @accounts.each do |a| %>
13
+ <tr>
14
+ <td><%= a.name %></td>
15
+ <td><%= a.balance %></td>
16
+ <td><%= link_to "Deposit", account_deposit_form_path(a) %></td>
17
+ <td><%= link_to "Withdraw", account_withdraw_form_path(a) %></td>
18
+ </tr>
19
+ <% end %>
20
+ </table>
21
+
22
+ <%= link_to "Transfer", account_transfer_path %>
@@ -0,0 +1,32 @@
1
+ <p><%= link_to "Back to all accounts", root_path %></p>
2
+
3
+ <h1>Transfer Between Accounts</h1>
4
+
5
+
6
+ <% if @transfer.errors.present? %>
7
+ <ul>
8
+ <% @transfer.errors.full_messages.each do |msg| %>
9
+ <li><%= msg %></li>
10
+ <% end %>
11
+ </ul>
12
+ <% end %>
13
+
14
+ <%= form_for @transfer, :as => :transfer, :url => account_transfer_path do |f| %>
15
+ <div>
16
+ <%= f.label :from %><br />
17
+ <%= f.select :from, @accounts.map { |a| ["#{a.name} - #{a.balance}", a.name] }, :selected => f.object.from.try(:name), :include_blank => true %>
18
+ </div>
19
+ <div>
20
+ <%= f.label :to %><br />
21
+ <%= f.select :to, @accounts.map { |a| ["#{a.name} - #{a.balance}", a.name] }, :selected => f.object.to.try(:name), :include_blank => true %>
22
+ </div>
23
+ <div>
24
+ <%= f.label :amount %><br />
25
+ <%= f.text_field :amount %>
26
+ </div>
27
+ <%= f.submit "Transfer" %>
28
+ <% end %>
29
+
30
+
31
+
32
+
@@ -0,0 +1,23 @@
1
+ <p><%= link_to "Back to all accounts", root_path %></p>
2
+
3
+ <h1>Withdraw from <%= @account.name %>'s Account</h1>
4
+
5
+ <p>Balance: <%= @account.balance %></p>
6
+
7
+ <% if @withdraw.errors.present? %>
8
+ <ul>
9
+ <% @withdraw.errors.full_messages.each do |msg| %>
10
+ <li><%= msg %></li>
11
+ <% end %>
12
+ </ul>
13
+ <% end %>
14
+
15
+ <%= form_for @withdraw, :as => :withdraw, :url => account_withdraw_path(@account) do |f| %>
16
+ <%= f.label :amount %>
17
+ <%= f.text_field :amount %>
18
+ <%= f.submit "Deposit" %>
19
+ <% end %>
20
+
21
+
22
+
23
+
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Bank</title>
5
+ <%= stylesheet_link_tag "application", :media => "all" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <div style="color: green"><%= flash[:notice] %></div>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Bank::Application
@@ -0,0 +1,65 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ # require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_mailer/railtie"
7
+ require "active_resource/railtie"
8
+ require "sprockets/railtie"
9
+ require "rails/test_unit/railtie"
10
+
11
+ if defined?(Bundler)
12
+ # If you precompile assets before deploying to production, use this line
13
+ Bundler.require(*Rails.groups(:assets => %w(development test)))
14
+ # If you want your assets lazily compiled in production, use this line
15
+ # Bundler.require(:default, :assets, Rails.env)
16
+ end
17
+
18
+ module Bank
19
+ class Application < Rails::Application
20
+ # Settings in config/environments/* take precedence over those specified here.
21
+ # Application configuration should go into files in config/initializers
22
+ # -- all .rb files in that directory are automatically loaded.
23
+
24
+ # Custom directories with classes and modules you want to be autoloadable.
25
+ # config.autoload_paths += %W(#{config.root}/extras)
26
+
27
+ # Only load the plugins named here, in the order given (default is alphabetical).
28
+ # :all can be used as a placeholder for all plugins not explicitly named.
29
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
30
+
31
+ # Activate observers that should always be running.
32
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
33
+
34
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
35
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
36
+ # config.time_zone = 'Central Time (US & Canada)'
37
+
38
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
39
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
40
+ # config.i18n.default_locale = :de
41
+
42
+ # Configure the default encoding used in templates for Ruby 1.9.
43
+ config.encoding = "utf-8"
44
+
45
+ # Configure sensitive parameters which will be filtered from the log file.
46
+ config.filter_parameters += [:password]
47
+
48
+ # Use SQL instead of Active Record's schema dumper when creating the database.
49
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
50
+ # like if you have constraints or database-specific column types
51
+ # config.active_record.schema_format = :sql
52
+
53
+ # Enforce whitelist mode for mass assignment.
54
+ # This will create an empty whitelist of attributes available for mass-assignment for all models
55
+ # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
56
+ # parameters by using an attr_accessible or attr_protected declaration.
57
+ # config.active_record.whitelist_attributes = true
58
+
59
+ # Enable the asset pipeline
60
+ config.assets.enabled = true
61
+
62
+ # Version of your assets, change this if you want to expire all your assets
63
+ config.assets.version = '1.0'
64
+ end
65
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
5
+
6
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Bank::Application.initialize!
@@ -0,0 +1,31 @@
1
+ Bank::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger
20
+ config.active_support.deprecation = :log
21
+
22
+ # Only use best-standards-support built into browsers
23
+ config.action_dispatch.best_standards_support = :builtin
24
+
25
+
26
+ # Do not compress assets
27
+ config.assets.compress = false
28
+
29
+ # Expands the lines which load the assets
30
+ config.assets.debug = true
31
+ end
@@ -0,0 +1,64 @@
1
+ Bank::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.consider_all_requests_local = false
9
+ config.action_controller.perform_caching = true
10
+
11
+ # Disable Rails's static asset server (Apache or nginx will already do this)
12
+ config.serve_static_assets = false
13
+
14
+ # Compress JavaScripts and CSS
15
+ config.assets.compress = true
16
+
17
+ # Don't fallback to assets pipeline if a precompiled asset is missed
18
+ config.assets.compile = false
19
+
20
+ # Generate digests for assets URLs
21
+ config.assets.digest = true
22
+
23
+ # Defaults to Rails.root.join("public/assets")
24
+ # config.assets.manifest = YOUR_PATH
25
+
26
+ # Specifies the header that your server uses for sending files
27
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
28
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
29
+
30
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31
+ # config.force_ssl = true
32
+
33
+ # See everything in the log (default is :info)
34
+ # config.log_level = :debug
35
+
36
+ # Prepend all log lines with the following tags
37
+ # config.log_tags = [ :subdomain, :uuid ]
38
+
39
+ # Use a different logger for distributed setups
40
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
41
+
42
+ # Use a different cache store in production
43
+ # config.cache_store = :mem_cache_store
44
+
45
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
46
+ # config.action_controller.asset_host = "http://assets.example.com"
47
+
48
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
49
+ # config.assets.precompile += %w( search.js )
50
+
51
+ # Disable delivery errors, bad email addresses will be ignored
52
+ # config.action_mailer.raise_delivery_errors = false
53
+
54
+ # Enable threaded mode
55
+ # config.threadsafe!
56
+
57
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
58
+ # the I18n.default_locale when a translation can not be found)
59
+ config.i18n.fallbacks = true
60
+
61
+ # Send deprecation notices to registered listeners
62
+ config.active_support.deprecation = :notify
63
+
64
+ end