Class: RTrail::Entity

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/rtrail/entity.rb

Overview

Base class for RTrail objects

Direct Known Subclasses

Case, Plan, Project, Result, Run, Section, Suite, Test

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#is_id?, #path_with_params

Constructor Details

#initialize(id_or_data) ⇒ Entity

Returns a new instance of Entity.



29
30
31
32
33
34
35
# File 'lib/rtrail/entity.rb', line 29

def initialize(id_or_data)
  if id_or_data.is_a?(Hash) || id_or_data.is_a?(Entity)
    @data = Hashie::Mash.new(id_or_data)
  else
    @data = fetch(id_or_data)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Pass-through to Hashie::Mash for attribute access



44
45
46
# File 'lib/rtrail/entity.rb', line 44

def method_missing(meth, *args, &block)
  return data.send(meth, *args, &block)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



36
37
38
# File 'lib/rtrail/entity.rb', line 36

def data
  @data
end

Class Method Details

.basenameObject

Return the plain lowercase name of the derived class, ex. “project”, “suite”, “case”, suitable for passing to the ‘get_*` API methods.



20
21
22
# File 'lib/rtrail/entity.rb', line 20

def basename
  return self.name.downcase.gsub(/^.*::/, '')
end

.clientObject



13
14
15
# File 'lib/rtrail/entity.rb', line 13

def client
  @@client
end

.client=(client) ⇒ Object



9
10
11
# File 'lib/rtrail/entity.rb', line 9

def client=(client)
  @@client = client
end

Instance Method Details

#add_entity(klass, parent_id, params = {}) ⇒ Object

Add a new entity by invoking POST ‘add_<thing>/<id>`.



59
60
61
62
# File 'lib/rtrail/entity.rb', line 59

def add_entity(klass, parent_id, params={})
  path = "add_#{klass.basename}/#{parent_id}"
  return klass.new(client.post(path, params))
end

#clientObject



25
26
27
# File 'lib/rtrail/entity.rb', line 25

def client
  return self.class.client
end

#fetch(id) ⇒ Object

Fetch data for a derived class object.



39
40
41
# File 'lib/rtrail/entity.rb', line 39

def fetch(id)
  return client.get("get_#{self.class.basename}/#{id}")
end

#get_entities(klass, parent_id, params = {}) ⇒ Object

Return a list of entities retrieved from ‘get_<thing>s/<id>`.



49
50
51
52
53
54
55
56
# File 'lib/rtrail/entity.rb', line 49

def get_entities(klass, parent_id, params={})
  path = path_with_params(
    "get_#{klass.basename}s/#{parent_id}", params)

  return client.get(path).map do |data|
    klass.new(data)
  end
end