├── test
├── dummy
│ ├── log
│ │ └── .gitkeep
│ ├── app
│ │ ├── mailers
│ │ │ └── .gitkeep
│ │ ├── models
│ │ │ └── .gitkeep
│ │ ├── helpers
│ │ │ └── application_helper.rb
│ │ ├── controllers
│ │ │ └── application_controller.rb
│ │ ├── views
│ │ │ └── layouts
│ │ │ │ └── application.html.erb
│ │ └── assets
│ │ │ ├── stylesheets
│ │ │ └── application.css
│ │ │ └── javascripts
│ │ │ └── application.js
│ ├── lib
│ │ └── assets
│ │ │ └── .gitkeep
│ ├── public
│ │ ├── favicon.ico
│ │ ├── 422.html
│ │ ├── 404.html
│ │ └── 500.html
│ ├── config.ru
│ ├── config
│ │ ├── environment.rb
│ │ ├── database.yml
│ │ ├── locales
│ │ │ └── en.yml
│ │ ├── initializers
│ │ │ ├── mime_types.rb
│ │ │ ├── inflections.rb
│ │ │ ├── backtrace_silencers.rb
│ │ │ ├── session_store.rb
│ │ │ ├── wrap_parameters.rb
│ │ │ └── secret_token.rb
│ │ ├── boot.rb
│ │ ├── environments
│ │ │ ├── development.rb
│ │ │ ├── test.rb
│ │ │ └── production.rb
│ │ ├── application.rb
│ │ └── routes.rb
│ ├── Rakefile
│ └── script
│ │ └── rails
├── fixtures
│ ├── sample_model.rb
│ └── sample_resource.rb
├── test_helper.rb
├── compliance_test.rb
├── modest_model_test.rb
└── sample_resource_test.rb
├── .travis.yml
├── Gemfile
├── .gitignore
├── lib
├── modest_model
│ ├── combined_attr.rb
│ ├── validators.rb
│ ├── resource.rb
│ ├── callbacks.rb
│ ├── base.rb
│ └── tenacity.rb
└── modest_model.rb
├── modest_model.gemspec
├── Rakefile
├── MIT-LICENSE
└── README.md
/test/dummy/log/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/app/mailers/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/app/models/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/lib/assets/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/test/dummy/public/favicon.ico:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | rvm:
2 | - 1.8.7
3 | - 1.9.2
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source "http://rubygems.org"
2 |
3 | gemspec
--------------------------------------------------------------------------------
/test/dummy/app/helpers/application_helper.rb:
--------------------------------------------------------------------------------
1 | module ApplicationHelper
2 | end
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | Gemfile.lock
2 | .bundle/
3 | log/*.log
4 | pkg/
5 | test/dummy/db/*.sqlite3
6 | test/dummy/log/*.log
7 | test/dummy/tmp/
--------------------------------------------------------------------------------
/test/dummy/app/controllers/application_controller.rb:
--------------------------------------------------------------------------------
1 | class ApplicationController < ActionController::Base
2 | protect_from_forgery
3 | end
4 |
--------------------------------------------------------------------------------
/test/fixtures/sample_model.rb:
--------------------------------------------------------------------------------
1 | class SampleModel < ModestModel::Base
2 | attributes :name, :email
3 | attribute :nickname, :absence => true
4 | end
--------------------------------------------------------------------------------
/test/dummy/config.ru:
--------------------------------------------------------------------------------
1 | # This file is used by Rack-based servers to start the application.
2 |
3 | require ::File.expand_path('../config/environment', __FILE__)
4 | run Dummy::Application
5 |
--------------------------------------------------------------------------------
/test/dummy/config/environment.rb:
--------------------------------------------------------------------------------
1 | # Load the rails application
2 | require File.expand_path('../application', __FILE__)
3 |
4 | # Initialize the rails application
5 | Dummy::Application.initialize!
6 |
--------------------------------------------------------------------------------
/test/dummy/config/database.yml:
--------------------------------------------------------------------------------
1 | defaults: &defaults
2 | adapter: sqlite3
3 | database: ":memory:"
4 |
5 | development:
6 | <<: *defaults
7 | production:
8 | <<: *defaults
9 | test:
10 | <<: *defaults
--------------------------------------------------------------------------------
/test/dummy/config/locales/en.yml:
--------------------------------------------------------------------------------
1 | # Sample localization file for English. Add more files in this directory for other locales.
2 | # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 |
4 | en:
5 | hello: "Hello world"
6 |
--------------------------------------------------------------------------------
/test/dummy/config/initializers/mime_types.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new mime types for use in respond_to blocks:
4 | # Mime::Type.register "text/richtext", :rtf
5 | # Mime::Type.register_alias "text/html", :iphone
6 |
--------------------------------------------------------------------------------
/lib/modest_model/combined_attr.rb:
--------------------------------------------------------------------------------
1 | module ModestModel
2 | module CombinedAttr
3 | def attribute *attrs
4 | validations = attrs.extract_options!
5 | attributes *attrs
6 | validates *(attrs +[validations]) if validations.any?
7 | end
8 | end
9 | end
--------------------------------------------------------------------------------
/test/dummy/config/boot.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3 |
4 | if File.exist?(gemfile)
5 | ENV['BUNDLE_GEMFILE'] = gemfile
6 | require 'bundler'
7 | Bundler.setup
8 | end
9 |
10 | $:.unshift File.expand_path('../../../../lib', __FILE__)
--------------------------------------------------------------------------------
/lib/modest_model/validators.rb:
--------------------------------------------------------------------------------
1 | module ModestModel
2 | module Validators
3 | class AbsenceValidator < ActiveModel::EachValidator
4 | def validate_each(record, attribute, value)
5 | record.errors.add(attribute, :invalid, options) unless value.blank?
6 | end
7 | end
8 | end
9 | end
--------------------------------------------------------------------------------
/lib/modest_model/resource.rb:
--------------------------------------------------------------------------------
1 | module ModestModel
2 | autoload :Tenacity, File.expand_path('../tenacity', __FILE__)
3 | autoload :Callbacks, File.expand_path('../callbacks', __FILE__)
4 |
5 | class Resource < Base
6 | include ModestModel::Tenacity
7 | include ModestModel::Callbacks
8 | end
9 | end
--------------------------------------------------------------------------------
/test/dummy/Rakefile:
--------------------------------------------------------------------------------
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 | Dummy::Application.load_tasks
8 |
--------------------------------------------------------------------------------
/test/dummy/app/views/layouts/application.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Dummy
5 | <%= stylesheet_link_tag "application" %>
6 | <%= javascript_include_tag "application" %>
7 | <%= csrf_meta_tags %>
8 |
9 |
10 |
11 | <%= yield %>
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/test/dummy/script/rails:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3 |
4 | APP_PATH = File.expand_path('../../config/application', __FILE__)
5 | require File.expand_path('../../config/boot', __FILE__)
6 | require 'rails/commands'
7 |
--------------------------------------------------------------------------------
/test/test_helper.rb:
--------------------------------------------------------------------------------
1 | # Configure Rails Environment
2 | ENV["RAILS_ENV"] = "test"
3 |
4 | require File.expand_path("../dummy/config/environment.rb", __FILE__)
5 | require "rails/test_help"
6 |
7 | Rails.backtrace_cleaner.remove_silencers!
8 |
9 | # Load support files
10 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11 |
--------------------------------------------------------------------------------
/test/dummy/app/assets/stylesheets/application.css:
--------------------------------------------------------------------------------
1 | /*
2 | * This is a manifest file that'll automatically include all the stylesheets available in this directory
3 | * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4 | * the top of the compiled file, but it's generally better to create a new file per style scope.
5 | *= require_self
6 | *= require_tree .
7 | */
--------------------------------------------------------------------------------
/test/dummy/config/initializers/inflections.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Add new inflection rules using the following format
4 | # (all these examples are active by default):
5 | # ActiveSupport::Inflector.inflections do |inflect|
6 | # inflect.plural /^(ox)$/i, '\1en'
7 | # inflect.singular /^(ox)en/i, '\1'
8 | # inflect.irregular 'person', 'people'
9 | # inflect.uncountable %w( fish sheep )
10 | # end
11 |
--------------------------------------------------------------------------------
/test/dummy/config/initializers/backtrace_silencers.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5 |
6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7 | # Rails.backtrace_cleaner.remove_silencers!
8 |
--------------------------------------------------------------------------------
/test/dummy/config/initializers/session_store.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4 |
5 | # Use the database for sessions instead of the cookie-based default,
6 | # which shouldn't be used to store highly confidential information
7 | # (create the session table with "rails generate session_migration")
8 | # Dummy::Application.config.session_store :active_record_store
9 |
--------------------------------------------------------------------------------
/lib/modest_model.rb:
--------------------------------------------------------------------------------
1 | begin
2 | require 'active_model'
3 | rescue LoadError => e
4 | retry if require('rubygems')
5 | end
6 |
7 | module ModestModel
8 | autoload :Base, File.expand_path('../modest_model/base', __FILE__)
9 | autoload :Validators, File.expand_path('../modest_model/validators', __FILE__)
10 | autoload :CombinedAttr, File.expand_path('../modest_model/combined_attr', __FILE__)
11 | autoload :Resource, File.expand_path('../modest_model/resource', __FILE__)
12 | end
--------------------------------------------------------------------------------
/test/dummy/config/initializers/wrap_parameters.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 | #
3 | # This file contains settings for ActionController::ParamsWrapper which
4 | # is enabled by default.
5 |
6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7 | ActionController::Base.wrap_parameters :format => [:json]
8 |
9 | # Disable root element in JSON by default.
10 | if defined?(ActiveRecord)
11 | ActiveRecord::Base.include_root_in_json = false
12 | end
13 |
--------------------------------------------------------------------------------
/test/dummy/app/assets/javascripts/application.js:
--------------------------------------------------------------------------------
1 | // This is a manifest file that'll be compiled into including all the files listed below.
2 | // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3 | // be included in the compiled file accessible from http://example.com/assets/application.js
4 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5 | // the compiled file.
6 | //
7 | //= require jquery
8 | //= require jquery_ujs
9 | //= require_tree .
10 |
--------------------------------------------------------------------------------
/test/dummy/config/initializers/secret_token.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Your secret key for verifying the integrity of signed cookies.
4 | # If you change this key, all old signed cookies will become invalid!
5 | # Make sure the secret is at least 30 characters and all random,
6 | # no regular words or you'll be exposed to dictionary attacks.
7 | Dummy::Application.config.secret_token = '515514ee6d1b2d0d850379bf295584a36f86da2c9901afe0f2a82b8d7fd3240f209dcd37b12ac2376a0e3e03ba56b7f9d2cb7a1548463d8c844dd39a7535b98d'
8 |
--------------------------------------------------------------------------------
/modest_model.gemspec:
--------------------------------------------------------------------------------
1 | Gem::Specification.new do |s|
2 | s.name = "modest_model"
3 | s.authors = ["Mike Fulcher"]
4 | s.summary = "Simple, tableless ActiveModel-compliant models"
5 | s.description = "Simple, tableless ActiveModel-compliant models. Like ActiveRecord models without the database."
6 | s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
7 | s.test_files = Dir["test/**/*"]
8 | s.version = "0.1.1"
9 | s.homepage = 'https://github.com/6twenty/modest_model'
10 |
11 | s.add_dependency "activemodel", "~> 3"
12 | s.add_development_dependency "rails", "~> 3.1.rc5"
13 | s.add_development_dependency "sqlite3"
14 | end
--------------------------------------------------------------------------------
/test/compliance_test.rb:
--------------------------------------------------------------------------------
1 | require 'test_helper'
2 | require 'fixtures/sample_model'
3 |
4 | class ComplianceTest < ActiveSupport::TestCase
5 | include ActiveModel::Lint::Tests
6 |
7 | test "model_name.human uses I18n" do
8 | begin
9 | I18n.backend.store_translations :en,
10 | :activemodel => { :models => { :sample_model => "My Sample Model" } }
11 |
12 | assert_equal "My Sample Model", model.class.model_name.human
13 | ensure
14 | I18n.reload!
15 | end
16 | end
17 |
18 | test "model_name exposes singular and human name" do
19 | assert_equal "sample_model", model.class.model_name.singular
20 | assert_equal "Sample model", model.class.model_name.human
21 | end
22 |
23 | def setup
24 | @model = SampleModel.new
25 | end
26 | end
--------------------------------------------------------------------------------
/test/dummy/public/422.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The change you wanted was rejected (422)
5 |
17 |
18 |
19 |
20 |
21 |
22 |
The change you wanted was rejected.
23 |
Maybe you tried to change something you didn't have access to.
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/test/dummy/public/404.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | The page you were looking for doesn't exist (404)
5 |
17 |
18 |
19 |
20 |
21 |
22 |
The page you were looking for doesn't exist.
23 |
You may have mistyped the address or the page may have moved.
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/test/dummy/public/500.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | We're sorry, but something went wrong (500)
5 |
17 |
18 |
19 |
20 |
21 |
22 |
We're sorry, but something went wrong.
23 |
We've been notified about this issue and we'll take a look at it shortly.
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env rake
2 | begin
3 | require 'bundler/setup'
4 | require 'bundler/gem_tasks'
5 | rescue LoadError
6 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7 | end
8 | begin
9 | require 'rdoc/task'
10 | rescue LoadError
11 | require 'rdoc/rdoc'
12 | require 'rake/rdoctask'
13 | RDoc::Task = Rake::RDocTask
14 | end
15 |
16 | RDoc::Task.new(:rdoc) do |rdoc|
17 | rdoc.rdoc_dir = 'rdoc'
18 | rdoc.title = 'ModestModel'
19 | rdoc.options << '--line-numbers'
20 | rdoc.rdoc_files.include('README.md')
21 | rdoc.rdoc_files.include('lib/**/*.rb')
22 | end
23 |
24 | Bundler::GemHelper.install_tasks
25 |
26 | require 'rake/testtask'
27 |
28 | Rake::TestTask.new(:test) do |t|
29 | t.libs << 'lib'
30 | t.libs << 'test'
31 | t.pattern = 'test/**/*_test.rb'
32 | t.verbose = false
33 | end
34 |
35 | task :default => :test
36 |
--------------------------------------------------------------------------------
/test/fixtures/sample_resource.rb:
--------------------------------------------------------------------------------
1 | class SampleResource < ModestModel::Resource
2 | attributes :id, :name, :email
3 | attributes :saved_at, :destroyed_at
4 | attributes :find_callback, :create_callback, :save_callback, :update_callback, :destroy_callback
5 |
6 | # Attributes with validations
7 | attribute :nickname, :absence => true
8 | attribute :number, :numericality => {:allow_blank => true}
9 |
10 | after_find :set_find_callback
11 | def set_find_callback
12 | self.find_callback = true
13 | end
14 |
15 | after_create do
16 | self.create_callback = true
17 | end
18 |
19 | after_update do
20 | self.update_callback = true
21 | end
22 |
23 |
24 | after_save do
25 | self.save_callback = true
26 | end
27 |
28 | after_destroy do
29 | self.destroy_callback = true
30 | end
31 |
32 | find do
33 | # Some call
34 | self.attributes = {:name => "User", :email => "user@example.com"}
35 | end
36 |
37 | save do
38 | self.saved_at = Time.now
39 | end
40 |
41 | destroy do
42 | self.destroyed_at = Time.now
43 | end
44 |
45 | end
--------------------------------------------------------------------------------
/MIT-LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2011 Mike Fulcher
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/test/dummy/config/environments/development.rb:
--------------------------------------------------------------------------------
1 | Dummy::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 | # Do not compress assets
26 | config.assets.compress = false
27 | end
28 |
--------------------------------------------------------------------------------
/lib/modest_model/callbacks.rb:
--------------------------------------------------------------------------------
1 | module ModestModel
2 | module Callbacks
3 | extend ActiveSupport::Concern
4 |
5 | CALLBACKS = [
6 | :after_initialize, :after_find, :before_validation, :after_validation,
7 | :before_save, :around_save, :after_save, :before_create, :around_create,
8 | :after_create, :before_update, :around_update, :after_update,
9 | :before_destroy, :around_destroy, :after_destroy
10 | ]
11 |
12 | included do
13 | extend ActiveModel::Callbacks
14 | include ActiveModel::Validations::Callbacks
15 |
16 | define_model_callbacks :initialize, :find, :only => :after
17 | define_model_callbacks :save, :create, :update, :destroy
18 | end
19 |
20 | def found #:nodoc:
21 | run_callbacks(:find) { super }
22 | end
23 |
24 | def destroy #:nodoc:
25 | run_callbacks(:destroy) { super }
26 | end
27 |
28 | private
29 |
30 | def create_or_update #:nodoc:
31 | run_callbacks(:save) { super }
32 | end
33 |
34 | def create #:nodoc:
35 | run_callbacks(:create) { super }
36 | end
37 |
38 | def update(*) #:nodoc:
39 | run_callbacks(:update) { super }
40 | end
41 | end
42 | end
--------------------------------------------------------------------------------
/test/modest_model_test.rb:
--------------------------------------------------------------------------------
1 | require 'test_helper'
2 | require 'fixtures/sample_model'
3 |
4 | class ModestModelTest < ActiveSupport::TestCase
5 | test "validates absence of nickname" do
6 | sample = SampleModel.new(:nickname => "Spam")
7 | assert !sample.valid?
8 | assert_equal ["is invalid"], sample.errors[:nickname]
9 | end
10 |
11 | test "can retrieve all attributes values" do
12 | sample = SampleModel.new
13 | sample.name = "John Doe"
14 | sample.email = "john.doe@example.com"
15 | assert_equal "John Doe", sample.attributes["name"]
16 | assert_equal "john.doe@example.com", sample.attributes["email"]
17 | end
18 |
19 | test 'sample mail can ask if an attribute is present or not' do
20 | sample = SampleModel.new
21 | assert !sample.name?
22 |
23 | sample.name = "User"
24 | assert sample.name?
25 |
26 | sample.email = ""
27 | assert !sample.email?
28 | end
29 |
30 | test 'sample mail can clear attributes using clear_ prefix' do
31 | sample = SampleModel.new
32 | sample.name = "User"
33 | sample.email = "user@example.com"
34 | assert_equal "User", sample.name
35 | assert_equal "user@example.com", sample.email
36 | sample.clear_name
37 | sample.clear_email
38 | assert_nil sample.name
39 | assert_nil sample.email
40 | end
41 |
42 | test 'sample mail has name and email as attributes' do
43 | sample = SampleModel.new
44 | sample.name = "User"
45 | assert_equal "User", sample.name
46 | sample.email = "user@example.com"
47 | assert_equal "user@example.com", sample.email
48 | end
49 | end
50 |
--------------------------------------------------------------------------------
/lib/modest_model/base.rb:
--------------------------------------------------------------------------------
1 | module ModestModel
2 | class Base
3 | include ActiveModel::Conversion
4 | extend ActiveModel::Naming
5 | extend ActiveModel::Translation
6 | include ActiveModel::Serialization
7 | include ActiveModel::Validations
8 | include ActiveModel::AttributeMethods
9 |
10 | include ModestModel::Validators
11 | extend ModestModel::CombinedAttr
12 |
13 | def initialize(attributes = {}, options={})
14 | self.assign_attributes(attributes, options)
15 | end
16 |
17 | class_attribute :_attributes
18 | self._attributes = []
19 |
20 | attribute_method_prefix 'clear_'
21 | attribute_method_suffix '?'
22 |
23 | def self.attributes(*names)
24 | attr_accessor *names
25 | define_attribute_methods names
26 |
27 | self._attributes += names
28 | end
29 |
30 | def attributes
31 | self._attributes.inject({}) do |hash, attr|
32 | hash[attr.to_s] = send(attr)
33 | hash
34 | end
35 | end
36 |
37 | def attributes= attributes, options = {}
38 | assign_attributes attributes, options = {}
39 | end
40 |
41 | def [] attr
42 | attributes[attr.to_s]
43 | end
44 |
45 | def []= attr, val
46 | self.send "#{attr}=", val
47 | end
48 |
49 | def persisted?
50 | false
51 | end
52 |
53 | protected
54 |
55 | def assign_attributes attributes, options = {}
56 | attributes.each do |attr, value|
57 | self[attr] = value
58 | end unless attributes.blank?
59 | end
60 |
61 | def clear_attribute(attribute)
62 | send("#{attribute}=", nil)
63 | end
64 |
65 | def attribute?(attribute)
66 | send(attribute).present?
67 | end
68 | end
69 | end
--------------------------------------------------------------------------------
/test/dummy/config/environments/test.rb:
--------------------------------------------------------------------------------
1 | Dummy::Application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb
3 |
4 | # The test environment is used exclusively to run your application's
5 | # test suite. You never need to work with it otherwise. Remember that
6 | # your test database is "scratch space" for the test suite and is wiped
7 | # and recreated between test runs. Don't rely on the data there!
8 | config.cache_classes = true
9 |
10 | # Configure static asset server for tests with Cache-Control for performance
11 | config.serve_static_assets = true
12 | config.static_cache_control = "public, max-age=3600"
13 |
14 | # Log error messages when you accidentally call methods on nil
15 | config.whiny_nils = true
16 |
17 | # Show full error reports and disable caching
18 | config.consider_all_requests_local = true
19 | config.action_controller.perform_caching = false
20 |
21 | # Raise exceptions instead of rendering exception templates
22 | config.action_dispatch.show_exceptions = false
23 |
24 | # Disable request forgery protection in test environment
25 | config.action_controller.allow_forgery_protection = false
26 |
27 | # Tell Action Mailer not to deliver emails to the real world.
28 | # The :test delivery method accumulates sent emails in the
29 | # ActionMailer::Base.deliveries array.
30 | config.action_mailer.delivery_method = :test
31 |
32 | # Use SQL instead of Active Record's schema dumper when creating the test database.
33 | # This is necessary if your schema can't be completely dumped by the schema dumper,
34 | # like if you have constraints or database-specific column types
35 | # config.active_record.schema_format = :sql
36 |
37 | # Print deprecation notices to the stderr
38 | config.active_support.deprecation = :stderr
39 | end
40 |
--------------------------------------------------------------------------------
/test/dummy/config/application.rb:
--------------------------------------------------------------------------------
1 | require File.expand_path('../boot', __FILE__)
2 |
3 | require 'rails/all'
4 |
5 | Bundler.require
6 | require "modest_model"
7 |
8 | module Dummy
9 | class Application < Rails::Application
10 | # Settings in config/environments/* take precedence over those specified here.
11 | # Application configuration should go into files in config/initializers
12 | # -- all .rb files in that directory are automatically loaded.
13 |
14 | # Custom directories with classes and modules you want to be autoloadable.
15 | # config.autoload_paths += %W(#{config.root}/extras)
16 |
17 | # Only load the plugins named here, in the order given (default is alphabetical).
18 | # :all can be used as a placeholder for all plugins not explicitly named.
19 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
20 |
21 | # Activate observers that should always be running.
22 | # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
23 |
24 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26 | # config.time_zone = 'Central Time (US & Canada)'
27 |
28 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30 | # config.i18n.default_locale = :de
31 |
32 | # Configure the default encoding used in templates for Ruby 1.9.
33 | config.encoding = "utf-8"
34 |
35 | # Configure sensitive parameters which will be filtered from the log file.
36 | config.filter_parameters += [:password]
37 |
38 | # Enable the asset pipeline
39 | config.assets.enabled = true
40 | end
41 | end
42 |
43 |
--------------------------------------------------------------------------------
/test/dummy/config/routes.rb:
--------------------------------------------------------------------------------
1 | Dummy::Application.routes.draw do
2 | # The priority is based upon order of creation:
3 | # first created -> highest priority.
4 |
5 | # Sample of regular route:
6 | # match 'products/:id' => 'catalog#view'
7 | # Keep in mind you can assign values other than :controller and :action
8 |
9 | # Sample of named route:
10 | # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
11 | # This route can be invoked with purchase_url(:id => product.id)
12 |
13 | # Sample resource route (maps HTTP verbs to controller actions automatically):
14 | # resources :products
15 |
16 | # Sample resource route with options:
17 | # resources :products do
18 | # member do
19 | # get 'short'
20 | # post 'toggle'
21 | # end
22 | #
23 | # collection do
24 | # get 'sold'
25 | # end
26 | # end
27 |
28 | # Sample resource route with sub-resources:
29 | # resources :products do
30 | # resources :comments, :sales
31 | # resource :seller
32 | # end
33 |
34 | # Sample resource route with more complex sub-resources
35 | # resources :products do
36 | # resources :comments
37 | # resources :sales do
38 | # get 'recent', :on => :collection
39 | # end
40 | # end
41 |
42 | # Sample resource route within a namespace:
43 | # namespace :admin do
44 | # # Directs /admin/products/* to Admin::ProductsController
45 | # # (app/controllers/admin/products_controller.rb)
46 | # resources :products
47 | # end
48 |
49 | # You can have the root of your site routed with "root"
50 | # just remember to delete public/index.html.
51 | # root :to => 'welcome#index'
52 |
53 | # See how all your routes lay out with "rake routes"
54 |
55 | # This is a legacy wild controller route that's not recommended for RESTful applications.
56 | # Note: This route will make all actions in every controller accessible via GET requests.
57 | # match ':controller(/:action(/:id(.:format)))'
58 | end
59 |
--------------------------------------------------------------------------------
/test/dummy/config/environments/production.rb:
--------------------------------------------------------------------------------
1 | Dummy::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 | # Specifies the header that your server uses for sending files
18 | # (comment out if your front-end server doesn't support this)
19 | config.action_dispatch.x_sendfile_header = "X-Sendfile" # Use 'X-Accel-Redirect' for nginx
20 |
21 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
22 | # config.force_ssl = true
23 |
24 | # See everything in the log (default is :info)
25 | # config.log_level = :debug
26 |
27 | # Use a different logger for distributed setups
28 | # config.logger = SyslogLogger.new
29 |
30 | # Use a different cache store in production
31 | # config.cache_store = :mem_cache_store
32 |
33 | # Enable serving of images, stylesheets, and JavaScripts from an asset server
34 | # config.action_controller.asset_host = "http://assets.example.com"
35 |
36 | # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
37 | # config.assets.precompile += %w( search.js )
38 |
39 | # Disable delivery errors, bad email addresses will be ignored
40 | # config.action_mailer.raise_delivery_errors = false
41 |
42 | # Enable threaded mode
43 | # config.threadsafe!
44 |
45 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
46 | # the I18n.default_locale when a translation can not be found)
47 | config.i18n.fallbacks = true
48 |
49 | # Send deprecation notices to registered listeners
50 | config.active_support.deprecation = :notify
51 | end
52 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ModestModel [](https://secure.travis-ci.org/6twenty/modest_model.png])
2 |
3 | ## Overview
4 |
5 | Inspired by [Crafting Rails Applications](http://pragprog.com/book/jvrails/crafting-rails-applications), ModestModel provides an ActiveModel-compliant class that allows you to quickly create simple, table-less models. The intended use is to back interactions with external APIs with Ruby-friendly models rather than raw structured data (such as hashes).
6 |
7 | ## Example
8 |
9 | ```ruby
10 | json = MyExternalApi.call('/some/path.json')
11 | attributes_hash = JSON.decode(json)
12 |
13 | # => {'name' => 'Michael', 'email' => 'michael@example.com'}
14 |
15 | class SampleModel < ModestModel::Base
16 | attributes :name, :email
17 | end
18 |
19 | SampleModel.new(attributes_hash)
20 |
21 | # => # "Spam")
10 | assert !sample.valid?
11 | assert_equal ["is invalid"], sample.errors[:nickname]
12 | end
13 |
14 | test "can retrieve all attributes values" do
15 | sample = SampleResource.new
16 | sample.name = "John Doe"
17 | sample.email = "john.doe@example.com"
18 | assert_equal "John Doe", sample.attributes["name"]
19 | assert_equal "john.doe@example.com", sample.attributes["email"]
20 | end
21 |
22 | test 'sample mail can ask if an attribute is present or not' do
23 | sample = SampleResource.new
24 | assert !sample.name?
25 |
26 | sample.name = "User"
27 | assert sample.name?
28 |
29 | sample.email = ""
30 | assert !sample.email?
31 | end
32 |
33 | test 'sample mail can clear attributes using clear_ prefix' do
34 | sample = SampleResource.new
35 | sample.name = "User"
36 | sample.email = "user@example.com"
37 | assert_equal "User", sample.name
38 | assert_equal "user@example.com", sample.email
39 | sample.clear_name
40 | sample.clear_email
41 | assert_nil sample.name
42 | assert_nil sample.email
43 | end
44 |
45 | test 'sample mail has name and email as attributes' do
46 | sample = SampleResource.new
47 | sample.name = "User"
48 | assert_equal "User", sample.name
49 | sample.email = "user@example.com"
50 | assert_equal "user@example.com", sample.email
51 | end
52 |
53 | # Testing the resource ability
54 |
55 | test "the create method returns a newly created object" do
56 | sample = SampleResource.create(:name => 'User', :email => 'user@example.com')
57 | assert_equal false, sample.new_record?
58 | assert_equal "User", sample.name
59 | assert_equal "user@example.com", sample.email
60 | end
61 |
62 | test "the find method returns a record with the id set" do
63 | sample = SampleResource.find(1)
64 | assert_equal 1, sample.id
65 | assert_equal "User", sample.name
66 | assert_equal "user@example.com", sample.email
67 | end
68 |
69 | test "the save method perfoms its action correctly" do
70 | sample = SampleResource.find(1)
71 | assert_equal nil, sample.saved_at
72 | sample.save
73 | assert_equal Time.now.to_s, sample.saved_at.to_s
74 | end
75 |
76 | test "the destroy method perfoms its action correctly" do
77 | sample = SampleResource.find(1)
78 | assert_equal nil, sample.destroyed_at
79 | assert_equal false, sample.destroyed?
80 | sample.destroy
81 | assert_equal true, sample.destroyed?
82 | assert_equal Time.now.to_s, sample.destroyed_at.to_s
83 | end
84 |
85 | test "setting the primary key sets a new attribute and removes the old one" do
86 | sample = SampleResource.find(1)
87 | assert_equal false, SampleResource.method_defined?(:new_id)
88 | assert_equal 1, sample.id
89 | SampleResource.set_primary_key :new_id
90 | sample = SampleResource.find(1)
91 | assert_equal false, SampleResource.method_defined?(:id)
92 | assert_equal 1, sample.new_id
93 | # Teardown
94 | SampleResource.set_primary_key :id
95 | end
96 |
97 | # Testing the validations on save
98 |
99 | test "the validations work on save not just valid?" do
100 | sample = SampleResource.new(:number => 'INVALID')
101 | assert_equal false, sample.save
102 | assert_equal ["is not a number"], sample.errors[:number]
103 |
104 | sample = SampleResource.create(:number => 'INVALID')
105 | assert_equal ["is not a number"], sample.errors[:number]
106 |
107 | sample = SampleResource.new
108 | assert_equal false, sample.update_attributes(:number => 'INVALID')
109 | assert_equal ["is not a number"], sample.errors[:number]
110 | end
111 |
112 | # Testing the callbacks
113 |
114 | test "the find callback works" do
115 | sample = SampleResource.new
116 | assert_equal nil, sample.find_callback
117 | sample = SampleResource.find(1)
118 | assert_equal true, sample.find_callback
119 | end
120 |
121 | test "the create callback works" do
122 | sample = SampleResource.new
123 | assert_equal nil, sample.create_callback
124 | sample = SampleResource.create
125 | assert_equal true, sample.create_callback
126 | end
127 |
128 | test "the save callback works" do
129 | sample = SampleResource.new
130 | assert_equal nil, sample.save_callback
131 | sample.save
132 | assert_equal true, sample.save_callback
133 | sample = SampleResource.create
134 | assert_equal true, sample.save_callback
135 | sample.update_attribute :save_callback, false
136 | assert_equal true, sample.save_callback
137 | end
138 |
139 | test "the update callback works" do
140 | sample = SampleResource.find(1)
141 | assert_equal nil, sample.update_callback
142 | sample.update_attribute :update_callback, false
143 | assert_equal true, sample.update_callback
144 | end
145 |
146 | test "the destroy callback works" do
147 | sample = SampleResource.find(1)
148 | assert_equal nil, sample.destroy_callback
149 | sample.destroy
150 | assert_equal true, sample.destroy_callback
151 | end
152 |
153 | end
154 |
--------------------------------------------------------------------------------
/lib/modest_model/tenacity.rb:
--------------------------------------------------------------------------------
1 | module ModestModel
2 |
3 | class ResourceNotFound < ::StandardError; end
4 | class InvalidResource < ::StandardError; end
5 |
6 | module Tenacity
7 | extend ActiveSupport::Concern
8 |
9 | included do
10 | class_attribute :_find
11 | self._find = Proc.new {}
12 |
13 | class_attribute :_save
14 | self._save = Proc.new {}
15 |
16 | class_attribute :_destroy
17 | self._destroy = Proc.new {}
18 |
19 | class_attribute :primary_key
20 | set_primary_key :id
21 | end
22 |
23 | module ClassMethods
24 |
25 | def undefine_attribute_method attr # :nodoc:
26 | self._attributes = self._attributes - [attr]
27 | undef_method attr if method_defined? attr
28 | undef_method "#{attr}=" if method_defined? "#{attr}="
29 | end
30 |
31 | # Sets the +primary_key+ and its associated attribute
32 | def set_primary_key attr
33 | undefine_attribute_method :id if attr == :id # Remove id, always
34 | if self.primary_key
35 | undefine_attribute_method self.primary_key
36 | # TODO: remove any validations on the current PK
37 | # self._validators.except!(self.primary_key.to_sym)
38 | end
39 | self.primary_key = attr
40 | attributes attr
41 | # TODO: add a presence validation on the new PK
42 | # validates attr, :presence => true
43 | end
44 |
45 | # When a block is passed, that block is set to +_find+.
46 | # When +id_+ is passed (or no +block+ is passed) a new record is instantiated with the passed +id_+ and the +_find+ block is called.
47 | # If the return from the +_find+ block is +nil+ or +false+, a ResourceNotFound error is raised.
48 | def find id_=nil, &block
49 | if block_given?
50 | self._find = block
51 | else
52 | resource = new(primary_key.to_sym => id_)
53 | resource.send(:call_find!) || raise(ModestModel::ResourceNotFound)
54 | resource.found
55 | end
56 | end
57 |
58 | # TODO - docs
59 | def create attributes = nil, options = {}, &block
60 | if attributes.is_a?(Array)
61 | attributes.collect { |attr| create(attr, options, &block) }
62 | else
63 | object = new(attributes, options)
64 | yield(object) if block_given?
65 | object.save
66 | object
67 | end
68 | end
69 |
70 | # The passed +block+ is set to +_save+ and called on save
71 | def save &block
72 | self._save = block if block_given?
73 | end
74 |
75 | # The passed +block+ is set to +_destroy+ and called on destroyed
76 | def destroy &block
77 | self._destroy = block if block_given?
78 | end
79 | end
80 |
81 | module InstanceMethods
82 |
83 | def initialize attributes = {}, options={} #:nodoc:
84 | super attributes, options
85 | @new_resource = true
86 | @destroyed = false
87 | end
88 |
89 | def found #:nodoc:
90 | @new_resource = false
91 | return self
92 | end
93 |
94 | def to_param #:nodoc:
95 | send(self.class.primary_key)
96 | end
97 |
98 | def new_resource? #:nodoc:
99 | @new_resource
100 | end
101 | alias :new_record? :new_resource?
102 |
103 | def destroyed? #:nodoc:
104 | @destroyed
105 | end
106 |
107 | def persisted? #:nodoc:
108 | false
109 | end
110 |
111 | # Runs the save block and returns true if the operation was successful, false if not
112 | def save(*)
113 | create_or_update
114 | end
115 |
116 | # Runs the save block and returns true if the operation was successful or raises an InvalidResource error if not
117 | def save!(*)
118 | create_or_update || raise(ModestModel::InvalidResource)
119 | end
120 |
121 | # Runs the destroy block and sets the resource as destroyed
122 | def destroy
123 | call_destroy!
124 | @destroyed = true
125 | return self
126 | end
127 |
128 | # Updates a single attribute and calls save.
129 | # This is especially useful for boolean flags on existing records. Also note that
130 | #
131 | # * Validation is skipped.
132 | # * Callbacks are invoked.
133 | #
134 | def update_attribute(name, value)
135 | send("#{name.to_s}=", value)
136 | save(:validate => false)
137 | end
138 |
139 | # Updates the attributes of the model from the passed-in hash and calls save the
140 | # will fail and false will be returned.
141 | def update_attributes(attributes, options = {})
142 | self.assign_attributes(attributes, options)
143 | save
144 | end
145 |
146 | # Updates its receiver just like +update_attributes+ but calls save! instead
147 | # of +save+, so an exception is raised if the resource is invalid.
148 | def update_attributes!(attributes, options = {})
149 | self.assign_attributes(attributes, options)
150 | save!
151 | end
152 |
153 | private
154 |
155 | def create_or_update #:nodoc:
156 | valid? ? (new_resource? ? create : update) : false
157 | end
158 |
159 | def create #:nodoc:
160 | @new_resource = false
161 | call_save!
162 | end
163 |
164 | def update #:nodoc:
165 | call_save!
166 | end
167 |
168 | def call_save! #:nodoc:
169 | instance_exec &self.class._save
170 | end
171 |
172 | def call_find! #:nodoc:
173 | instance_exec &self.class._find
174 | end
175 |
176 | def call_destroy! #:nodoc:
177 | instance_exec &self.class._destroy
178 | end
179 | end
180 | end
181 | end
--------------------------------------------------------------------------------