Module: GovDelivery::TMS::InstanceResource::ClassMethods

Defined in:
lib/govdelivery/tms/instance_resource.rb

Instance Method Summary collapse

Instance Method Details

#collection_attribute(attr, tms_class) ⇒ Object

For collections that are represented as attributes (i.e. inline, no href) and that have a class name other than the one we would infer.

Examples:

collection_attributes :recipients, 'EmailRecipient'


84
85
86
87
88
# File 'lib/govdelivery/tms/instance_resource.rb', line 84

def collection_attribute(attr, tms_class)
  @collection_attributes ||= []
  @collection_attributes.push(attr).uniq!
  setup_collection(attr, GovDelivery::TMS.const_get(tms_class))
end

#collection_attributes(*attrs) ⇒ Object

For collections that are represented as attributes (i.e. inline, no href)

Examples:

collection_attributes :recipients


64
65
66
67
68
69
70
71
# File 'lib/govdelivery/tms/instance_resource.rb', line 64

def collection_attributes(*attrs)
  @collection_attributes ||= []
  if attrs.any?
    @collection_attributes.map!(&:to_sym).concat(attrs).uniq!
    @collection_attributes.each { |a| setup_collection(a) }
  end
  @collection_attributes
end

#custom_class_namesObject



73
74
75
# File 'lib/govdelivery/tms/instance_resource.rb', line 73

def custom_class_names
  @custom_class_names ||= {}
end

#linkable_attributes(*attrs) ⇒ Object

Linkable attributes are sent on POST/PUT.



26
27
28
29
30
31
32
# File 'lib/govdelivery/tms/instance_resource.rb', line 26

def linkable_attributes(*attrs)
  @linkable_attributes ||= []
  if attrs.any?
    @linkable_attributes.map!(&:to_sym).concat(attrs).uniq! if attrs.any?
  end
  @linkable_attributes
end

#nullable_attributes(*attrs) ⇒ Object

Nullable attributes are sent as null in the request



50
51
52
53
54
55
56
# File 'lib/govdelivery/tms/instance_resource.rb', line 50

def nullable_attributes(*attrs)
  @nullable_attributes ||= []
  if attrs.any?
    @nullable_attributes.map!(&:to_sym).concat(attrs).uniq! if attrs.any?
  end
  @nullable_attributes
end

#readonly_attributes(*attrs) ⇒ Object

Readonly attributes don’t get POSTed. (timestamps are included by default)



38
39
40
41
42
43
44
45
# File 'lib/govdelivery/tms/instance_resource.rb', line 38

def readonly_attributes(*attrs)
  @readonly_attributes ||= [:created_at, :updated_at, :completed_at]
  if attrs.any?
    @readonly_attributes.map!(&:to_sym).concat(attrs).uniq!
    setup_attributes(@readonly_attributes, true)
  end
  @readonly_attributes
end

#readonly_collection_attribute(attr, tms_class) ⇒ Object

Read-only collection attributes don’t get POSTed. Use this for collections that are represented as attributes, but cannot be modified.

Examples:

readonly_collection_attribute :opens


97
98
99
100
101
# File 'lib/govdelivery/tms/instance_resource.rb', line 97

def readonly_collection_attribute(attr, tms_class)
  @readonly_collection_attributes ||= []
  @readonly_collection_attributes.push(attr).uniq!
  setup_collection(attr, GovDelivery::TMS.const_get(tms_class))
end

#setup_attributes(attrs, readonly = false) ⇒ Object



103
104
105
106
107
108
# File 'lib/govdelivery/tms/instance_resource.rb', line 103

def setup_attributes(attrs, readonly = false)
  attrs.map(&:to_sym).each do |property|
    send :define_method, :"#{property}=", &lambda { |v| @attributes[property] = v } unless readonly
    send :define_method, property.to_sym, &lambda { @attributes[property] }
  end
end

#setup_collection(property, klass = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/govdelivery/tms/instance_resource.rb', line 110

def setup_collection(property, klass = nil)
  if klass
    custom_class_names[property] = klass
  else
    klass ||= GovDelivery::TMS.const_get(property.to_s.capitalize)
  end

  send :define_method, property.to_sym, &lambda { @attributes[property] ||= klass.new(self.client, nil, nil) }
end

#writeable_attributes(*attrs) ⇒ Object

Writeable attributes are sent on POST/PUT.



14
15
16
17
18
19
20
21
# File 'lib/govdelivery/tms/instance_resource.rb', line 14

def writeable_attributes(*attrs)
  @writeable_attributes ||= []
  if attrs.any?
    @writeable_attributes.map!(&:to_sym).concat(attrs).uniq! if attrs.any?
    setup_attributes(@writeable_attributes, false)
  end
  @writeable_attributes
end