formup 1.0.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: 7ebc0d5bea3cc821ffd21007e02a4c5a803daa00
4
+ data.tar.gz: 074890af86518f3930639a62cd4100d6b6a064ea
5
+ SHA512:
6
+ metadata.gz: 58b6d583641ac427fae6dbcf8e379670708712eea9cd8a01b82f7117146ea70b290f1333c8078609d45625ac141d52ca2f18a918cff347b7b1ecf522d700379d
7
+ data.tar.gz: 7d62563a81aa83bfed8c48af7501a6c355e1d863804cf7031769f50dc64d22ab96bc82d36fc9822931c288682fc1951e5fea490a32d3f4661a6706667b2b1ec5
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ gemfile:
6
+ - gemfiles/activemodel_3_2_x.gemfile
7
+ - gemfiles/activemodel_4_0_x.gemfile
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in formup.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 pinzolo
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,77 @@
1
+ # Formup
2
+
3
+ [![Build Status](https://secure.travis-ci.org/pinzolo/formup.png)](http://travis-ci.org/pinzolo/formup)
4
+
5
+ Formup is rubygem for creating data model based form class.
6
+ Formup use activemodel gem.(>= 3.2.0)
7
+ You can get the benefits of ActiveModel. ex) validations, application to `form_for` etc.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'formup'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install formup
22
+
23
+ ## Usage
24
+
25
+ ### Include Formup to your class.
26
+
27
+ ```ruby
28
+ class SalesForm
29
+ include Formup
30
+
31
+ # define attributes (sales_id, sales_price, sales_tax)
32
+ source :sales, :attributes => [:id, :price, :tax]
33
+ # define attributes (customer_id, customer_name, contact)
34
+ source :customer, :attributes => [:id, :name], :aliases => { :email => :contact }
35
+
36
+ attr_accessor :items
37
+
38
+ # Handle hash parameters that were not processed in initialize method.
39
+ def handle_extra_params(params)
40
+ self.items = {}
41
+ params.each do |k, v|
42
+ self.items[v] = params[k.to_s.gsub(/^code/, "count")] if k.to_s.match(/^code/)
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ ### Initialize with hash parameters.(like ActiveRecord)
49
+
50
+ ```ruby
51
+ # params => { :sales_id => 1, :sales_price => 1000, ... }
52
+ form = SalesForm.new(params)
53
+ ```
54
+
55
+ ### Load from hash and data model that have some accessor methods.
56
+
57
+ ```ruby
58
+ form = SalesForm.new
59
+ sales = Sales.find(1)
60
+ form.load(:sales => sales, :customer => { :id => 1, :name => "John Doe", :email => "john@example.com"})
61
+ ```
62
+
63
+ ### Get parameters for data model by `params_for` method.
64
+
65
+ ```ruby
66
+ form = SalesForm.new(params)
67
+ sales = Sales.new(form.params_for(:sales))
68
+ sales.save
69
+ ```
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ task :default => [:spec]
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = 'spec/**/*_spec.rb'
9
+ spec.rspec_opts = ['-cfs']
10
+ end
11
+ rescue LoadError => e
12
+ end
data/formup.gemspec ADDED
@@ -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 'formup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "formup"
8
+ spec.version = Formup::VERSION
9
+ spec.authors = ["pinzlo"]
10
+ spec.email = ["pinzolo@gmail.com"]
11
+ spec.description = %q{formup is rubygem for creating data model based form class}
12
+ spec.summary = %q{Create data model based form class}
13
+ spec.homepage = "https://github.com/pinzolo/formup"
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_runtime_dependency "activemodel", ">=3.2.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activemodel", "~>3.2.0"
4
+
5
+ gemspec :path => "../"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activemodel", "~>4.0.0"
4
+
5
+ gemspec :path => "../"
data/lib/formup.rb ADDED
@@ -0,0 +1,127 @@
1
+ # coding: utf-8
2
+ require "formup/version"
3
+ require "formup/source"
4
+ require "active_support/concern"
5
+ require "active_model"
6
+
7
+ module Formup
8
+ extend ActiveSupport::Concern
9
+
10
+ TRUE_VALUES = [1, true, "1", "true", "t"]
11
+
12
+ included do
13
+ include ActiveModel::Conversion
14
+ include ActiveModel::Validations
15
+ extend ActiveModel::Naming
16
+ extend ActiveModel::Translation
17
+ end
18
+
19
+ # Class methods {{{
20
+ module ClassMethods
21
+ def sources
22
+ initialize_sources
23
+ @sources.dup
24
+ end
25
+
26
+ def source(key, options={})
27
+ initialize_sources
28
+ attribute_defs = create_attribute_defs(key, options[:attributes], options[:aliases])
29
+ @sources[key] = Formup::Source.new(key, attribute_defs)
30
+ deploy_attributes(attribute_defs)
31
+ end
32
+
33
+ private
34
+ def create_attribute_defs(key, attributes, aliases)
35
+ attribute_defs = {}.with_indifferent_access
36
+
37
+ if attributes
38
+ attributes.each do |attr|
39
+ attribute_defs[attr.to_s] = "#{key}_#{attr}"
40
+ end
41
+ end
42
+ if aliases
43
+ aliases.each do |k, v|
44
+ attribute_defs[k.to_s] = v.to_s
45
+ end
46
+ end
47
+ attribute_defs
48
+ end
49
+
50
+ def deploy_attributes(defs)
51
+ defs.values.each do |attr|
52
+ attr_accessor attr
53
+
54
+ define_method "#{attr}?" do
55
+ Formup::TRUE_VALUES.include?(self.__send__(attr))
56
+ end
57
+ end
58
+ end
59
+
60
+ def initialize_sources
61
+ @sources ||= {}.with_indifferent_access
62
+ end
63
+ end
64
+ # }}}
65
+
66
+ # Instance methods {{{
67
+ def initialize(params = {})
68
+ return unless params
69
+
70
+ parameters = params.dup.with_indifferent_access
71
+ self.class.sources.each do |_, src|
72
+ src.attribute_defs.each do |attr_def|
73
+ __send__(attr_def.attr.to_s + "=", parameters.delete(attr_def.attr)) if parameters.key?(attr_def.attr)
74
+ end
75
+ end
76
+ handle_extra_params(parameters) unless parameters.empty?
77
+ end
78
+
79
+ def handle_extra_params(parameters)
80
+ end
81
+
82
+ def persisted?
83
+ false
84
+ end
85
+
86
+ def params_for(key,*excludes_attrs)
87
+ if excludes_attrs && excludes_attrs.first == false
88
+ excludes = []
89
+ else
90
+ excludes = [excludes_attrs].flatten.compact
91
+ excludes << :id if excludes.empty?
92
+ end
93
+
94
+ parameters = {}.with_indifferent_access
95
+ return parameters unless self.class.sources.key?(key)
96
+
97
+ source = self.class.sources[key]
98
+ source.attribute_defs.inject(parameters) do |result, attr_def|
99
+ result[attr_def.base] = __send__(attr_def.attr) if excludes.all? { |attr| attr.to_s != attr_def.base }
100
+ result
101
+ end
102
+ end
103
+
104
+ def load(params = {})
105
+ params.each do |k, v|
106
+ if self.class.sources.key?(k)
107
+ source = self.class.sources[k]
108
+ source.attribute_defs.each do |attr_def|
109
+ value = extract_value(v, attr_def.base)
110
+ __send__(attr_def.attr + "=", value) if value
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ private
117
+ def extract_value(obj, attr)
118
+ if obj.respond_to?(attr)
119
+ obj.__send__(attr)
120
+ elsif obj.respond_to?(:[])
121
+ obj[attr.to_s] || obj[attr.to_sym]
122
+ else
123
+ nil
124
+ end
125
+ end
126
+ # }}}
127
+ end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+ module Formup
3
+ class AttrDef
4
+ attr_reader :base, :attr
5
+
6
+ def initialize(base, attr)
7
+ @base = base.to_s if base
8
+ @attr = attr.to_s if attr
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ require "active_support/core_ext/hash/indifferent_access"
3
+ require "formup/attr_def"
4
+
5
+ module Formup
6
+ class Source
7
+
8
+ attr_accessor :key, :attribute_defs
9
+
10
+ def initialize(key, attribute_defs = {}, excludes = nil)
11
+ raise "Formup::Source require key param." if key.nil?
12
+
13
+ @key = key.to_s
14
+ @attribute_defs = attribute_defs.map { |k, v| Formup::AttrDef.new(k, v) } if attribute_defs
15
+ @attribute_defs ||= []
16
+ end
17
+
18
+ def base?(base)
19
+ @attribute_defs.any? { |attr_def| attr_def.base == base.to_s }
20
+ end
21
+
22
+ def base(attr)
23
+ attr_def = @attribute_defs.detect { |attr_def| attr_def.attr == attr.to_s }
24
+ attr_def ? attr_def.base : nil
25
+ end
26
+
27
+ def attr?(attr)
28
+ @attribute_defs.any? { |attr_def| attr_def.attr == attr.to_s }
29
+ end
30
+
31
+ def attr(base)
32
+ attr_def = @attribute_defs.detect { |attr_def| attr_def.base == base.to_s }
33
+ attr_def ? attr_def.attr : nil
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ # coding: utf-8
2
+ module Formup
3
+ VERSION = "1.0.0"
4
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ require "formup/attr_def"
3
+
4
+ describe Formup::AttrDef do
5
+ describe "#initailize" do
6
+ context "given not string arguments" do
7
+ before do
8
+ @attr_def = Formup::AttrDef.new(:id, 1)
9
+ end
10
+
11
+ describe "#base" do
12
+ it "returns string value" do
13
+ expect(@attr_def.base).to eq "id"
14
+ end
15
+ end
16
+
17
+ describe "#attr" do
18
+ it "returns string value" do
19
+ expect(@attr_def.attr).to eq "1"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,146 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup/source"
4
+ require "active_support/hash_with_indifferent_access"
5
+
6
+ describe Formup::Source do
7
+ describe "#initialize" do
8
+ context "with key only" do
9
+ before do
10
+ @src = Formup::Source.new(:user)
11
+ end
12
+
13
+ describe "#attribute_defs" do
14
+ it "returns empty Hash" do
15
+ expect(@src.attribute_defs).not_to be_nil
16
+ end
17
+ end
18
+ end
19
+
20
+ context "with string key" do
21
+ before do
22
+ @src = Formup::Source.new("user")
23
+ end
24
+
25
+ describe "#key" do
26
+ it "returns string keye" do
27
+ expect(@src.key).to eq "user"
28
+ end
29
+ end
30
+ end
31
+
32
+ context "with symbol key" do
33
+ before do
34
+ @src = Formup::Source.new(:user)
35
+ end
36
+
37
+ describe "#key" do
38
+ it "returns string keye" do
39
+ expect(@src.key).to eq "user"
40
+ end
41
+ end
42
+ end
43
+
44
+ context "with nil as key" do
45
+ it "raise error" do
46
+ expect { Formup::Source.new(nil) }.to raise_error
47
+ end
48
+ end
49
+
50
+ context "with attribute_defs" do
51
+ before do
52
+ @src = Formup::Source.new(:user, {:name => :nickname, :email => :user_email})
53
+ end
54
+
55
+ describe "#attribute_defs" do
56
+ it "returns Array" do
57
+ expect(@src.attribute_defs).to be_a_instance_of(Array)
58
+ end
59
+
60
+ it "returns Formup::AttrDef array" do
61
+ expect(@src.attribute_defs.all? { |attr_def| attr_def.is_a?(Formup::AttrDef) }).to eq true
62
+ end
63
+ end
64
+
65
+ describe "#base?" do
66
+ it "has name as base" do
67
+ expect(@src.base?("name")).to eq true
68
+ end
69
+
70
+ it "has email as base" do
71
+ expect(@src.base?("email")).to eq true
72
+ end
73
+
74
+ it "can accept symbol argument" do
75
+ expect(@src.base?(:name)).to eq true
76
+ end
77
+ end
78
+
79
+ describe "#base" do
80
+ context "argument is nickname" do
81
+ it "returns name" do
82
+ expect(@src.base("nickname")).to eq "name"
83
+ end
84
+ end
85
+
86
+ context "argument is user_email" do
87
+ it "returns email" do
88
+ expect(@src.base("user_email")).to eq "email"
89
+ end
90
+ end
91
+
92
+ context "argument is user_age" do
93
+ it "returns nil" do
94
+ expect(@src.base("user_age")).to be_nil
95
+ end
96
+ end
97
+
98
+ context "argument is a symbol" do
99
+ it "returns same value when string argument" do
100
+ expect(@src.base(:user_email)).to eq "email"
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "#attr?" do
106
+ it "has nickname as attr" do
107
+ expect(@src.attr?("nickname")).to eq true
108
+ end
109
+
110
+ it "has user_email as attr" do
111
+ expect(@src.attr?("user_email")).to eq true
112
+ end
113
+
114
+ it "can accept symbol argument" do
115
+ expect(@src.attr?(:nickname)).to eq true
116
+ end
117
+ end
118
+
119
+ describe "#attr" do
120
+ context "argument is name" do
121
+ it "returns nickname" do
122
+ expect(@src.attr("name")).to eq "nickname"
123
+ end
124
+ end
125
+
126
+ context "argument is email" do
127
+ it "returns user_email" do
128
+ expect(@src.attr("email")).to eq "user_email"
129
+ end
130
+ end
131
+
132
+ context "arument is age" do
133
+ it "returns nil" do
134
+ expect(@src.attr("age")).to be_nil
135
+ end
136
+ end
137
+
138
+ context "argument is a symbol" do
139
+ it "returns same value when string argument" do
140
+ expect(@src.attr(:name)).to eq "nickname"
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,144 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup"
4
+
5
+ describe Formup do
6
+ describe "boolean cast method" do
7
+ before do
8
+ class TestClassForBooleanCastMethod
9
+ include Formup
10
+ source :key, :attributes => [:value]
11
+ end
12
+ end
13
+
14
+ before(:each) do
15
+ @model = TestClassForBooleanCastMethod.new
16
+ end
17
+
18
+ context "when true assigned" do
19
+ before do
20
+ @model.key_value = true
21
+ end
22
+ it "returns true" do
23
+ expect(@model.key_value?).to eq true
24
+ end
25
+ end
26
+
27
+ context "when 1 assigned" do
28
+ before do
29
+ @model.key_value = 1
30
+ end
31
+ it "returns true" do
32
+ expect(@model.key_value?).to eq true
33
+ end
34
+ end
35
+
36
+ context "when '1' assigned" do
37
+ before do
38
+ @model.key_value = '1'
39
+ end
40
+ it "returns true" do
41
+ expect(@model.key_value?).to eq true
42
+ end
43
+ end
44
+
45
+ context "when 't' assigned" do
46
+ before do
47
+ @model.key_value = 't'
48
+ end
49
+ it "returns true" do
50
+ expect(@model.key_value?).to eq true
51
+ end
52
+ end
53
+
54
+ context "when 'true' assigned" do
55
+ before do
56
+ @model.key_value = 'true'
57
+ end
58
+ it "returns true" do
59
+ expect(@model.key_value?).to eq true
60
+ end
61
+ end
62
+
63
+ context "when false assigned" do
64
+ before do
65
+ @model.key_value = false
66
+ end
67
+ it "returns false" do
68
+ expect(@model.key_value?).to eq false
69
+ end
70
+ end
71
+
72
+ context "when 0 assigned" do
73
+ before do
74
+ @model.key_value = 0
75
+ end
76
+ it "returns false" do
77
+ expect(@model.key_value?).to eq false
78
+ end
79
+ end
80
+
81
+ context "when '0' assigned" do
82
+ before do
83
+ @model.key_value = '0'
84
+ end
85
+ it "returns false" do
86
+ expect(@model.key_value?).to eq false
87
+ end
88
+ end
89
+
90
+ context "when 'f' assigned" do
91
+ before do
92
+ @model.key_value = 'f'
93
+ end
94
+ it "returns false" do
95
+ expect(@model.key_value?).to eq false
96
+ end
97
+ end
98
+
99
+ context "when 'false' assigned" do
100
+ before do
101
+ @model.key_value = 'false'
102
+ end
103
+ it "returns false" do
104
+ expect(@model.key_value?).to eq false
105
+ end
106
+ end
107
+
108
+ context "when other Number assigned" do
109
+ before do
110
+ @model.key_value = 10
111
+ end
112
+ it "returns false" do
113
+ expect(@model.key_value?).to eq false
114
+ end
115
+ end
116
+
117
+ context "when other String assigned" do
118
+ before do
119
+ @model.key_value = 'test'
120
+ end
121
+ it "returns false" do
122
+ expect(@model.key_value?).to eq false
123
+ end
124
+ end
125
+
126
+ context "when other Object assigned" do
127
+ before do
128
+ @model.key_value = :test
129
+ end
130
+ it "returns false" do
131
+ expect(@model.key_value?).to eq false
132
+ end
133
+ end
134
+
135
+ context "when nil" do
136
+ before do
137
+ @model.key_value = nil
138
+ end
139
+ it "returns false" do
140
+ expect(@model.key_value?).to eq false
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,69 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup"
4
+ require "active_model/conversion"
5
+ require "active_model/validations"
6
+ require "active_model/naming"
7
+ require "active_model/translation"
8
+
9
+ describe Formup do
10
+ context "included" do
11
+ before do
12
+ class TestClassForIncluded
13
+ include Formup
14
+ end
15
+ @obj = TestClassForIncluded.new
16
+ end
17
+
18
+ describe ActiveModel::Conversion do
19
+ it "is included" do
20
+ expect(TestClassForIncluded.included_modules.include?(ActiveModel::Conversion)).to eq true
21
+ end
22
+ end
23
+
24
+ describe ActiveModel::Validations do
25
+ it "contains ActiveModel::Validations" do
26
+ expect(TestClassForIncluded.included_modules.include?(ActiveModel::Validations)).to eq true
27
+ end
28
+ end
29
+
30
+ describe ActiveModel::Naming do
31
+ it "is extended" do
32
+ expect((class << TestClassForIncluded; self end).included_modules.include?(ActiveModel::Naming)).to eq true
33
+ end
34
+ end
35
+
36
+ describe ActiveModel::Translation do
37
+ it "is extended" do
38
+ expect((class << TestClassForIncluded; self end).included_modules.include?(ActiveModel::Translation)).to eq true
39
+ end
40
+ end
41
+
42
+ describe "#persisted?" do
43
+ it "defined" do
44
+ expect(@obj.respond_to?(:persisted?)).to eq true
45
+ end
46
+
47
+ it "returns false" do
48
+ expect(@obj.persisted?).to eq false
49
+ end
50
+ end
51
+
52
+ describe ".source" do
53
+ it "difined" do
54
+ expect(TestClassForIncluded.respond_to?(:source)).to eq true
55
+ end
56
+ end
57
+
58
+ describe ".sources" do
59
+ it "defined" do
60
+ expect { TestClassForIncluded.sources }.not_to raise_error
61
+ end
62
+
63
+ it "returns empty Hash" do
64
+ expect(TestClassForIncluded.sources.length).to eq 0
65
+ end
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup"
4
+
5
+ describe Formup do
6
+ describe "#initialize" do
7
+ before do
8
+ class TestClassForInitialize
9
+ include Formup
10
+ source :key1, :attributes => [:id, :name, :value]
11
+ source :key2, :attributes => [:email], :aliases => { :id => :key, :name => :label }
12
+
13
+ attr_accessor :extra_params_handled
14
+
15
+ def handle_extra_params(params)
16
+ @extra_params_handled = true
17
+ end
18
+ end
19
+ @obj = TestClassForInitialize.new({:key1_id => 1, :key1_name => "foo", :key1_value => true,
20
+ :key => 2, :label => "bar", :key2_email => "test@example.com", :extra => "baz"})
21
+ end
22
+
23
+ it "values assigned" do
24
+ expect(@obj.key1_id).to eq 1
25
+ expect(@obj.key1_name).to eq "foo"
26
+ expect(@obj.key1_value).to eq true
27
+ expect(@obj.key).to eq 2
28
+ expect(@obj.label).to eq "bar"
29
+ expect(@obj.key2_email).to eq "test@example.com"
30
+ end
31
+
32
+ it "handle_extra_params called" do
33
+ expect(@obj.extra_params_handled).to eq true
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,82 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup"
4
+
5
+ describe Formup do
6
+ describe "#initialize" do
7
+ before do
8
+ class TestClassForLoad
9
+ include Formup
10
+ source :key1, :attributes => [:id, :name, :value]
11
+ source :key2, :attributes => [:email], :aliases => { :id => :key, :name => :label }
12
+ end
13
+
14
+ class DataModel
15
+ attr_accessor :id, :name, :email
16
+ end
17
+ end
18
+
19
+ context "with hash" do
20
+ before do
21
+ @obj = TestClassForLoad.new
22
+ @obj.load(:key1 => { :id => 1, :name => "foo", :value => true})
23
+ end
24
+
25
+ it "values assigned" do
26
+ expect(@obj.key1_id).to eq 1
27
+ expect(@obj.key1_name).to eq "foo"
28
+ expect(@obj.key1_value).to eq true
29
+ end
30
+
31
+ it "other values not assined" do
32
+ expect(@obj.key).to be_nil
33
+ expect(@obj.label).to be_nil
34
+ expect(@obj.key2_email).to be_nil
35
+ end
36
+ end
37
+
38
+ context "with data model" do
39
+ before do
40
+ data = DataModel.new
41
+ data.id = 2
42
+ data.name = "bar"
43
+ data.email = "test@example.com"
44
+ @obj = TestClassForLoad.new
45
+ @obj.load(:key2 => data)
46
+ end
47
+
48
+ it "values assigned" do
49
+ expect(@obj.key).to eq 2
50
+ expect(@obj.label).to eq "bar"
51
+ expect(@obj.key2_email).to eq "test@example.com"
52
+ end
53
+
54
+ it "other values not assined" do
55
+ expect(@obj.key1_id).to be_nil
56
+ expect(@obj.key1_name).to be_nil
57
+ expect(@obj.key1_value).to be_nil
58
+ end
59
+ end
60
+
61
+ context "with hash and data model" do
62
+ before do
63
+ data = DataModel.new
64
+ data.id = 2
65
+ data.name = "bar"
66
+ data.email = "test@example.com"
67
+ @obj = TestClassForLoad.new
68
+ @obj.load(:key1 => { :id => 1, :name => "foo", :value => true}, :key2 => data)
69
+ end
70
+
71
+ it "values assigned" do
72
+ expect(@obj.key1_id).to eq 1
73
+ expect(@obj.key1_name).to eq "foo"
74
+ expect(@obj.key1_value).to eq true
75
+ expect(@obj.key).to eq 2
76
+ expect(@obj.label).to eq "bar"
77
+ expect(@obj.key2_email).to eq "test@example.com"
78
+ end
79
+ end
80
+ end
81
+ end
82
+
@@ -0,0 +1,114 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup"
4
+
5
+ describe Formup do
6
+ before do
7
+ class TestClassForParamsFor1
8
+ include Formup
9
+ source :key1, :attributes => [:id, :name, :value]
10
+ source :key2, :attributes => [:email], :aliases => { :id => :key, :name => :label }
11
+ end
12
+ end
13
+
14
+ before(:each) do
15
+ @obj = TestClassForParamsFor1.new do
16
+ key1_id = 1
17
+ key1_name = "foo"
18
+ key1_value = "bar"
19
+ key = 2
20
+ label = "baz"
21
+ key2_email = "hoge@example.com"
22
+ end
23
+ end
24
+
25
+ describe "#params_for(key)" do
26
+ it "returns hash that doesn't have id key(declared by attributes)" do
27
+ params = @obj.params_for(:key1)
28
+ expect(params.length).to eq 2
29
+ expect(params.key?(:id)).to eq false
30
+ expect(params.key?(:name)).to eq true
31
+ expect(params.key?(:value)).to eq true
32
+ end
33
+
34
+ it "returns hash that doesn't have id key(declared by aliases)" do
35
+ params = @obj.params_for(:key2)
36
+ expect(params.length).to eq 2
37
+ expect(params.key?(:id)).to eq false
38
+ expect(params.key?(:name)).to eq true
39
+ expect(params.key?(:email)).to eq true
40
+ end
41
+ end
42
+
43
+ describe "#params_for(key, symbol)" do
44
+ it "returns hash that doesn't have name key(declared by attributes)" do
45
+ params = @obj.params_for(:key1, :name)
46
+ expect(params.length).to eq 2
47
+ expect(params.key?(:id)).to eq true
48
+ expect(params.key?(:name)).to eq false
49
+ expect(params.key?(:value)).to eq true
50
+ end
51
+
52
+ it "returns hash that doesn't have name key(declared by aliases)" do
53
+ params = @obj.params_for(:key2, :name)
54
+ expect(params.length).to eq 2
55
+ expect(params.key?(:id)).to eq true
56
+ expect(params.key?(:name)).to eq false
57
+ expect(params.key?(:email)).to eq true
58
+ end
59
+ end
60
+
61
+ describe "#params_for(key, string)" do
62
+ it "returns hash that doesn't have name key(declared by attributes)" do
63
+ params = @obj.params_for(:key1, "name")
64
+ expect(params.length).to eq 2
65
+ expect(params.key?(:id)).to eq true
66
+ expect(params.key?(:name)).to eq false
67
+ expect(params.key?(:value)).to eq true
68
+ end
69
+
70
+ it "returns hash that doesn't have name key(declared by aliases)" do
71
+ params = @obj.params_for(:key2, "name")
72
+ expect(params.length).to eq 2
73
+ expect(params.key?(:id)).to eq true
74
+ expect(params.key?(:name)).to eq false
75
+ expect(params.key?(:email)).to eq true
76
+ end
77
+ end
78
+
79
+ describe "#params_for(key, array)" do
80
+ it "returns hash that doesn't have id and name keys(declared by attributes)" do
81
+ params = @obj.params_for(:key1, :id, :name)
82
+ expect(params.length).to eq 1
83
+ expect(params.key?(:id)).to eq false
84
+ expect(params.key?(:name)).to eq false
85
+ expect(params.key?(:value)).to eq true
86
+ end
87
+
88
+ it "returns hash that doesn't have id and name keys(declared by aliases)" do
89
+ params = @obj.params_for(:key2, "id", "name")
90
+ expect(params.length).to eq 1
91
+ expect(params.key?(:id)).to eq false
92
+ expect(params.key?(:name)).to eq false
93
+ expect(params.key?(:email)).to eq true
94
+ end
95
+ end
96
+
97
+ describe "#params_for(key, false)" do
98
+ it "returns hash that has all keys(declared by attributes)" do
99
+ params = @obj.params_for(:key1, false)
100
+ expect(params.length).to eq 3
101
+ expect(params.key?(:id)).to eq true
102
+ expect(params.key?(:name)).to eq true
103
+ expect(params.key?(:value)).to eq true
104
+ end
105
+
106
+ it "returns hash that has all keys(declared by aliases)" do
107
+ params = @obj.params_for(:key2, false)
108
+ expect(params.length).to eq 3
109
+ expect(params.key?(:id)).to eq true
110
+ expect(params.key?(:name)).to eq true
111
+ expect(params.key?(:email)).to eq true
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,78 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup"
4
+
5
+ describe Formup do
6
+ context "source method called" do
7
+ before do
8
+ class TestClassForSourceCalled
9
+ include Formup
10
+ source :key1, :attributes => [:attr1, :attr2, :attr3]
11
+ source :key2, :aliases => {:attr1 => :alias1, :attr2 => :alias2}, :excludes => :attr1
12
+ source :key3, :attributes => [:attr1, :attr2], :aliases => {:attr3 => :alias3, :attr4 => :alias4}, :excludes => [:attr3, :attr4]
13
+ end
14
+ end
15
+
16
+ before(:each) do
17
+ @obj = TestClassForSourceCalled.new
18
+ end
19
+
20
+ describe ".sources" do
21
+ it "has 3 items" do
22
+ expect(TestClassForSourceCalled.sources.length).to eq 3
23
+ end
24
+
25
+ it "contains given keys" do
26
+ expect(TestClassForSourceCalled.sources.key?(:key1)).to eq true
27
+ expect(TestClassForSourceCalled.sources.key?(:key2)).to eq true
28
+ expect(TestClassForSourceCalled.sources.key?(:key3)).to eq true
29
+ end
30
+
31
+ it "has values joined key and attributes" do
32
+ expect(TestClassForSourceCalled.sources[:key1].attr(:attr1)).to eq "key1_attr1"
33
+ expect(TestClassForSourceCalled.sources[:key1].attr(:attr2)).to eq "key1_attr2"
34
+ expect(TestClassForSourceCalled.sources[:key1].attr(:attr3)).to eq "key1_attr3"
35
+ expect(TestClassForSourceCalled.sources[:key3].attr(:attr1)).to eq "key3_attr1"
36
+ expect(TestClassForSourceCalled.sources[:key3].attr(:attr2)).to eq "key3_attr2"
37
+ end
38
+
39
+ it "has values given by aliases" do
40
+ expect(TestClassForSourceCalled.sources[:key2].attr(:attr1)).to eq "alias1"
41
+ expect(TestClassForSourceCalled.sources[:key2].attr(:attr2)).to eq "alias2"
42
+ expect(TestClassForSourceCalled.sources[:key3].attr(:attr3)).to eq "alias3"
43
+ expect(TestClassForSourceCalled.sources[:key3].attr(:attr4)).to eq "alias4"
44
+ end
45
+ end
46
+
47
+ it "define accessor methods by attributes option" do
48
+ expect(@obj.respond_to?(:key1_attr1)).to eq true
49
+ expect(@obj.respond_to?(:key1_attr1=)).to eq true
50
+ expect(@obj.respond_to?(:key1_attr2)).to eq true
51
+ expect(@obj.respond_to?(:key1_attr2=)).to eq true
52
+ expect(@obj.respond_to?(:key1_attr3)).to eq true
53
+ expect(@obj.respond_to?(:key1_attr3=)).to eq true
54
+ expect(@obj.respond_to?(:key3_attr1)).to eq true
55
+ expect(@obj.respond_to?(:key3_attr1=)).to eq true
56
+ expect(@obj.respond_to?(:key3_attr2)).to eq true
57
+ expect(@obj.respond_to?(:key3_attr2=)).to eq true
58
+ end
59
+
60
+ it "define accessor methods by aliases option" do
61
+ expect(@obj.respond_to?(:alias1)).to eq true
62
+ expect(@obj.respond_to?(:alias2)).to eq true
63
+ expect(@obj.respond_to?(:alias3)).to eq true
64
+ expect(@obj.respond_to?(:alias4)).to eq true
65
+ end
66
+
67
+ it "define boolean methods" do
68
+ expect(@obj.respond_to?(:key1_attr1?)).to eq true
69
+ expect(@obj.respond_to?(:key1_attr2?)).to eq true
70
+ expect(@obj.respond_to?(:key1_attr3?)).to eq true
71
+ expect(@obj.respond_to?(:key3_attr2?)).to eq true
72
+ expect(@obj.respond_to?(:alias1?)).to eq true
73
+ expect(@obj.respond_to?(:alias2?)).to eq true
74
+ expect(@obj.respond_to?(:alias3?)).to eq true
75
+ expect(@obj.respond_to?(:alias4?)).to eq true
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+ require "formup"
4
+
5
+ describe Formup do
6
+ context "included" do
7
+ before do
8
+ class TestClassForValidation
9
+ include Formup
10
+
11
+ source :item, :attributes => [:name]
12
+ attr_accessor :price
13
+
14
+ validates :item_name, :presence => true
15
+ validates :price, :inclusion => { :in => 0..10000 }
16
+ end
17
+ end
18
+
19
+ before(:each) do
20
+ @obj = TestClassForValidation.new(:item_name => "foo")
21
+ @obj.price = 5000
22
+ end
23
+
24
+ it "validation of normal attribute is enable" do
25
+ expect(@obj.valid?).to eq true
26
+ @obj.price = -1000
27
+ expect(@obj.valid?).to eq false
28
+ end
29
+
30
+ it "validation of attribute that defined by source method is enable" do
31
+ expect(@obj.valid?).to eq true
32
+ @obj.item_name = ""
33
+ expect(@obj.valid?).to eq false
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ RSpec.configure do |config|
3
+ config.expect_with :rspec do |c|
4
+ c.syntax = :expect
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formup
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - pinzlo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-17 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.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '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
+ description: formup is rubygem for creating data model based form class
70
+ email:
71
+ - pinzolo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - formup.gemspec
84
+ - gemfiles/activemodel_3_2_x.gemfile
85
+ - gemfiles/activemodel_4_0_x.gemfile
86
+ - lib/formup.rb
87
+ - lib/formup/attr_def.rb
88
+ - lib/formup/source.rb
89
+ - lib/formup/version.rb
90
+ - spec/formup/attr_def_spec.rb
91
+ - spec/formup/source_spec.rb
92
+ - spec/formup_boolean_cast_spec.rb
93
+ - spec/formup_included_spec.rb
94
+ - spec/formup_initialize_spec.rb
95
+ - spec/formup_load_spec.rb
96
+ - spec/formup_params_for_spec.rb
97
+ - spec/formup_source_called_spec.rb
98
+ - spec/formup_validation_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/pinzolo/formup
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.0.2
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Create data model based form class
124
+ test_files:
125
+ - spec/formup/attr_def_spec.rb
126
+ - spec/formup/source_spec.rb
127
+ - spec/formup_boolean_cast_spec.rb
128
+ - spec/formup_included_spec.rb
129
+ - spec/formup_initialize_spec.rb
130
+ - spec/formup_load_spec.rb
131
+ - spec/formup_params_for_spec.rb
132
+ - spec/formup_source_called_spec.rb
133
+ - spec/formup_validation_spec.rb
134
+ - spec/spec_helper.rb