light_form 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1068ccb3160f2a1059f1aaac07e446ba7e92991d
4
+ data.tar.gz: 7cb06749857c28860f558e4bc6cf669aada3680d
5
+ SHA512:
6
+ metadata.gz: f7b5bf89967dc0b71054bda26b3ad240c7db7126921e9da16bdf99418fbd5d4efe37f0979b787d087cec04b97d05d7b26265805c841cda41a3c4c4a4285d6b63
7
+ data.tar.gz: b109b70705d9e2b79e5c97e4b78ecf0df65408825d1289b3ff7f7bbd360db7b739908fffa0a5db875df76028fcc14685369adc10f3f5d5c7f13418f953b9534e
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .rubocop_todo.yml
24
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require spec_helper
3
+ --format progress
4
+ --no-profile
data/.rubocop.yml ADDED
@@ -0,0 +1,48 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-02-23 06:17:38 +0100 using RuboCop version 0.27.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 3
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ # Offense count: 2
13
+ # Cop supports --auto-correct.
14
+ # Configuration parameters: PreferredDelimiters.
15
+ Style/PercentLiteralDelimiters:
16
+ Enabled: false
17
+
18
+ # Offense count: 2
19
+ Style/RegexpLiteral:
20
+ MaxSlashes: 0
21
+
22
+ # Offense count: 1
23
+ # Cop supports --auto-correct.
24
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
25
+ Style/TrailingBlankLines:
26
+ Enabled: false
27
+
28
+ # Offense count: 1
29
+ # Cop supports --auto-correct.
30
+ # Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, Whitelist.
31
+ Style/TrivialAccessors:
32
+ Enabled: false
33
+
34
+ # Offense count: 2
35
+ # Cop supports --auto-correct.
36
+ Style/UnneededPercentQ:
37
+ Enabled: false
38
+
39
+ Documentation:
40
+ Enabled: false
41
+
42
+ Metrics/LineLength:
43
+ Max: 120
44
+
45
+ AllCops:
46
+ Exclude:
47
+ - '**/Guardfile'
48
+ - '**/model_form.gemspec'
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ script: bundle exec rake
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in light_form.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard :rspec, cmd: 'rspec' do
2
+ watch(%r{^lib/(.+).rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
3
+ watch(%r{^spec/(.+).rb$}) { |m| "spec/#{m[1]}.rb" }
4
+ watch('spec/spec_helper.rb') { 'spec' }
5
+ watch('Gemfile')
6
+ end
7
+
8
+ guard :rubocop, all_on_start: false, cli: ['--format', 'clang'] do
9
+ watch(%r{.+\.rb$})
10
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Pawel Niemczyk
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,31 @@
1
+ # LightForm
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'light_form'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install light_form
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/light_form/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: 'spec'
@@ -0,0 +1,74 @@
1
+ require 'set'
2
+ require 'active_model'
3
+
4
+ module LightForm
5
+ class Form
6
+ include ActiveModel::Model
7
+
8
+ def initialize(params = {})
9
+ super(_prepare_params(params))
10
+ end
11
+
12
+ class << self
13
+ def config
14
+ @config ||= {}
15
+ end
16
+
17
+ def properties(*prop_names)
18
+ add_property = method(:_add_property)
19
+ prop_names.each(&add_property)
20
+ end
21
+
22
+ def property(prop_name, options = {}, &_block)
23
+ _add_property(prop_name)
24
+ _add_property_transform(prop_name, options)
25
+ _add_property_validation(prop_name, options[:validates]) if options[:validates]
26
+ end
27
+
28
+ private
29
+
30
+ def _properties_transform
31
+ config[:properties_transform] ||= {}
32
+ end
33
+
34
+ def _properties
35
+ config[:properties] ||= Set.new
36
+ end
37
+
38
+ def _add_property_transform(prop_name, options = {})
39
+ transformations = options.slice(:from, :transform_with, :default)
40
+ return if transformations.empty?
41
+ _properties_transform[prop_name] = transformations
42
+ end
43
+
44
+ def _add_property(prop_name)
45
+ send(:attr_accessor, prop_name) if _properties.add?(prop_name)
46
+ end
47
+
48
+ def _add_property_validation(prop_name, validation)
49
+ validates(prop_name, validation)
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def _prepare_params(value)
56
+ params = value.clone
57
+ _prepare_params_keys_and_values!(params)
58
+ properties = self.class.config[:properties] || []
59
+ params.extract!(*properties)
60
+ end
61
+
62
+ def _prepare_params_keys_and_values!(params)
63
+ properties_from = self.class.config[:properties_transform] || {}
64
+ properties_from.each do |key_to, hash|
65
+ params[key_to] = params.delete(hash[:from]) if hash[:from]
66
+ next params[key_to] = hash[:default] if params[key_to].empty? && hash[:default]
67
+ next unless hash[:transform_with]
68
+ transformation = hash[:transform_with]
69
+ trans_proc = transformation.is_a?(Symbol) ? method(transformation) : transformation
70
+ params[key_to] = trans_proc.call(params[key_to])
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module LightForm
2
+ VERSION = '0.0.1'
3
+ end
data/lib/light_form.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'light_form/version'
2
+ require 'light_form/form'
3
+ module LightForm
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'light_form/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'light_form'
8
+ spec.version = LightForm::VERSION
9
+ spec.authors = ['Pawel Niemczyk']
10
+ spec.email = ['pniemczyk.info@gmail.com']
11
+ spec.summary = %q{Light Form}
12
+ spec.description = %q{Light form}
13
+ spec.homepage = 'https://github.com/pniemczyk/light_form'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_runtime_dependency 'activemodel', '>= 3.0.2'
22
+ spec.add_development_dependency 'bundler', '>= 1.6'
23
+ spec.add_development_dependency 'rake', '~> 10.4'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_development_dependency 'guard', '~> 2.12'
26
+ spec.add_development_dependency 'guard-rspec', '~> 4.5'
27
+ spec.add_development_dependency 'guard-rubocop', '~> 1.2'
28
+ spec.add_development_dependency 'coveralls', '~> 0.7'
29
+ spec.add_development_dependency 'awesome_print', '~> 1.6'
30
+ end
@@ -0,0 +1,118 @@
1
+ describe LightForm::Form do
2
+ context '.properties' do
3
+ subject do
4
+ class_factory do
5
+ properties :ab, :cd
6
+ properties :cd, :ef
7
+ end
8
+ end
9
+
10
+ it 'add attributes' do
11
+ instance = subject.new(ab: 'ab', cd: 'cd', ef: 'ef', skip: 'this')
12
+ expect(instance.ab).to eq('ab')
13
+ expect(instance.cd).to eq('cd')
14
+ expect(instance.ef).to eq('ef')
15
+ expect { instance.skip }.to raise_error(NoMethodError)
16
+ end
17
+ end
18
+
19
+ context 'property' do
20
+ context 'add' do
21
+ it 'new property' do
22
+ test_obj = object_factory(attributes: { ab: 'ab', cd: 'cd', skip: 'this' }) do
23
+ property :ab
24
+ property :cd
25
+ end
26
+
27
+ expect(test_obj.ab).to eq('ab')
28
+ expect(test_obj.cd).to eq('cd')
29
+ expect { test_obj.skip }.to raise_error(NoMethodError)
30
+ end
31
+ end
32
+
33
+ context 'with options' do
34
+ context 'add with :from option' do
35
+ subject do
36
+ object_factory(attributes: { aB: 'ab' }) do
37
+ property :ab, from: :aB
38
+ end
39
+ end
40
+
41
+ it 'assign property value from key provided by :from option' do
42
+ expect(subject.ab).to eq('ab')
43
+ end
44
+ end
45
+
46
+ context 'add with :default option' do
47
+ subject do
48
+ object_factory(attributes: { ab: '' }) do
49
+ property :ab, default: 'Default'
50
+ end
51
+ end
52
+
53
+ it 'assign property value from key provided by :default option when value is empty' do
54
+ expect(subject.ab).to eq('Default')
55
+ end
56
+ end
57
+
58
+ context 'add with :default and :transform_with option' do
59
+ let(:time) { Time.parse('2015-03-29 14:41:28 +0200') }
60
+ subject do
61
+ object_factory(attributes: { time: '' }) do
62
+ property :time, default: Time.parse('2015-03-29 14:41:28 +0200'), transform_with: -> (v) { Time.parse(v) }
63
+ end
64
+ end
65
+
66
+ it 'assign property value from key :default when value is empty and skip :transform_with' do
67
+ expect(subject.time).to eq(time)
68
+ end
69
+ end
70
+
71
+ context 'add with :transform_with option assign property value after transformation' do
72
+ it 'by proc' do
73
+ test_obj = object_factory(attributes: { number: '12' }) do
74
+ property :number, transform_with: -> (v) { v.to_i }
75
+ end
76
+
77
+ expect(test_obj.number).to eq(12)
78
+ end
79
+
80
+ it 'by method' do
81
+ test_obj = object_factory(attributes: { number: '12' }) do
82
+ property :number, transform_with: :convert_to_number
83
+
84
+ def convert_to_number(value)
85
+ value.to_i
86
+ end
87
+ end
88
+
89
+ expect(test_obj.number).to eq(12)
90
+ end
91
+ end
92
+
93
+ context 'add with :validates option' do
94
+ subject do
95
+ object_factory(real_class_name: 'FakeForm', attributes: { name: '' }) do
96
+ property :name, validates: { presence: true }
97
+ end
98
+ end
99
+
100
+ it 'attribute has validation' do
101
+ expect(subject.valid?).to eq(false)
102
+ expect(subject.errors.as_json).to eq(name: ["can't be blank"])
103
+ end
104
+ end
105
+ end
106
+ # context 'add nested' do
107
+ # it 'first_level' do
108
+ # test_obj = object_factory(attributes: { ab: { cd: 'cd'} }) do
109
+ # property :ab do
110
+ # property :cd
111
+ # end
112
+ # end
113
+
114
+ # expect(test_obj.ab).to eq(cd: 'cd')
115
+ # end
116
+ # end
117
+ end
118
+ end
@@ -0,0 +1,31 @@
1
+ require 'bundler/setup'
2
+ require 'awesome_print'
3
+ Bundler.setup
4
+
5
+ require 'coveralls'
6
+ Coveralls.wear!
7
+
8
+ require 'light_form'
9
+ Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
10
+
11
+ RSpec.configure do |config|
12
+ config.filter_run :focus
13
+ config.run_all_when_everything_filtered = true
14
+
15
+ config.default_formatter = 'doc' if config.files_to_run.one?
16
+
17
+ config.profile_examples = 10
18
+ config.order = :random
19
+
20
+ Kernel.srand config.seed
21
+
22
+ config.expect_with :rspec do |expectations|
23
+ expectations.syntax = :expect
24
+ end
25
+
26
+ config.mock_with :rspec do |mocks|
27
+ mocks.syntax = :expect
28
+ mocks.verify_partial_doubles = true
29
+ end
30
+ end
31
+ require 'guard/rubocop'
@@ -0,0 +1,11 @@
1
+ def class_factory(opts = {}, &block)
2
+ Class.new(described_class).tap do |klass|
3
+ klass.class_eval(&block) if block_given?
4
+ Object.const_set(opts[:real_class_name], klass) if opts[:real_class_name]
5
+ klass.class_eval("def self.name; \"#{name}\"; end") if opts[:name]
6
+ end
7
+ end
8
+
9
+ def object_factory(opts = {}, &block)
10
+ class_factory(opts, &block).new(opts.fetch(:attributes, {}))
11
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: light_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pawel Niemczyk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-29 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: 3.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.2
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.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
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.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.4'
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
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.7'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.7'
125
+ - !ruby/object:Gem::Dependency
126
+ name: awesome_print
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.6'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.6'
139
+ description: Light form
140
+ email:
141
+ - pniemczyk.info@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - Guardfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - lib/light_form.rb
156
+ - lib/light_form/form.rb
157
+ - lib/light_form/version.rb
158
+ - light_form.gemspec
159
+ - spec/lib/form_spec.rb
160
+ - spec/spec_helper.rb
161
+ - spec/support/factory_helper.rb
162
+ homepage: https://github.com/pniemczyk/light_form
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.4.5
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Light Form
186
+ test_files:
187
+ - spec/lib/form_spec.rb
188
+ - spec/spec_helper.rb
189
+ - spec/support/factory_helper.rb