nform 1.0.2 → 1.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 +4 -4
- data/lib/nform/attributes.rb +28 -15
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 650de4de056c13e0ad264142166a65fdad7d92a6
|
4
|
+
data.tar.gz: e6768bf06dc70032b5e630809f93e10330e1c1c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4eba41db6bec1c34ba333032f91533685384893be4ba8c9357a1e34bd480879d91affb7c58a03613f620fe973c81680304919da9a04eb3b0f9f27a52083199cf
|
7
|
+
data.tar.gz: e2b51d4902daab7e3ce23b930c5e62dbfce89ed0358c09ef1159973e303a3d8d619e782248d5bc296656d6bf25db65ad14db0a47854a755f3663dc285bcb2d87
|
data/lib/nform/attributes.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_support/core_ext/hash'
|
2
2
|
require 'nform/coercions'
|
3
|
+
require 'set'
|
3
4
|
|
4
5
|
module NForm
|
5
6
|
module Attributes
|
@@ -18,10 +19,21 @@ module NForm
|
|
18
19
|
@undef_attr = option
|
19
20
|
end
|
20
21
|
|
22
|
+
def hash_representation(option)
|
23
|
+
unless %i|partial complete|.include?(option)
|
24
|
+
raise ArgumentError, "Unknown option `#{option}` for hash representation. Options are :partial or :complete"
|
25
|
+
end
|
26
|
+
@hash_rep = option
|
27
|
+
end
|
28
|
+
|
21
29
|
def __undef_attr
|
22
30
|
@undef_attr ||= :raise
|
23
31
|
end
|
24
32
|
|
33
|
+
def __hash_rep
|
34
|
+
@hash_rep ||= :complete
|
35
|
+
end
|
36
|
+
|
25
37
|
def define_attributes
|
26
38
|
attribute_set.each do |name,options|
|
27
39
|
define_method(name) do
|
@@ -31,6 +43,7 @@ module NForm
|
|
31
43
|
# TODO: must use coercion set
|
32
44
|
c = get_coercion(options[:coerce])
|
33
45
|
define_method("#{name}=") do |input|
|
46
|
+
@__touched_keys << name
|
34
47
|
instance_variable_set("@#{name}", c.call(input,self))
|
35
48
|
end
|
36
49
|
end
|
@@ -60,43 +73,43 @@ module NForm
|
|
60
73
|
|
61
74
|
module InstanceMethods
|
62
75
|
def initialize(input={})
|
76
|
+
@__touched_keys = Set.new
|
63
77
|
i = input.symbolize_keys
|
64
78
|
require_attributes!(i)
|
65
79
|
self.class.define_attributes
|
66
80
|
set_attributes!(i)
|
67
|
-
set_missing_defaults
|
68
81
|
end
|
69
82
|
|
70
83
|
def to_hash
|
71
|
-
self.class.
|
84
|
+
if self.class.__hash_rep == :partial
|
85
|
+
@__touched_keys
|
86
|
+
else
|
87
|
+
self.class.attribute_set.keys
|
88
|
+
end.each.with_object({}) do |k,memo|
|
72
89
|
memo[k] = send(k)
|
73
90
|
end
|
74
91
|
end
|
75
92
|
|
76
93
|
private
|
77
94
|
def require_attributes!(input_hash)
|
95
|
+
# Check for missing required attributes
|
78
96
|
required = self.class.attribute_set.map{|name,options| name if options[:required]}.compact
|
79
97
|
missing = (required - input_hash.keys)
|
80
98
|
if missing.any?
|
81
99
|
raise ArgumentError, "Missing required attributes: #{missing.inspect}"
|
82
100
|
end
|
83
|
-
end
|
84
101
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
90
|
-
send "#{k}=", v if respond_to?("#{k}=")
|
102
|
+
# Check for unallowed extra attributes
|
103
|
+
if self.class.__undef_attr == :raise
|
104
|
+
extra = (input_hash.keys - self.class.attribute_set.keys)
|
105
|
+
raise ArgumentError, "Undefined attribute(s): #{extra.join(',')}" if extra.any?
|
91
106
|
end
|
92
107
|
end
|
93
108
|
|
94
|
-
def
|
95
|
-
self.class.attribute_set.each do |
|
96
|
-
|
97
|
-
|
98
|
-
send("#{name}=",default) if send(name).nil?
|
99
|
-
end
|
109
|
+
def set_attributes!(input_hash)
|
110
|
+
self.class.attribute_set.each do |a, opts|
|
111
|
+
val = input_hash[a] || opts[:default]
|
112
|
+
send "#{a}=", val if input_hash.has_key?(a) || !opts[:default].nil?
|
100
113
|
end
|
101
114
|
end
|
102
115
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Burleson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
61
|
version: '0'
|
62
62
|
requirements: []
|
63
63
|
rubyforge_project:
|
64
|
-
rubygems_version: 2.
|
64
|
+
rubygems_version: 2.5.1
|
65
65
|
signing_key:
|
66
66
|
specification_version: 4
|
67
67
|
summary: A form / service / view object library.
|