Class: Velocity::Base

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

Direct Known Subclasses

Contributor, Invite, Role, Team

Defined Under Namespace

Classes: NotFoundError

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



13
14
15
# File 'lib/velocity/base.rb', line 13

def initialize(args)
  super(args.deep_transform_keys {|key| key.to_s.underscore })
end

Class Attribute Details

.resource_class_nameObject (readonly)

Returns the value of attribute resource_class_name.



6
7
8
# File 'lib/velocity/base.rb', line 6

def resource_class_name
  @resource_class_name
end

Class Method Details

.allObject



22
23
24
25
26
# File 'lib/velocity/base.rb', line 22

def self.all
  resource_class.new({}).fetch.map do |data|
    new(data["attributes"].merge(id: data["id"]))
  end
end

.create(args) ⇒ Object



17
18
19
20
# File 'lib/velocity/base.rb', line 17

def self.create(args)
  data = resource_class.new(args).create
  new(data["attributes"].merge(id: data["id"]))
end

.find_by(args) ⇒ Object



34
35
36
# File 'lib/velocity/base.rb', line 34

def self.find_by(args)
  where(args).first
end

.find_by!(args) ⇒ Object



38
39
40
# File 'lib/velocity/base.rb', line 38

def self.find_by!(args)
  where(args).first or raise NotFoundError, "record not found with #{args.inspect}"
end

.resource_class(class_name = nil) ⇒ Object



8
9
10
# File 'lib/velocity/base.rb', line 8

def resource_class(class_name = nil)
  @resource_class_name ||= class_name
end

.where(args) ⇒ Object



28
29
30
31
32
# File 'lib/velocity/base.rb', line 28

def self.where(args)
  resource_class.new(args).fetch.map do |data|
    new(data["attributes"].merge(id: data["id"]))
  end
end

Instance Method Details

#update(args) ⇒ Object



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

def update(args)
  data = self.class.resource_class.new(args.merge(id: self.id)).update
  self.class.new(data["attributes"].merge(id: data["id"]))
end