Module: SimpleModel::Association::ClassMethods

Defined in:
lib/simplemodel/association.rb

Instance Method Summary collapse

Instance Method Details

#associationsObject



7
8
9
# File 'lib/simplemodel/association.rb', line 7

def associations
  @associations ||= {}
end

#many(to_model, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/simplemodel/association.rb', line 64

def many(to_model, options = {})
  to_model    = to_model.to_s
  class_name  = options[:class_name]  || to_model.classify
  name = to_model
  self.associations[to_model.to_s.to_sym] = {:class_name => class_name, :name => to_model}

  class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    attr_reader :#{to_model}

    def #{to_model}
      @#{to_model} ||= TypedArray.new(#{class_name})
    end#

    def clear_#{to_model}
      @#{to_model} = nil
    end

    def #{to_model}=(array)
      @#{to_model} = TypedArray.new(#{class_name})
      array.each do |elt|
        if elt.kind_of?(Hash)
          instance = #{class_name}.new(elt)
        else
          instance = elt
        end
        self.#{to_model} << instance
      end
      return self.#{to_model}
    end

    def add_#{to_model}(*args)
      @#{to_model} = TypedArray.new(#{class_name})
      args.each do |attr|
          instance = #{class_name}.new(attr)
          self.#{to_model} << instance
      end
    end
  EOS
end

#one(to_model, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simplemodel/association.rb', line 35

def one(to_model, options = {})
  to_model    = to_model.to_s
  class_name  = options[:class_name]  || to_model.classify
  self.associations[to_model.to_s.to_sym] = {:class_name => class_name, :name => to_model}
  class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    attr_reader :#{to_model}

    def #{to_model}
      @#{to_model}
    end#

    def clear_#{to_model}
      @#{to_model} = nil
    end

    def #{to_model}=(elt)
      if elt.kind_of?(Hash)
        instance = #{class_name}.new(elt)
      elsif elt.instance_of?(#{class_name}) || elt == nil
        instance = elt
      else
        raise TypeError, "Expected Hash of attributes or #{class_name.name}"
      end
      @#{to_model} = instance
    end

  EOS
end

#singular?(str) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/simplemodel/association.rb', line 10

def singular?(str)
  str.pluralize != str && str.singularize == str
end

#try_association(key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simplemodel/association.rb', line 14

def try_association(key)
  modul = self.name.deconstantize.constantize
  begin
    model_klass = modul.const_get(key.singularize.classify)
    if model_klass <= SimpleModel::Base
      opt = {:class_name => model_klass}
      if singular?(key.to_s)
        self.one(key, opt)
      else
        self.many(key, opt)
      end
      return true
    else
      return false
    end
  rescue => e
    return false
  end
  return false
end