simple_form_object 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/simple_form_object/version.rb +3 -0
- data/lib/simple_form_object.rb +69 -0
- data/simple_form_objects.gemspec +26 -0
- data/spec/model_spec.rb +111 -0
- data/spec/spec_helper.rb +1 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c37b0286cc72f6c39614929b2fd7e2dd3016e6d0
|
4
|
+
data.tar.gz: 60680fc3ecbbd4e32b4b58353c214f2458b5ab7d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2080be5c6f844ea3af7c85e2aac7fae1d5ad0e6a9c1ae36209dcf14a38a5eccfd7d3111ec97ee985b12202fb611d12a331a97a0d4181df79e87805e5ae516561
|
7
|
+
data.tar.gz: a9932b96ff317b5c44d771be3d793c68d970395dc9b04e2dffa5c65674dea7bc55a7cccb3afe295da3901208341597b72036d74cf8dda64b96b7b7abdad091c2
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Leonard Garvey
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# SimpleFormObjects
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_form_objects'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_form_objects
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/simple_form_objects/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "simple_form_object/version"
|
2
|
+
require "active_model"
|
3
|
+
require "active_support"
|
4
|
+
|
5
|
+
module SimpleFormObject
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
include ActiveModel::Model
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def attribute(name, type = :string, options = {})
|
12
|
+
self.send(:attr_accessor, name)
|
13
|
+
|
14
|
+
_attributes << Attribute.new(name, type, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def _attributes
|
18
|
+
@_attributes ||= []
|
19
|
+
end
|
20
|
+
|
21
|
+
def _attribute(attribute_name)
|
22
|
+
_attributes.select{|a| a.name == attribute_name}.first
|
23
|
+
end
|
24
|
+
|
25
|
+
def model_name
|
26
|
+
ActiveModel::Name.new(self, nil, self.to_s.gsub(/Form^/, ''))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def column_for_attribute(attribute)
|
31
|
+
self.class._attribute(attribute).fake_column
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(attributes={})
|
35
|
+
super
|
36
|
+
self.class._attributes.each do |attribute|
|
37
|
+
attribute.apply_default_to(self)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Attribute
|
42
|
+
def initialize(name, type, options)
|
43
|
+
@name = name
|
44
|
+
@type = type
|
45
|
+
@options = options
|
46
|
+
|
47
|
+
extract_options
|
48
|
+
end
|
49
|
+
|
50
|
+
attr_accessor :name, :type, :options
|
51
|
+
|
52
|
+
def fake_column
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def apply_default_to(form)
|
57
|
+
if form.send(@name).nil?
|
58
|
+
form.send("#{@name}=", @default) if @default
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def extract_options
|
65
|
+
@default = options.fetch(:default, nil)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_form_object/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_form_object"
|
8
|
+
spec.version = SimpleFormObject::VERSION
|
9
|
+
spec.authors = ["Leonard Garvey"]
|
10
|
+
spec.email = ["lengarvey@gmail.com"]
|
11
|
+
spec.summary = %q{Simple form objects for simple form and rails}
|
12
|
+
spec.description = %q{Very simple form objects for simple form and rails}
|
13
|
+
spec.homepage = "http://github.com/lengarvey/simple_form_objects"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", "~> 4.0"
|
22
|
+
spec.add_dependency "activesupport", "~> 4.0"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
data/spec/model_spec.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'including SimpleFormObject' do
|
4
|
+
context 'when included' do
|
5
|
+
let(:instance) do
|
6
|
+
class Klass
|
7
|
+
include SimpleFormObject
|
8
|
+
end
|
9
|
+
|
10
|
+
Klass.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should have ActiveModel::Model' do
|
14
|
+
expect(instance).to respond_to :valid?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.attribute' do
|
19
|
+
let(:attribute) { :foo }
|
20
|
+
let(:instance) do
|
21
|
+
class Klass
|
22
|
+
include SimpleFormObject
|
23
|
+
|
24
|
+
attribute :foo
|
25
|
+
end
|
26
|
+
|
27
|
+
Klass.new
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should respond to "attribute"' do
|
31
|
+
expect(instance).to respond_to(attribute)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should respond to "attribute="' do
|
35
|
+
expect(instance).to respond_to("#{attribute}=")
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'type' do
|
39
|
+
let(:types) { %i(boolean string email url tel password search text file hidden integer float decimal range datetime date time select radio_buttons check_boxes country time_zone) }
|
40
|
+
|
41
|
+
let(:type) { :boolean }
|
42
|
+
let(:attr) { :foo }
|
43
|
+
let(:instance) do
|
44
|
+
class AnotherForm
|
45
|
+
include SimpleFormObject
|
46
|
+
|
47
|
+
attribute :foo, :boolean
|
48
|
+
end
|
49
|
+
|
50
|
+
AnotherForm.new
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should return a fake column with the correct type' do
|
54
|
+
expect(instance.column_for_attribute(attr).type).to eq type
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'options' do
|
59
|
+
describe 'default' do
|
60
|
+
let(:instance) do
|
61
|
+
class AnotherForm
|
62
|
+
include SimpleFormObject
|
63
|
+
|
64
|
+
attribute :foo, :boolean, default: true
|
65
|
+
end
|
66
|
+
|
67
|
+
AnotherForm.new
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should set the attribute to the default' do
|
71
|
+
expect(instance.foo).to eq true
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'when a value is supplied in the initialization hash' do
|
75
|
+
let(:instance) do
|
76
|
+
class AnotherForm
|
77
|
+
include SimpleFormObject
|
78
|
+
|
79
|
+
attribute :foo, :boolean, default: true
|
80
|
+
end
|
81
|
+
|
82
|
+
AnotherForm.new(foo: false)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should use the value in the hash' do
|
86
|
+
expect(instance.foo).to eq false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '.model_name' do
|
95
|
+
let(:klass) do
|
96
|
+
class KlassForm
|
97
|
+
include SimpleFormObject
|
98
|
+
end
|
99
|
+
|
100
|
+
Klass
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should return an ActiveModel::Name' do
|
104
|
+
expect(klass.model_name).to be_a_kind_of ActiveModel::Name
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should remove Form from the name of the class' do
|
108
|
+
expect(klass.model_name.name).to eq "Klass"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'simple_form_object'
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_form_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leonard Garvey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Very simple form objects for simple form and rails
|
84
|
+
email:
|
85
|
+
- lengarvey@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/simple_form_object.rb
|
96
|
+
- lib/simple_form_object/version.rb
|
97
|
+
- simple_form_objects.gemspec
|
98
|
+
- spec/model_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
homepage: http://github.com/lengarvey/simple_form_objects
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.2.0
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Simple form objects for simple form and rails
|
124
|
+
test_files:
|
125
|
+
- spec/model_spec.rb
|
126
|
+
- spec/spec_helper.rb
|