Module: Githu3::Relations

Included in:
Resource
Defined in:
lib/githu3/relations.rb

Instance Method Summary collapse

Instance Method Details

#embeds_many(m, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/githu3/relations.rb', line 14

def embeds_many m, opts={}
  opts[:class_name] ||= m.to_s.singularize
  define_method(m) do 
    klass = Githu3.const_get(opts[:class_name].to_s.camelize)
    data = self._attributes.send(m)
    data.map { |o| klass.new(o, @client) } unless data.nil?
  end
end

#embeds_one(m, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/githu3/relations.rb', line 5

def embeds_one m, opts={}
  opts[:class_name] ||= m.to_s
  define_method(m) do
    klass = Githu3.const_get(opts[:class_name].to_s.camelize)
    data = self._attributes.send(m)
    klass.new(data, @client) unless data.nil?
  end
end

#has_many(m, opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/githu3/relations.rb', line 24

def has_many m, opts={}
  opts[:class_name] ||= m.to_s.singularize
  define_method(m) do |*args|
    params = args.extract_options!
    klass = Githu3.const_get(opts[:class_name].to_s.camelize)
    _resource_path = [opts[:nested_in], m].compact.join("/")
    if args.length == 1
      klass.new([_path, _resource_path, args.first].join("/"), @client)
    else
      Githu3::ResourceCollection.new(@client, klass, [_path, _resource_path].join("/"), params)
    end
  end
end