Class: Teachstreet::Result

Inherits:
Object
  • Object
show all
Includes:
Memoizable
Defined in:
lib/teachstreet.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Result

Returns a new instance of Result.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/teachstreet.rb', line 52

def initialize(attrs = {})
  @attrs = attrs
  (class << self; self; end).class_eval do
    attrs.keys.each do |key|
      if (!method_defined?(key) || key.to_s == 'id')
        define_method key.to_sym do
          @attrs[key]
        end
      end # end if
    end # end each
  end # end class_eval
end

Class Method Details

.has_many(attr_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/teachstreet.rb', line 39

def self.has_many(attr_name)
  attr_sym = attr_name.to_sym
  attr_string = attr_name.to_s
  define_method attr_sym do
    if @attrs.key?(attr_string)
      @attrs[attr_string].map do |a|
        Teachstreet.const_get(attr_string.gsub(/s$/i, '').camelcase).new(a)
      end
    end
  end
  memoize attr_sym
end

.has_one(attr_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/teachstreet.rb', line 28

def self.has_one(attr_name)
  attr_sym = attr_name.to_sym
  attr_string = attr_name.to_s
  define_method attr_sym do
    if @attrs.key?(attr_string)
      Teachstreet.const_get(attr_string.camelcase).new(@attrs[attr_string])
    end
  end 
  memoize attr_sym
end