act_form 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50804b31cfd1b52553e16af190b4f8471cdd6e2f
4
+ data.tar.gz: 6392da038a5fdf1ef6f9e70376b5a0efdf791d30
5
+ SHA512:
6
+ metadata.gz: 7cbf1b8b3a10db0f6d9569953593267a1de6935ed7a40ea20e6a902aac71857559f0c5d7cd1f2dfc5bd7dc53166d342589de5006996d1bc605fe9fc10346e1dc
7
+ data.tar.gz: 251cef49ed01f2133b23cbac6c81b07c45807aee3853e1d1295901723d0603f7209fb78bece6923e755c88382bad6fb03a587db6a909a87bed289a0156da5900
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at alex@qingju.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in act_form.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 alex
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,41 @@
1
+ # ActForm
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/form_model`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'act_form'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install form_model
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/form_model. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/*_spec.rb"
6
+ end
7
+
8
+ task :default => :test
data/act_form.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'act_form/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'act_form'
8
+ spec.version = ActForm::VERSION
9
+ spec.authors = ['zires']
10
+ spec.email = ['zshuaibin@gmail.com']
11
+
12
+ spec.summary = %q{A simple way to create form/command/service objects.}
13
+ spec.description = %q{The simple way to create form objects or command/service objects with ActiveModel.}
14
+ spec.homepage = 'https://github.com/simple-and-powerful/form-model'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_runtime_dependency 'virtus', '~> 1.0', '>= 1.0.0'
31
+ spec.add_runtime_dependency 'activemodel', '~> 4.0', '>= 4.0.0'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.12'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ end
data/lib/act_form.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'act_form/version'
2
+ require 'act_form/model'
3
+ require 'act_form/runnable'
4
+
5
+ module ActForm
6
+ class Base
7
+ include Model
8
+ end
9
+
10
+ class RecordForm < Base
11
+ attr_reader :record
12
+ def initialize(record, **attrs)
13
+ @record = record
14
+ @extract_attrs = @record.attributes.extract! *self.class.attribute_set.map(&:name).map(&:to_s)
15
+ super(@extract_attrs.merge(attrs))
16
+ end
17
+
18
+ def save
19
+ if valid?
20
+ sync(@record)
21
+ @persisted = @record.save
22
+ else
23
+ false
24
+ end
25
+ end
26
+ end
27
+
28
+ class Command < Base
29
+ include Runnable
30
+ private_class_method :new
31
+ end
32
+ end
33
+
34
+ require 'act_form/railtie' if defined?(Rails)
@@ -0,0 +1,51 @@
1
+ require 'active_support/concern'
2
+ require 'act_form/utils'
3
+
4
+ module ActForm
5
+ module Combinable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :_forms, instance_writer: false
10
+ self._forms = []
11
+ end
12
+
13
+ class_methods do
14
+
15
+ def combine(*forms)
16
+ forms.each do |form_class|
17
+ raise ArgumentError, 'Can not combine itself' if form_class == self
18
+
19
+ next if self._forms.include?(form_class)
20
+
21
+ self._forms << form_class
22
+
23
+ self.attribute_set.merge(form_class.attribute_set)
24
+
25
+ Utils.merge(self._validators, form_class._validators)
26
+
27
+ method_name = form_class.model_name.singular
28
+ define_method(method_name) { form_class.new(attributes) }
29
+ form_class._validate_callbacks.each do |callback|
30
+ filter = callback.filter
31
+ if filter.is_a?(Symbol) || filter.is_a?(String)
32
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
33
+ def #{filter}
34
+ form = #{method_name}
35
+ form.send(:#{filter})
36
+ form.errors.messages.each do |attr, _errors|
37
+ _errors.each { |err| errors.add(attr, err) }
38
+ end
39
+ end
40
+ private :#{filter}
41
+ RUBY
42
+ end
43
+ end
44
+ self._validate_callbacks.append *form_class._validate_callbacks
45
+ end # forms.each
46
+ end # combine
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,47 @@
1
+ require 'active_support/core_ext/object/deep_dup'
2
+ require 'active_model'
3
+ require 'virtus'
4
+ require 'act_form/combinable'
5
+
6
+ module ActForm
7
+ module Model
8
+ extend ActiveSupport::Concern
9
+ include ActiveModel::Validations
10
+ include ActiveModel::Conversion
11
+
12
+ included do
13
+ extend ActiveModel::Naming
14
+ extend ActiveModel::Translation
15
+ include Virtus.model
16
+ end
17
+
18
+ def sync(target)
19
+ self.class.attribute_set.map(&:name).each do |attr|
20
+ target.public_send("#{attr}=", public_send(attr)) if target.respond_to?(attr)
21
+ end
22
+ end
23
+
24
+ def save(target)
25
+ if valid?
26
+ sync(target)
27
+ @persisted = target.save
28
+ else
29
+ false
30
+ end
31
+ end
32
+
33
+ def persisted?
34
+ !!@persisted
35
+ end
36
+
37
+ class_methods do
38
+ private
39
+ def inherited(child_class)
40
+ child_class._validators = self._validators.deep_dup
41
+ child_class._validate_callbacks = self._validate_callbacks.deep_dup
42
+ child_class.include Combinable
43
+ super
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ module ActForm
2
+ class Railtie < ::Rails::Railtie
3
+ generators do
4
+ require 'generators/form_model/install_generator'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActForm
4
+ module Runnable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attr_reader :result
9
+ end
10
+
11
+ class_methods do
12
+ def run(*args)
13
+ new(*args).run
14
+ end
15
+ end
16
+
17
+ def has_errors?
18
+ !errors.empty?
19
+ end
20
+
21
+ def run
22
+ if valid?
23
+ @result = perform
24
+ @performed = true
25
+ end
26
+ self
27
+ end
28
+
29
+ def perform; end
30
+
31
+ def success?
32
+ !has_errors? && !!@performed
33
+ end
34
+
35
+ def failure?
36
+ !success?
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ module ActForm
2
+ class Utils
3
+ class << self
4
+ def merge(a_validators, b_validators)
5
+ b_validators.each do |k, v|
6
+ a_validators[k] ? (a_validators[k] += v) : (a_validators[k] = v)
7
+ end
8
+ end # merge
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ActForm
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ module ActForm
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+
6
+ desc 'Create the forms dir and copy record_form.rb'
7
+ def copy_initializer
8
+ directory 'forms', 'app/forms'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,2 @@
1
+ class RecordForm < ActForm::RecordForm
2
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: act_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - zires
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: activemodel
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '4.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 4.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '4.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 4.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.12'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.12'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '10.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '10.0'
81
+ description: The simple way to create form objects or command/service objects with
82
+ ActiveModel.
83
+ email:
84
+ - zshuaibin@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - CODE_OF_CONDUCT.md
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - act_form.gemspec
96
+ - lib/act_form.rb
97
+ - lib/act_form/combinable.rb
98
+ - lib/act_form/model.rb
99
+ - lib/act_form/railtie.rb
100
+ - lib/act_form/runnable.rb
101
+ - lib/act_form/utils.rb
102
+ - lib/act_form/version.rb
103
+ - lib/generators/act_form/install_generator.rb
104
+ - lib/generators/templates/forms/record_form.rb
105
+ homepage: https://github.com/simple-and-powerful/form-model
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.5.1
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: A simple way to create form/command/service objects.
129
+ test_files: []