Class: Formed::Base
- Inherits:
-
Object
show all
- Extended by:
- Relation::Delegation::DelegateCache
- Includes:
- ActiveModel::AttributeAssignment, ActiveModel::AttributeMethods, ActiveModel::Attributes, ActiveModel::Callbacks, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Validations, ActsLikeModel, Associations, Attributes, Core, FromModel, FromParams, Inheritance, NestedAttributes, Reflection
- Defined in:
- lib/formed/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
generate_relation_method, inherited, initialize_relation_delegate_cache, relation_delegate_class
Methods included from FromModel
#from_model
Methods included from FromParams
#from_params
#map_model
#_destroy, #_ensure_no_duplicate_errors, #associated_records_to_validate, #association_valid?, #normalize_reflection_attribute, #validate_collection_association, #validate_single_association
Methods included from Reflection
add_aggregate_reflection, add_reflection, create
#initialize_dup
Methods included from Attributes
#_has_attribute?, #attribute_present?, #attributes_with_values, #column_for_attribute, #has_attribute?, #type_for_attribute
#association, #association_cached?, #initialize_dup
Constructor Details
#initialize(attributes = nil) {|_self| ... } ⇒ Base
Returns a new instance of Base.
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/formed/base.rb', line 74
def initialize(attributes = nil)
@new_record = true
@attributes = self.class._default_attributes.deep_dup
init_internals
initialize_internals_callback
assign_attributes(attributes) if attributes
yield self if block_given?
_run_initialize_callbacks
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
112
113
114
|
# File 'lib/formed/base.rb', line 112
def context
@context
end
|
Class Method Details
.from_json(json) ⇒ Object
132
133
134
135
|
# File 'lib/formed/base.rb', line 132
def self.from_json(json)
params = JSON.parse(json)
from_params(params)
end
|
.model_name ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/formed/base.rb', line 155
def self.model_name
if model.is_a?(Symbol)
ActiveModel::Name.new(self, nil, model.to_s)
elsif model.present?
ActiveModel::Name.new(self, nil, model.model_name.name.split("::").last)
else
name = self.name.demodulize.delete_suffix("Form")
name = self.name if name.blank?
ActiveModel::Name.new(self, nil, name)
end
end
|
Instance Method Details
#destroy? ⇒ Boolean
179
180
181
|
# File 'lib/formed/base.rb', line 179
def destroy?
_destroy
end
|
#init_internals ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/formed/base.rb', line 61
def init_internals
@marked_for_destruction = false
@association_cache = {}
klass = self.class
@strict_loading = false
@strict_loading_mode = :all
klass.define_attribute_methods
end
|
#initialize_internals_callback ⇒ Object
72
|
# File 'lib/formed/base.rb', line 72
def initialize_internals_callback; end
|
#inspect ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/formed/base.rb', line 114
def inspect
inspection = if defined?(@attributes) && @attributes
self.class.attribute_names.filter_map do |name|
"#{name}: #{_read_attribute(name).inspect}" if self.class.attribute_types.key?(name)
end.join(", ")
else
"not initialized"
end
"#<#{self.class} #{inspection}>"
end
|
#invalid?(options = {}) ⇒ Boolean
147
148
149
|
# File 'lib/formed/base.rb', line 147
def invalid?(options = {})
!valid?(options)
end
|
#marked_for_destruction? ⇒ Boolean
175
176
177
|
# File 'lib/formed/base.rb', line 175
def marked_for_destruction?
attributes["_destroy"]
end
|
#model_name ⇒ Object
167
168
169
|
# File 'lib/formed/base.rb', line 167
def model_name
self.class.model_name
end
|
#new_record? ⇒ Boolean
171
172
173
|
# File 'lib/formed/base.rb', line 171
def new_record?
!persisted?
end
|
#persisted? ⇒ Boolean
128
129
130
|
# File 'lib/formed/base.rb', line 128
def persisted?
id.present? && id.to_i.positive?
end
|
#to_key ⇒ Object
151
152
153
|
# File 'lib/formed/base.rb', line 151
def to_key
[id]
end
|
#valid?(options = {}) ⇒ Boolean
137
138
139
140
141
142
143
144
145
|
# File 'lib/formed/base.rb', line 137
def valid?(options = {})
run_callbacks(:validation) do
options = {} if options.blank?
context = options[:context]
validations = [super(context)]
validations.all?
end
end
|
#with_context(contexts = {}) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/formed/base.rb', line 101
def with_context(contexts = {})
@context = OpenStruct.new(contexts)
_reflections.each do |_, reflection|
if (instance = public_send(reflection.name))
instance.with_context(@context)
end
end
self
end
|