dynamic_form_objects 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dynamic_form_objects.gemspec +34 -0
- data/lib/dynamic_form_objects.rb +74 -0
- data/lib/dynamic_form_objects/version.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e72592a1d8346dfc1a13ca164a1afa5dffb94987
|
4
|
+
data.tar.gz: 4059cd5301b624d31a11b2475e788d027188d410
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d95f6ef9be95bf7425a9482ca55dcbaed0755357393d8718f051bb5102a234eaa629d74ead8cafa72a85e98fbd4e96de5c63f6a345ad94f9631c2c2ed792c58
|
7
|
+
data.tar.gz: 0824968379eb6cfcace6adf0dc5b608ad3d1b6c1331bc436f8be547711650ed935866e019af2c6177f8da1f326b088eb3844dfa19f2be4ec51b040ca0ec4c5b5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# DynamicFormObjects
|
2
|
+
|
3
|
+
Create Form Objects "on the "fly". Useful for defining simple forms.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'dynamic_form_objects'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install dynamic_form_objects
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
Create a form object:
|
23
|
+
```ruby
|
24
|
+
UserForm = DynamicFormObjects.define_form('UserForm', :name, :age)
|
25
|
+
|
26
|
+
# Or add some validations:
|
27
|
+
UserForm = DynamicFormObjects.define_form('UserForm', :name, :age).with_validations do
|
28
|
+
validates_presence_of :name
|
29
|
+
# Other validations...
|
30
|
+
end
|
31
|
+
|
32
|
+
def new
|
33
|
+
@user = UserForm.new
|
34
|
+
end
|
35
|
+
|
36
|
+
def create
|
37
|
+
@user = UserForm.new(params[:user])
|
38
|
+
if @user.valid?
|
39
|
+
...
|
40
|
+
else
|
41
|
+
...
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dynamic_form_objects.
|
49
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dynamic_form_objects"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dynamic_form_objects/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dynamic_form_objects"
|
8
|
+
spec.version = DynamicFormObjects::VERSION
|
9
|
+
spec.authors = ["Laertis Pappas"]
|
10
|
+
spec.email = ["laertis.pappas@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Utility helper to define form objects on the fly}
|
13
|
+
spec.description = %q{Define a form class that can be used with ativesupport.}
|
14
|
+
spec.homepage = "https://www.github.com/laertispappas/dynamic_form_objects"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_dependency 'activemodel'
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
34
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require "dynamic_form_objects/version"
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
module DynamicFormObjects
|
5
|
+
# Defines a new form object:
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# UserForm = DynamicFormObjects.define_form("UserForm", :first_name, :last_name, :age, :password).with_validations do
|
10
|
+
# validates_presence_of :first_name
|
11
|
+
# validates_presence_of :last_name
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# -- or
|
15
|
+
# UserForm = DynamicFormObjects.define_form("UserForm").
|
16
|
+
# with_validations { validates_presence_of :name}.
|
17
|
+
# with_validations { validates_presence_of :age }
|
18
|
+
#
|
19
|
+
# -- Create a new instance
|
20
|
+
# user_form = UserForm.new
|
21
|
+
# user_form = UserForm.new first_name: 'Foo', last_name: 'Bar'
|
22
|
+
#
|
23
|
+
# -- Check if valid
|
24
|
+
# if user_form.valid?
|
25
|
+
# ...
|
26
|
+
|
27
|
+
def self.define_form(form_name, *args)
|
28
|
+
Class.new(Object) do |klass|
|
29
|
+
include ActiveModel::Model
|
30
|
+
|
31
|
+
attr_reader(*args)
|
32
|
+
|
33
|
+
@__attrs = args
|
34
|
+
@__form_name = form_name
|
35
|
+
|
36
|
+
def self.attrs
|
37
|
+
@__attrs
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.with_validations(&block)
|
41
|
+
class_eval(&block)
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.model_name
|
46
|
+
ActiveModel::Name.new(self, nil, @__form_name)
|
47
|
+
end
|
48
|
+
|
49
|
+
def initialize(opts = {})
|
50
|
+
set_instance_variables(opts)
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_h
|
54
|
+
@__hash_value
|
55
|
+
end
|
56
|
+
alias_method :to_hash, :to_h
|
57
|
+
|
58
|
+
def persisted?
|
59
|
+
false
|
60
|
+
end
|
61
|
+
|
62
|
+
def set_instance_variables(opts = {})
|
63
|
+
defined_attrs = self.class.attrs
|
64
|
+
attrs_and_values = defined_attrs.map{|k| [k, opts[k]]}
|
65
|
+
attrs_and_values.each do |k, v|
|
66
|
+
instance_variable_set("@#{k.to_s}", v)
|
67
|
+
end
|
68
|
+
|
69
|
+
instance_variable_set("@__hash_value", Hash[attrs_and_values])
|
70
|
+
end
|
71
|
+
private :set_instance_variables
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynamic_form_objects
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Laertis Pappas
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-24 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Define a form class that can be used with ativesupport.
|
70
|
+
email:
|
71
|
+
- laertis.pappas@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- dynamic_form_objects.gemspec
|
85
|
+
- lib/dynamic_form_objects.rb
|
86
|
+
- lib/dynamic_form_objects/version.rb
|
87
|
+
homepage: https://www.github.com/laertispappas/dynamic_form_objects
|
88
|
+
licenses: []
|
89
|
+
metadata:
|
90
|
+
allowed_push_host: https://rubygems.org
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.4.5.1
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Utility helper to define form objects on the fly
|
111
|
+
test_files: []
|
112
|
+
has_rdoc:
|