Module: Vmodel::InstanceMethods

Defined in:
lib/vmodel.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *argv, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vmodel.rb', line 81

def method_missing(name, *argv, &block)
  attr = name.to_s.gsub("=","")
  if name.to_s.end_with?("=")
    if @_attributes.include?(attr)
      @_attributes[attr] = argv[0]
      return argv[0]
    elsif self.class.wild_mod
      return @_attributes[attr] = argv[0]
    end
  else
    if @_attributes.include?(attr)
      return @_attributes[attr]
    end
    return self.class.wild_mod ? nil : super
  end
  super
end

Instance Method Details

#attributesObject



77
78
79
# File 'lib/vmodel.rb', line 77

def attributes
  @_attributes
end

#errorsObject



72
73
74
75
# File 'lib/vmodel.rb', line 72

def errors
  validate!
  @_errors
end

#initialize(attributes = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vmodel.rb', line 47

def initialize(attributes = {})
  @_attributes = {}
  @_errors = []

  if self.class.wild_mod
    attributes.each do |k,v|
      @_attributes[k.to_s] = v
    end
  else
    self.class.allow_attributes.each do |item|
      @_attributes[item] = attributes[item] || attributes[item.to_sym]
    end
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
# File 'lib/vmodel.rb', line 99

def respond_to?(method)
  if wild_mod
    return true
  end
  attr = method.to_s.gsub("=","")
  @_attributes.include?(attr)
end

#valid?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/vmodel.rb', line 68

def valid?
  errors.size == 0
end

#validate!Object



62
63
64
65
66
# File 'lib/vmodel.rb', line 62

def validate!
  self.class.validations.each do |k,v|
    validate(k, @_attributes[k], v)
  end
end