dactyl_form 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/lib/dactyl_form.rb +125 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a44f1d9c24004102f0bc0d6a68831b987c7aec02
|
4
|
+
data.tar.gz: c31179eaa53da893629af033d57708e2e717bf74
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45b2c5baedd05621ca90e9913ba14a9944d5eb66c6aedca8124c0629230f2a2eb4664b0a9c8eeddabeb9fac197186a24b5216c0a16330e4c2422c76fb0670fb7
|
7
|
+
data.tar.gz: 83280bee850af5d07d034edd69ecc9a292f42806e573675be4a3469c359f000e6bdb16718d1a538f0623d696c9ecf13fc87fe4207f91ae1d47cc99b6a14f7920
|
data/lib/dactyl_form.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# TODO Thread safety
|
2
|
+
# TODO Raise exceptions on invalid input
|
3
|
+
# TODO Possible normalizations infinite loop
|
4
|
+
# TODO Comments
|
5
|
+
# TODO (attribute)_change inspection and handling
|
6
|
+
# TODO Readme file
|
7
|
+
# TODO License file
|
8
|
+
|
9
|
+
class DactylForm
|
10
|
+
|
11
|
+
include ActiveModel::Validations
|
12
|
+
|
13
|
+
attr_reader :object
|
14
|
+
|
15
|
+
def initialize(object, params = nil)
|
16
|
+
@object = object
|
17
|
+
@params = params
|
18
|
+
@attribute_cache = {}
|
19
|
+
@persisted = object.persisted?
|
20
|
+
self.class.define_getters unless self.class.getters_defined?
|
21
|
+
end
|
22
|
+
|
23
|
+
def http_method
|
24
|
+
updating? ? :put : :post
|
25
|
+
end
|
26
|
+
|
27
|
+
def updating?
|
28
|
+
persisted?
|
29
|
+
end
|
30
|
+
|
31
|
+
def creating?
|
32
|
+
!updating?
|
33
|
+
end
|
34
|
+
|
35
|
+
def process
|
36
|
+
if valid?
|
37
|
+
save_object
|
38
|
+
true
|
39
|
+
else
|
40
|
+
restore_params
|
41
|
+
false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def self.object(name)
|
48
|
+
define_method(name) { @object }
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.attributes(*attributes)
|
52
|
+
@attributes ||= []
|
53
|
+
@attributes += attributes
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.define_getters
|
57
|
+
raise 'No attributes specified' if @attributes.blank?
|
58
|
+
@attributes.uniq.each { |attribute| define_getter(attribute) }
|
59
|
+
@getters_defined = true
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.define_getter(attribute)
|
63
|
+
define_method(attribute) do
|
64
|
+
@attribute_cache.fetch(attribute) do
|
65
|
+
|
66
|
+
if params_provided?
|
67
|
+
value = read_from_params(attribute)
|
68
|
+
elsif persisted?
|
69
|
+
value = read_from_object(attribute)
|
70
|
+
else
|
71
|
+
value = read_from_default(attribute)
|
72
|
+
end
|
73
|
+
@attribute_cache[attribute] = value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.getters_defined?
|
79
|
+
!!@getters_defined
|
80
|
+
end
|
81
|
+
|
82
|
+
def params_provided?
|
83
|
+
!!@params
|
84
|
+
end
|
85
|
+
|
86
|
+
def persisted?
|
87
|
+
@persisted
|
88
|
+
end
|
89
|
+
|
90
|
+
def assign_attributes
|
91
|
+
raise "Form object should implement 'assign_attributes'"
|
92
|
+
end
|
93
|
+
|
94
|
+
def read_from_object(attribute)
|
95
|
+
getter = "#{attribute}_from_object"
|
96
|
+
respond_to?(getter) ? send(getter) : @object.send(attribute)
|
97
|
+
end
|
98
|
+
|
99
|
+
def read_from_default(attribute)
|
100
|
+
getter = "#{attribute}_from_default"
|
101
|
+
respond_to?(getter) ? send(getter) : nil
|
102
|
+
end
|
103
|
+
|
104
|
+
def read_from_params(attribute)
|
105
|
+
getter = "#{attribute}_from_params"
|
106
|
+
return @params[attribute] unless respond_to?(getter)
|
107
|
+
|
108
|
+
arity = method(getter).arity
|
109
|
+
arity == 0 ? send(getter) : send(getter, @params[attribute])
|
110
|
+
end
|
111
|
+
|
112
|
+
def save_object
|
113
|
+
assign_attributes
|
114
|
+
ActiveRecord::Base.transaction do
|
115
|
+
@object.save!
|
116
|
+
updating? ? try(:after_update) : try(:after_create)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def restore_params
|
121
|
+
@attribute_cache.each_key do |attribute|
|
122
|
+
@attribute_cache[attribute] = @params[attribute]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dactyl_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Gubitskiy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
description: Form objects made easy
|
28
|
+
email: d.gubitskiy@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/dactyl_form.rb
|
34
|
+
homepage: https://github.com/enthrops/dactyl_form
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.4.2
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Form objects made easy
|
58
|
+
test_files: []
|