class FlattenRecord::Definition
Public Class Methods
new(definition, key=nil)
click to toggle source
# File lib/flatten_record/definition.rb, line 3 def initialize(definition, key=nil) @definition = definition @errors = [] @methods = {} @compute = {} @include = {} @except = [] @only = [] @class_name = nil @prefix = nil @_key = key end
Public Instance Methods
[](key)
click to toggle source
# File lib/flatten_record/definition.rb, line 16 def [](key) instance_variable_get "@#{key}" end
error_message()
click to toggle source
# File lib/flatten_record/definition.rb, line 38 def error_message @errors.join("\n") end
valid?()
click to toggle source
# File lib/flatten_record/definition.rb, line 42 def valid? @errors.blank? end
validates(key, value)
click to toggle source
# File lib/flatten_record/definition.rb, line 29 def validates(key, value) if protected_methods.include?("validate_#{key}".to_sym) send("validate_#{key}", value) else @errors << "unknown options '#{key}'" end self end
validates_with(target_model, model)
click to toggle source
# File lib/flatten_record/definition.rb, line 20 def validates_with(target_model, model) @target_model = target_model @model = model @definition.each do |key, value| validates(key, value) end end
Protected Instance Methods
validate_class_name(name)
click to toggle source
# File lib/flatten_record/definition.rb, line 76 def validate_class_name(name) error = "undefined class with '#{name}'" @errors << error if !Object.const_defined?(name.to_sym) @class_name = name end
validate_compute(model_methods)
click to toggle source
# File lib/flatten_record/definition.rb, line 72 def validate_compute(model_methods) @compute = model_methods end
validate_except(attrs)
click to toggle source
# File lib/flatten_record/definition.rb, line 47 def validate_except(attrs) validate_attrs(:except, attrs) end
validate_include(childs)
click to toggle source
# File lib/flatten_record/definition.rb, line 63 def validate_include(childs) childs.each do |child, child_definition| error = "unknown association '#{child}' in '#{@target_model.name.to_s}'" assoc = @target_model.reflect_on_association(child) @errors << error if assoc.blank? @include[child] = Definition.new(child_definition, child) end end
validate_methods(methods)
click to toggle source
# File lib/flatten_record/definition.rb, line 55 def validate_methods(methods) methods.each do |method, type| error = "undefined method '#{method}' in #{@target_model.name}" @errors << error unless target_method?(method) @methods[method] = type end end
validate_only(attrs)
click to toggle source
# File lib/flatten_record/definition.rb, line 51 def validate_only(attrs) validate_attrs(:only, attrs) end
validate_prefix(name)
click to toggle source
# File lib/flatten_record/definition.rb, line 82 def validate_prefix(name) @prefix = name end
Private Instance Methods
model_method?(method)
click to toggle source
# File lib/flatten_record/definition.rb, line 100 def model_method?(method) @model.attribute_method?(method) || @model.method_defined?(method) end
target_method?(method)
click to toggle source
# File lib/flatten_record/definition.rb, line 105 def target_method?(method) @target_model.attribute_method?(method) || @target_model.method_defined?(method) end
validate_attrs(name, attrs)
click to toggle source
# File lib/flatten_record/definition.rb, line 87 def validate_attrs(name, attrs) attrs.each do |attr| error = "unknown attribute '#{attr}' in #{@target_model.name.to_s}" @errors << error unless target_method?(attr) case name when :only @only << attr when :except @except << attr end end end