Class: SimpleModel::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, Association::Model
Defined in:
lib/simplemodel/base.rb,
lib/simplemodel/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Association::Model

included

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



82
83
84
85
# File 'lib/simplemodel/base.rb', line 82

def initialize(opts = {})
  @unknown_attributes = {}
  self.attributes = opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object (private)

:nodoc:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/simplemodel/base.rb', line 120

def method_missing(method_symbol, *arguments) #:nodoc:
  method_name = method_symbol.to_s
  if method_name =~ /(=|\?)$/
    case $1
    when "="
      if self.class.auto_find_relations && self.class.try_association($`)
        self.send "#{$`}=".to_sym, arguments.first
      elsif self.class.auto_define_attributes
        @attributes[$`] = arguments.first
        self.class.raw_attributes[$`.to_s.to_sym] = nil
      elsif !self.raw_attributes.has_key?($`.to_s.to_sym)
        self.attributes.delete($`)
        self.unknown_attributes[$`] = arguments.first
      else
          @attributes[$`] = arguments.first
      end
    when "?"
      @attributes.has_key?($`)
    end
  else
    return attributes[method_name] if attributes.has_key?(method_name)
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



43
44
45
# File 'lib/simplemodel/base.rb', line 43

def attributes
  @attributes
end

#unknown_attributesObject

Returns the value of attribute unknown_attributes.



43
44
45
# File 'lib/simplemodel/base.rb', line 43

def unknown_attributes
  @unknown_attributes
end

Class Method Details

.attributes(*attributes) ⇒ Object



38
39
40
# File 'lib/simplemodel/base.rb', line 38

def attributes(*attributes)
  self.known_attributes |= attributes.map(&:to_s)
end

.auto_define_attributes(bool = false) ⇒ Object



30
31
32
# File 'lib/simplemodel/base.rb', line 30

def auto_define_attributes(bool=false)
  self.auto_define_attributes = bool
end

.auto_find_relations(bool = false) ⇒ Object



34
35
36
# File 'lib/simplemodel/base.rb', line 34

def auto_find_relations(bool=false)
  self.auto_find_relations = bool
end

.define_attribute(name, opts) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/simplemodel/base.rb', line 16

def define_attribute(name, opts)
  self.raw_attributes[name] = nil
  self.known_attributes << name.to_s
  if opts.has_key?(:json_field)
    self.json_mapper[opts[:json_field]] = name.to_s
  end
end

.define_attributes(*attr) ⇒ Object



11
12
13
14
# File 'lib/simplemodel/base.rb', line 11

def define_attributes(*attr)
  attr.each{|a| self.raw_attributes[a] = nil}
  attributes *attr
end

.override_attributes(*attr) ⇒ Object



24
25
26
27
28
# File 'lib/simplemodel/base.rb', line 24

def override_attributes(*attr)
  self.raw_attributes = {}
  self.known_attributes = Set.new
  define_attributes(*attr)
end

Instance Method Details

#[](val) ⇒ Object



45
46
47
# File 'lib/simplemodel/base.rb', line 45

def [](val)
  self.attributes[val]
end

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/simplemodel/base.rb', line 87

def has_attribute?(name)
  self.attributes.has_key?(name)
end

#known_attributesObject



62
63
64
# File 'lib/simplemodel/base.rb', line 62

def known_attributes
  self.class.known_attributes | self.attributes.keys.map(&:to_s)
end

#raw_attributesObject



66
67
68
# File 'lib/simplemodel/base.rb', line 66

def raw_attributes
  self.class.raw_attributes
end

#refreshObject



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

def refresh()
  if self.class.auto_define_attributes
    self.class.raw_attributes = {}
  end
  self.attributes.each do |name, attr|
    res = self.send "#{name}=".to_sym, attr
    if not self.raw_attributes.has_key?(name.to_s.to_sym)
      self.attributes.delete(name)
    end
  end
  return self
end

#respond_to?(method, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simplemodel/base.rb', line 93

def respond_to?(method, include_priv = false)
  method_name = method.to_s
  if self.attributes.nil?
    super
  elsif known_attributes.include?(method_name)
    true
  elsif method_name =~ /(?:=|\?)$/ && self.attributes.include?($`)
    true
  else
    super
  end
end

#respond_to_without_attributes?Object



91
# File 'lib/simplemodel/base.rb', line 91

alias_method :respond_to_without_attributes?, :respond_to?