Class: Rubill::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rubill/base.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/rubill/base.rb', line 7

def initialize(remote)
  self.remote_record = remote
end

Instance Attribute Details

#remote_recordObject

Returns the value of attribute remote_record.



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

def remote_record
  @remote_record
end

Class Method Details

.activeObject



19
20
21
# File 'lib/rubill/base.rb', line 19

def self.active
  where([Query::Filter.new("isActive", "=", "1")])
end

.allObject



85
86
87
88
# File 'lib/rubill/base.rb', line 85

def self.all
  # Note: this method returns ALL of desired entity, including inactive
  where([])
end

.create(data) ⇒ Object



39
40
41
# File 'lib/rubill/base.rb', line 39

def self.create(data)
  new(Query.create(remote_class_name, data.merge({entity: remote_class_name})))
end

.delete(id) ⇒ Object



47
48
49
# File 'lib/rubill/base.rb', line 47

def self.delete(id)
  Query.delete(remote_class_name, id)
end

.find(id) ⇒ Object



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

def self.find(id)
  new(Query.read(remote_class_name, id))
end

.find_by_name(name) ⇒ Object



51
52
53
54
55
# File 'lib/rubill/base.rb', line 51

def self.find_by_name(name)
  raw_result = Query.list(remote_class_name, 0, 1, [Query::Filter.new("name", "=", name)])

  new(raw_result.first)
end

.update(data) ⇒ Object



43
44
45
# File 'lib/rubill/base.rb', line 43

def self.update(data)
  Query.update(remote_class_name, data.merge({entity: remote_class_name}))
end

.where(filters = []) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubill/base.rb', line 57

def self.where(filters=[])
  raise ArgumentError unless filters.is_a?(Enumerable)
  raise ArgumentError if !filters.is_a?(Hash) && !filters.all? { |f| f.is_a?(Query::Filter) }

  if filters.is_a?(Hash)
    filters = filters.map do |field, value|
      Query::Filter.new(field.to_s, "=", value)
    end
  end

  result = []
  start = 0
  step = 999
  loop do
    chunk = Query.list(remote_class_name, start, step, filters)

    if !chunk.empty?
      records = chunk.map { |r| new(r) }
      result += records
      start += step
    end

    break if chunk.length < step
  end

  result
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
# File 'lib/rubill/base.rb', line 11

def [](key)
  remote_record.send(:[], key)
end

#[]=(key, value) ⇒ Object



15
16
17
# File 'lib/rubill/base.rb', line 15

def []=(key, value)
  remote_record.send(:[]=, key, value)
end

#deleteObject



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

def delete
  self.class.delete(id)
end

#idObject



23
24
25
# File 'lib/rubill/base.rb', line 23

def id
  remote_record[:id]
end

#saveObject



27
28
29
# File 'lib/rubill/base.rb', line 27

def save
  self.class.update(remote_record)
end