Class: RemoteResource::Model::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_resource/model/relation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, attributes = {}) ⇒ Relation

Returns a new instance of Relation.



6
7
8
9
# File 'lib/remote_resource/model/relation.rb', line 6

def initialize(model_class, attributes={})
  @model_class    = model_class
  self.attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/remote_resource/model/relation.rb', line 11

def method_missing(meth, *args, &block)
  if model_class.delegated_to_relation.try(:include?, meth)
    if args.count > 0
      arg = if model_class.delegated_to_relation_merged.try(:include?, meth)
              (attributes[meth] || {}).merge(args.first)
            else
              args.first
            end
      self.class.for_model(model_class, attributes.merge(meth => arg))
    else
      attributes[meth]
    end

  elsif model_class.delegated_from_relation.try(:include?, meth)
    options = args.extract_options!
    model_class.send(meth, *args, options.merge(attributes), &block)
  else
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/remote_resource/model/relation.rb', line 4

def attributes
  @attributes
end

#model_classObject (readonly)

Returns the value of attribute model_class.



3
4
5
# File 'lib/remote_resource/model/relation.rb', line 3

def model_class
  @model_class
end

Class Method Details

.for_model(model, attributes = {}) ⇒ Object



93
94
95
96
97
# File 'lib/remote_resource/model/relation.rb', line 93

def self.for_model(model, attributes={})
  rel = RemoteResource::Model::Relation.new(model, attributes)
  rel.send(:extend, "#{model.name}::RelationMethods".constantize) if model.const_defined?(:RelationMethods)
  rel
end

Instance Method Details

#all(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/remote_resource/model/relation.rb', line 36

def all(options={})
  raise '`path` for query not specified' unless attributes[:path]
  results = model_class.connection.request(
      attributes[:path],
      separate:    attributes[:separate],
      http_method: attributes[:via],
      query:       attributes[:query],
      body:        attributes[:body],
      cookies:     attributes[:cookies],
      headers:     attributes[:headers],
      unwrap:      attributes[:unwrap],
  )
  results.take(attributes[:limit] || results.count).map do |result_row|
    build(result_row, options)
  end
end

#build(doc, options = {}, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/remote_resource/model/relation.rb', line 59

def build(doc, options={}, &block)
  model    = model_class.new(url: attributes[:path])
  document = RemoteResource::DocumentWrapper.new(doc)

  mapping_name = options[:mapping] || :default
  mapping  = model_class.mappings[mapping_name]
  raise "Mapping `#{mapping_name}` not found" unless mapping

  model.instance_exec(document, &mapping)
  instance_exec(model, &block) if block_given?
  model
end

#first(options = {}) ⇒ Object Also known as: find



53
54
55
# File 'lib/remote_resource/model/relation.rb', line 53

def first(options={})
  limit(1).all(options).first
end

#on_all_pages(all_options = {}, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/remote_resource/model/relation.rb', line 72

def on_all_pages(all_options={}, &block)
  all_entities = []
  begin
    page            = (page || -1) + 1
    remaining_limit = attributes[:limit] ? attributes[:limit] - all_entities.count : nil
    entities        = block_given? ? instance_exec(page, &block) : limit(remaining_limit).page(page).all(all_options)
    all_entities    += entities if entities.any?
  end while entities.any?
  all_entities.compact
end

#on_pages(urls, all_options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/remote_resource/model/relation.rb', line 83

def on_pages(urls, all_options={})
  on_all_pages do |n|
    if urls[n]
      path(urls[n]).all(all_options)
    else
      []
    end
  end
end

#relationObject



32
33
34
# File 'lib/remote_resource/model/relation.rb', line 32

def relation
  self
end