Class: Attributor::Model
- Inherits:
-
Hash
- Object
- Hash
- Attributor::Model
show all
- Defined in:
- lib/attributor/types/model.rb
Constant Summary
Constants inherited
from Hash
Hash::CIRCULAR_REFERENCE_MARKER, Hash::MAX_EXAMPLE_DEPTH
Instance Attribute Summary
Attributes inherited from Hash
#contents, #dumping, #validating
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Hash
#==, attributes, construct, constructable?, definition, dsl_class, dump, example, example_contents, from_hash, #generate_subcontext, #get, #key_attribute, #key_type, keys, load, native_type, of, #set, valid_type?, validate, #value_attribute, #value_type
Methods included from Container
included
Constructor Details
#initialize(data = nil) ⇒ Model
95
96
97
98
99
100
101
102
|
# File 'lib/attributor/types/model.rb', line 95
def initialize(data = nil)
if data
loaded = self.class.load(data)
@contents = loaded.attributes
else
@contents = {}
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/attributor/types/model.rb', line 145
def method_missing(name, *args)
attribute_name = name.to_s
attribute_name.chomp!('=')
if self.class.attributes.has_key?(attribute_name.to_sym)
self.class.define_accessors(attribute_name)
return self.__send__(name, *args)
end
super
end
|
Class Method Details
.check_option!(name, value) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/attributor/types/model.rb', line 75
def self.check_option!(name, value)
case name
when :identity
raise AttributorException, "Invalid identity type #{value.inspect}" unless value.kind_of?(::Symbol)
:ok when :reference
:ok when :dsl_compiler
:ok when :dsl_compiler_options
:ok
else
super
end
end
|
.define_accessors(name) ⇒ Object
Define accessors for attribute of given name.
45
46
47
48
49
|
# File 'lib/attributor/types/model.rb', line 45
def self.define_accessors(name)
name = name.to_sym
self.define_reader(name)
self.define_writer(name)
end
|
.define_reader(name) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/attributor/types/model.rb', line 51
def self.define_reader(name)
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{name}
@contents[:#{name}]
end
RUBY
end
|
.define_writer(name) ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/attributor/types/model.rb', line 60
def self.define_writer(name)
context = ["assignment","of(#{name})"].freeze
module_eval do
define_method(name.to_s + "=") do |value|
self.set(name, value, context: context)
end
end
end
|
.describe(shallow = false) ⇒ Object
69
70
71
72
73
|
# File 'lib/attributor/types/model.rb', line 69
def self.describe(shallow=false)
hash = super
hash[:attributes] = hash.delete :keys
hash
end
|
.generate_subcontext(context, subname) ⇒ Object
91
92
93
|
# File 'lib/attributor/types/model.rb', line 91
def self.generate_subcontext(context, subname)
context + [subname]
end
|
.inherited(klass) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/attributor/types/model.rb', line 22
def self.inherited(klass)
k = self.key_type
ka = self.key_attribute
v = self.value_type
va = self.value_attribute
klass.instance_eval do
@saved_blocks = []
@options = {}
@keys = {}
@key_type = k
@value_type = v
@key_attribute = ka
@value_attribute = va
end
end
|
Instance Method Details
#attributes ⇒ Object
130
131
132
|
# File 'lib/attributor/types/model.rb', line 130
def attributes
@contents
end
|
#dump(context: Attributor::DEFAULT_ROOT_CONTEXT, **opts) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/attributor/types/model.rb', line 158
def dump(context: Attributor::DEFAULT_ROOT_CONTEXT, **opts)
return CIRCULAR_REFERENCE_MARKER if @dumping
@dumping = true
self.attributes.each_with_object({}) do |(name, value), result|
attribute = self.class.attributes[name]
result[name.to_sym] = attribute.dump(value, context: context + [name] )
end
ensure
@dumping = false
end
|
#respond_to_missing?(name) ⇒ Boolean
135
136
137
138
139
140
141
142
|
# File 'lib/attributor/types/model.rb', line 135
def respond_to_missing?(name,*)
attribute_name = name.to_s
attribute_name.chomp!('=')
return true if self.class.attributes.key?(attribute_name.to_sym)
super
end
|
#validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
TODO: memoize validation results here, but only after rejiggering how we store the context.
Two calls to validate() with different contexts should return get the same errors,
but with their respective contexts.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/attributor/types/model.rb', line 108
def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)
raise AttributorException, "validation conflict" if @validating
@validating = true
context = [context] if context.is_a? ::String
self.class.attributes.each_with_object(Array.new) do |(sub_attribute_name, sub_attribute), errors|
sub_context = self.class.generate_subcontext(context,sub_attribute_name)
value = self.__send__(sub_attribute_name)
if value.respond_to?(:validating) next if value.validating
end
errors.push *sub_attribute.validate(value, sub_context)
end
ensure
@validating = false
end
|