informal 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in informal.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 by Josh Susser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Informal
2
+
3
+ Informal is a small gem that enhances a Plain Old Ruby Object so it can be used
4
+ with Rails 3 form helpers in place of an ActiveRecord model. It works with the
5
+ Rails `form_for` helper, and `simple_form` as well.
6
+
7
+ Here's a quick (and slightly insane) example:
8
+
9
+ # models/command.rb
10
+ require "informal"
11
+ class Command
12
+ include Informal::Model
13
+ attr_accessor :command, :args
14
+ validates_presence_of :command
15
+ def run; `#{command} #{args}`; end
16
+ end
17
+
18
+ # views/commands/new.html.erb
19
+ <%= form_for @command do |form| %>
20
+ <%= form.text_field :command %>
21
+ <%= form.text_field :args %>
22
+ <%= form.submit "Do It!" %>
23
+ <% end %>
24
+
25
+ # controllers/commands_controller.rb
26
+ def create
27
+ command = Command.new(params[:command])
28
+ if command.valid?
29
+ command.run
30
+ end
31
+ end
32
+
33
+ ## Installation
34
+
35
+ It's a Ruby gem, so just install it with `gem install informal`, add it to your
36
+ bundler Gemfile, or do whatever you like to do with gems. There is nothing to
37
+ configure.
38
+
39
+ ## Usage
40
+
41
+ The insanity of the above example aside, Informal is pretty useful for creating
42
+ simple RESTful resources that don't map directly to ActiveRecord models. It
43
+ evolved from handling login credentials to creating model objects that were
44
+ stored in a serialized attribute of a parent resource.
45
+
46
+ In many ways using an informal model is just like using an AR model in
47
+ controllers and views. The biggest difference is that you don't `save` an
48
+ informal object, but you can add validations and check if it's `valid?`. If
49
+ there are any validation errors, the object will have all the usual error
50
+ decorations so that error messages will display properly in the form view.
51
+
52
+ ### Initialization, #super and attributes
53
+
54
+ If you include `Informal::Model`, your class automatically gets an
55
+ `#initialize` method that takes a params hash and calls setters for all
56
+ attributes in the hash. If your model class inherits from a class that has its
57
+ own `#initialize` method that needs to get the super call, you should instead
58
+ include `Informal::ModelNoInit`, which does not create an `#initialize` method.
59
+ Make your own `#initialize` method, and in that you can assign the attributes
60
+ using the `#attributes=` method and also call super with whatever args are
61
+ needed.
62
+
63
+ ## License
64
+
65
+ Released under the MIT License. See the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new("test_all") do |t|
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = false
8
+ end
9
+
10
+ desc "Run all tests"
11
+ task :default => :test_all
data/informal.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "informal/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "informal"
7
+ s.version = Informal::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Josh Susser"]
10
+ s.email = ["josh@hasmanythrough.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Easily use any Plain Old Ruby Object as the model for Rails form helpers.}
13
+ s.description = %q{Easily use any Plain Old Ruby Object as the model for Rails form helpers.}
14
+
15
+ s.rubyforge_project = "informal"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('activemodel', "~> 3.0")
23
+ end
data/lib/informal.rb ADDED
@@ -0,0 +1 @@
1
+ require "informal/model"
@@ -0,0 +1,15 @@
1
+ require "informal/model_no_init"
2
+
3
+ module Informal
4
+ module Model
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ include ModelNoInit
8
+ end
9
+ end
10
+
11
+ def initialize(attrs={})
12
+ self.attributes = attrs
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ require "active_model"
2
+ module Informal
3
+ module ModelNoInit
4
+ def self.included(klass)
5
+ klass.class_eval do
6
+ extend ActiveModel::Naming
7
+ include ActiveModel::Validations
8
+ end
9
+ end
10
+
11
+ def attributes=(attrs)
12
+ attrs.each_pair { |name, value| self.send("#{name}=", value) }
13
+ end
14
+
15
+ def new_record?
16
+ true
17
+ end
18
+
19
+ def to_key
20
+ [object_id]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Informal
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
2
+
3
+ class ModelNoInitTest < Test::Unit::TestCase
4
+ class KalEl
5
+ attr_accessor :k
6
+ def initialize(uber)
7
+ @k = uber
8
+ end
9
+ end
10
+ class Poro < KalEl
11
+ include Informal::ModelNoInit
12
+ attr_accessor :x, :y, :z
13
+ validates_presence_of :x, :y, :z
14
+ def initialize(attrs={})
15
+ super(true)
16
+ self.attributes = attrs
17
+ end
18
+ end
19
+ def poro_class; Poro; end
20
+
21
+ include ModelTestCases
22
+
23
+ def test_super
24
+ assert @model.k
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
2
+
3
+ class ModelTest < Test::Unit::TestCase
4
+ class Poro
5
+ include Informal::Model
6
+ attr_accessor :x, :y, :z
7
+ validates_presence_of :x, :y, :z
8
+ end
9
+ def poro_class; Poro; end
10
+
11
+ include ModelTestCases
12
+ end
@@ -0,0 +1,29 @@
1
+ module ModelTestCases
2
+ def setup
3
+ @model = self.poro_class.new(:x => 1, :y => 2)
4
+ end
5
+
6
+ def test_new
7
+ assert_equal 1, @model.x
8
+ assert_equal 2, @model.y
9
+ assert_nil @model.z
10
+ end
11
+
12
+ def test_new_record
13
+ assert @model.new_record?
14
+ end
15
+
16
+ def test_to_key
17
+ assert_equal [@model.object_id], @model.to_key
18
+ end
19
+
20
+ def test_naming
21
+ assert_equal "Poro", @model.class.model_name.human
22
+ end
23
+
24
+ def test_validations
25
+ assert @model.invalid?
26
+ assert_equal [], @model.errors[:x]
27
+ assert @model.errors[:z].any? {|err| err =~ /blank/}
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ require "test/unit"
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+
6
+ require "informal/model"
7
+ require "informal/model_no_init"
8
+
9
+ require File.expand_path(File.join(File.dirname(__FILE__), "model_test_cases"))
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: informal
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Josh Susser
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-24 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activemodel
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ version: "3.0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: Easily use any Plain Old Ruby Object as the model for Rails form helpers.
37
+ email:
38
+ - josh@hasmanythrough.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - informal.gemspec
52
+ - lib/informal.rb
53
+ - lib/informal/model.rb
54
+ - lib/informal/model_no_init.rb
55
+ - lib/informal/version.rb
56
+ - test/model_no_init_test.rb
57
+ - test/model_test.rb
58
+ - test/model_test_cases.rb
59
+ - test/test_helper.rb
60
+ has_rdoc: true
61
+ homepage: ""
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project: informal
90
+ rubygems_version: 1.6.2
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Easily use any Plain Old Ruby Object as the model for Rails form helpers.
94
+ test_files:
95
+ - test/model_no_init_test.rb
96
+ - test/model_test.rb
97
+ - test/model_test_cases.rb
98
+ - test/test_helper.rb