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, Entry, Plan, Project, Result, Run, Section, Suite, Test

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

included, #is_id?, #path_with_params

Constructor Details

#initialize(data = {}) ⇒ Entity

Create a new Entity with the given field data.

Parameters:

  • data (Hash, Entity) (defaults to: {})

    Field values to store in the Entity instance, or an Entity instance to clone.



36
37
38
39
40
41
42
43
44
# File 'lib/rtrail/entity.rb', line 36

def initialize(data={})
  if data.is_a?(Hash)
    @data = Hashie::Mash.new(data)
  elsif data.is_a?(Entity)
    @data = Hashie::Mash.new(data.data)
  else
    raise ArgumentError.new("data must be a Hash or Entity.")
  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



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

def method_missing(meth, *args, &block)
  if data.respond_to?(meth)
    return data.send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

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.



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

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

.clientObject



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

def client
  @@client
end

.client=(client) ⇒ Object



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

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>`.



69
70
71
72
# File 'lib/rtrail/entity.rb', line 69

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

#clientObject



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

def client
  return self.class.client
end

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

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



59
60
61
62
63
64
65
66
# File 'lib/rtrail/entity.rb', line 59

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