objective_form 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,110 @@
|
|
1
|
+
require "objective_form/attribute"
|
2
|
+
require "objective_form/pseudo_record"
|
3
|
+
require "objective_form/pseudo_relation"
|
4
|
+
|
5
|
+
class ObjectiveForm::Base
|
6
|
+
extend ActiveModel::Naming
|
7
|
+
include ActiveModel::Conversion
|
8
|
+
include ActiveModel::Validations
|
9
|
+
class_attribute :pseudo_relations, :attributes
|
10
|
+
|
11
|
+
def initialize(attributes = {})
|
12
|
+
attributes.each do |name, value|
|
13
|
+
send("#{name}=", value)
|
14
|
+
end
|
15
|
+
|
16
|
+
pseudo_relations.each do |rel|
|
17
|
+
instance_variable_set("@#{rel.name}", []) unless send(rel.name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def persisted?
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
def save
|
26
|
+
if valid?
|
27
|
+
persist!
|
28
|
+
true
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def persist!
|
35
|
+
raise NotImplementedError
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def initialize_attribute_value(klass_or_proc, value)
|
41
|
+
return value.to_s if klass_or_proc == String
|
42
|
+
|
43
|
+
if klass_or_proc == Array
|
44
|
+
return [] if value.blank?
|
45
|
+
return Array.wrap(value)
|
46
|
+
end
|
47
|
+
|
48
|
+
if klass_or_proc.respond_to?(:new)
|
49
|
+
klass_or_proc.new(value)
|
50
|
+
elsif klass_or_proc.respond_to?(:call)
|
51
|
+
klass_or_proc.call(value)
|
52
|
+
else
|
53
|
+
value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class << self
|
58
|
+
public
|
59
|
+
|
60
|
+
def has_many(name, pseudo_record)
|
61
|
+
store_relation(name, pseudo_record)
|
62
|
+
define_relation_accessors(name, pseudo_record)
|
63
|
+
end
|
64
|
+
|
65
|
+
def attribute(name, klass_or_proc = String)
|
66
|
+
store_attribute(name, klass_or_proc)
|
67
|
+
define_attribute_accessors(name, klass_or_proc)
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def store_attribute(name, klass_or_proc)
|
73
|
+
self.attributes ||= []
|
74
|
+
self.attributes += Array.wrap(ObjectiveForm::Attribute.new(name, klass_or_proc))
|
75
|
+
end
|
76
|
+
|
77
|
+
def store_relation(name, pseudo_record)
|
78
|
+
self.pseudo_relations ||= []
|
79
|
+
self.pseudo_relations += Array.wrap(ObjectiveForm::PseudoRelation.new(name, pseudo_record))
|
80
|
+
end
|
81
|
+
|
82
|
+
def define_attribute_accessors(name, klass_or_proc)
|
83
|
+
attr_reader name
|
84
|
+
|
85
|
+
define_method("#{name}=") do |value|
|
86
|
+
instance_variable_set("@#{name}", initialize_attribute_value(klass_or_proc, value))
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def define_relation_accessors(name, pseudo_record)
|
91
|
+
attr_reader name
|
92
|
+
|
93
|
+
define_method("#{name}=") do |values|
|
94
|
+
Array.wrap(values).each do |value|
|
95
|
+
pseudo_record_instance = pseudo_record.new(value)
|
96
|
+
instance_variable_set("@#{name}", instance_variable_get("@#{name}") + [pseudo_record_instance])
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
define_method("#{name}_attributes=") do |values|
|
101
|
+
instance_variable_set("@#{name}", []) unless instance_variable_defined?("@#{name}")
|
102
|
+
values.each do |_, attributes|
|
103
|
+
next if attributes['_destroy'] and ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(attributes['_destroy'])
|
104
|
+
pseudo_record_instance = pseudo_record.new(*attributes.except('_destroy').values)
|
105
|
+
instance_variable_set("@#{name}", instance_variable_get("@#{name}") + [pseudo_record_instance])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class ObjectiveForm::PseudoRecord < Struct
|
2
|
+
class_attribute :_fields
|
3
|
+
attr_writer :_destroy
|
4
|
+
|
5
|
+
def self.new(*args)
|
6
|
+
fields = args.extract_options!
|
7
|
+
super(*fields.keys).tap do |klass|
|
8
|
+
klass._fields = fields
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def persisted?
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
def _destroy
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "objective_form/base"
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: objective_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- stevo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-07 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Objective form is for whoever miss dynamic nested fields adding in form
|
15
|
+
object using nested_form gem by Ryan Bates. Beside that it is lightweight abstraction
|
16
|
+
of form objects for Rails.
|
17
|
+
email:
|
18
|
+
- blazejek@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- lib/objective_form.rb
|
24
|
+
- lib/objective_form/attribute.rb
|
25
|
+
- lib/objective_form/base.rb
|
26
|
+
- lib/objective_form/pseudo_record.rb
|
27
|
+
- lib/objective_form/pseudo_relation.rb
|
28
|
+
- lib/objective_form/version.rb
|
29
|
+
homepage: https://github.com//objective_form
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project: ! '[none]'
|
49
|
+
rubygems_version: 1.8.24
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Form objects with nested_form support
|
53
|
+
test_files: []
|