Class: Clubhouse::ClubhouseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/clubhouse2/clubhouse_resource.rb

Constant Summary collapse

@@subclasses =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, object:) ⇒ ClubhouseResource

Returns a new instance of ClubhouseResource.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clubhouse2/clubhouse_resource.rb', line 34

def initialize(client:, object:)
	@client = client

	self.class.properties.each do |this_property|
		self.class.class_eval { attr_accessor(this_property.to_sym) }
		self.class.send(:define_method, (this_property.to_s + '=').to_sym) do |value|
			update({ this_property => resolve_to_ids(value) })
			instance_variable_set('@' + this_property.to_s, resolve_to_ids(value))
		end
	end

	set_properties(object)
	self
end

Class Method Details

.inherited(other) ⇒ Object



5
6
7
# File 'lib/clubhouse2/clubhouse_resource.rb', line 5

def self.inherited(other)
	@@subclasses << other
end

.property_filter_createObject

A list of properties to exlude from any create request



10
11
12
13
14
15
16
17
# File 'lib/clubhouse2/clubhouse_resource.rb', line 10

def self.property_filter_create
	[
		:archived, :days_to_thermometer, :entity_type, :id, :show_thermometer, :stats, :created_at, :updated_at,
		:started_at, :completed_at, :comments, :position, :started, :project_ids, :completed, :blocker, :moved_at,
		:task_ids, :files, :comment_ids, :workflow_state_id, :story_links, :mention_ids, :file_ids, :linked_file_ids,
		:tasks
	]
end

.property_filter_updateObject

A list of properties to exlude from any update request



20
21
22
# File 'lib/clubhouse2/clubhouse_resource.rb', line 20

def self.property_filter_update
	self.property_filter_create
end

.subclass(sub_class) ⇒ Object



24
25
26
# File 'lib/clubhouse2/clubhouse_resource.rb', line 24

def self.subclass(sub_class)
	@@subclasses.find { |s| s.name == 'Clubhouse::%s' % sub_class.capitalize }
end

.validate(args) ⇒ Object



32
# File 'lib/clubhouse2/clubhouse_resource.rb', line 32

def self.validate(args); end

Instance Method Details

#api_urlObject



28
29
30
# File 'lib/clubhouse2/clubhouse_resource.rb', line 28

def api_url
	self.class.api_url + "/#{@id}"
end

#delete!Object



76
77
78
79
# File 'lib/clubhouse2/clubhouse_resource.rb', line 76

def delete!
	flush
	@client.api_request(:delete, @client.url(api_url))
end

#flushObject

Empties resource cache



65
66
67
# File 'lib/clubhouse2/clubhouse_resource.rb', line 65

def flush
	@client.flush(self.class)
end

#resolve_to_ids(object) ⇒ Object



49
50
51
52
# File 'lib/clubhouse2/clubhouse_resource.rb', line 49

def resolve_to_ids(object)
	return object.collect { |o| resolve_to_ids(o) } if object.is_a? Array
	(object.respond_to?(:id) ? object.id : object)
end

#set_properties(object) ⇒ Object



54
55
56
57
58
# File 'lib/clubhouse2/clubhouse_resource.rb', line 54

def set_properties(object)
	object.each_pair do |k, v|
		instance_variable_set('@' + k.to_s, value_format(k, v))
	end
end

#to_hObject



81
82
83
# File 'lib/clubhouse2/clubhouse_resource.rb', line 81

def to_h
	Hash[ (self.class.properties - self.class.property_filter_create).map { |name| [ name, instance_variable_get('@' + name.to_s) ] } ].compact
end

#update(args = {}) ⇒ Object



69
70
71
72
73
74
# File 'lib/clubhouse2/clubhouse_resource.rb', line 69

def update(args = {})
	new_params = args.reject { |k, v| self.class.property_filter_update.include? k.to_sym }
	validate(new_params)
	flush
	@client.api_request(:put, @client.url(api_url), :json => new_params)
end

#value_format(key, value) ⇒ Object



60
61
62
# File 'lib/clubhouse2/clubhouse_resource.rb', line 60

def value_format(key, value)
	DateTime.strptime(value+'+0000', '%Y-%m-%dT%H:%M:%SZ%z') rescue value
end