Class: Cornerstore::Resource::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cornerstore/resource/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, ary = nil, name = nil) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
# File 'lib/cornerstore/resource/base.rb', line 5

def initialize(parent = nil, ary = nil, name = nil)
  @klass = Cornerstore.const_get(self.class.name.split('::')[-2])
  @name = name
  @parent = parent
  @filters = Hash.new
  @objects = Array.new
  from_array(ary) if ary.is_a? Array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



76
77
78
79
80
81
82
# File 'lib/cornerstore/resource/base.rb', line 76

def method_missing(method, *args, &block)
  if Cornerstore::Resource::Writable.method_defined?(method)
    raise "Sorry, this part of the Cornerstore-API is currently read-only"
  else
    super
  end
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/cornerstore/resource/base.rb', line 3

def parent
  @parent
end

Instance Method Details

#allObject



44
45
46
# File 'lib/cornerstore/resource/base.rb', line 44

def all
  self.clone
end

#each(&block) ⇒ Object



60
61
62
# File 'lib/cornerstore/resource/base.rb', line 60

def each(&block)
  to_a.each(&block)
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


70
71
72
# File 'lib/cornerstore/resource/base.rb', line 70

def empty?
  @objects.empty?
end

#find(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cornerstore/resource/base.rb', line 19

def find(*args)
  ids = Array.new(args).flatten.compact.uniq
  case ids.size
    when 0
      raise "Couldn't find #{@klass.name} without an ID"
    when 1
      find_by_id_or_param(ids.first)
    else
      find_by_ids(ids)
  end
end

#find_by_id(id) ⇒ Object



35
36
37
# File 'lib/cornerstore/resource/base.rb', line 35

def find_by_id(id)
  @objects.find{|obj| obj._id == id}
end

#find_by_id_or_param(id_or_param) ⇒ Object



31
32
33
# File 'lib/cornerstore/resource/base.rb', line 31

def find_by_id_or_param(id_or_param)
  find_by_id id_or_param.to_s
end

#find_by_ids(*args) ⇒ Object



39
40
41
42
# File 'lib/cornerstore/resource/base.rb', line 39

def find_by_ids(*args)
  ids = Array.new(args).flatten.compact.uniq
  all.select{|item| ids.include? item._id }
end

#from_array(ary) ⇒ Object



14
15
16
17
# File 'lib/cornerstore/resource/base.rb', line 14

def from_array(ary)
  @objects = ary.map{|h| @klass.new(h, @parent)}
  @load = true
end

#push(obj) ⇒ Object Also known as: <<



48
49
50
51
52
# File 'lib/cornerstore/resource/base.rb', line 48

def push(obj)
  @objects << obj
  obj.parent = @parent
  self
end

#sizeObject Also known as: count, length



64
65
66
# File 'lib/cornerstore/resource/base.rb', line 64

def size
  @objects.size
end

#to_aObject Also known as: to_ary



55
56
57
# File 'lib/cornerstore/resource/base.rb', line 55

def to_a
  @objects
end