Module: FormModel::ClassMethods

Defined in:
lib/form_model/model.rb,
lib/form_model/model.rb

Instance Method Summary collapse

Instance Method Details

#after_read(&block) ⇒ Object



117
118
119
# File 'lib/form_model/model.rb', line 117

def after_read &block
  self.after_read_block = block
end

#before_write(&block) ⇒ Object



121
122
123
# File 'lib/form_model/model.rb', line 121

def before_write &block
  self.before_write_block = block
end

#bind_to(&block) ⇒ Object



109
110
111
# File 'lib/form_model/model.rb', line 109

def bind_to(&block)
  self.bound_block = block
end

#bound_classObject



125
126
127
# File 'lib/form_model/model.rb', line 125

def bound_class
  @bound_class ||= self.bound_block.call
end

#form_attributesObject



129
130
131
# File 'lib/form_model/model.rb', line 129

def form_attributes
  self.attribute_set.map{|attr| attr.name.to_s}
end

#load(data_model) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/form_model/model.rb', line 133

def load(data_model)
  data_model_attributes = data_model.attributes
  attrs = data_model_attributes.slice(*form_attributes)
  self.new(data_model, attrs).tap do |instance|
    instance.instance_exec(&after_read_block) unless after_read_block.nil?
  end
end

#mapper(mapper_class, options = {}) ⇒ Object



113
114
115
# File 'lib/form_model/model.rb', line 113

def mapper(mapper_class, options = {})
  mappers << mapper_class.new(options)
end

#validates_associated(*args) ⇒ Object



37
38
39
# File 'lib/form_model/model.rb', line 37

def validates_associated(*args)
  validates_with(FormModel::AssociatedValidator, _merge_attributes(args))
end