Class: Formed::Base

Inherits:
Object
  • 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

Methods included from Relation::Delegation::DelegateCache

generate_relation_method, inherited, initialize_relation_delegate_cache, relation_delegate_class

Methods included from FromModel

#from_model

Methods included from FromParams

#from_params

Methods included from ActsLikeModel

#map_model

Methods included from NestedAttributes

#_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

Methods included from Inheritance

#initialize_dup

Methods included from Attributes

#_has_attribute?, #attribute_present?, #attributes_with_values, #column_for_attribute, #has_attribute?, #type_for_attribute

Methods included from Associations

#association, #association_cached?, #initialize_dup

Constructor Details

#initialize(attributes = nil) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:

  • _self (Formed::Base)

    the object that the method was called on



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

#contextObject (readonly)

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_nameObject



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

Returns:

  • (Boolean)


179
180
181
# File 'lib/formed/base.rb', line 179

def destroy?
  _destroy
end

#init_internalsObject



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_callbackObject



72
# File 'lib/formed/base.rb', line 72

def initialize_internals_callback; end

#inspectObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/formed/base.rb', line 114

def inspect
  # We check defined?(@attributes) not to issue warnings if the object is
  # allocated but not initialized.
  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

Returns:

  • (Boolean)


147
148
149
# File 'lib/formed/base.rb', line 147

def invalid?(options = {})
  !valid?(options)
end

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/formed/base.rb', line 175

def marked_for_destruction?
  attributes["_destroy"]
end

#model_nameObject



167
168
169
# File 'lib/formed/base.rb', line 167

def model_name
  self.class.model_name
end

#new_record?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/formed/base.rb', line 171

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/formed/base.rb', line 128

def persisted?
  id.present? && id.to_i.positive?
end

#to_keyObject



151
152
153
# File 'lib/formed/base.rb', line 151

def to_key
  [id]
end

#valid?(options = {}) ⇒ Boolean

Returns:

  • (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