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'


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

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


53
54
55
56
57
58
59
60
# File 'lib/govdelivery/tms/instance_resource.rb', line 53

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



62
63
64
# File 'lib/govdelivery/tms/instance_resource.rb', line 62

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

#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


86
87
88
89
90
# File 'lib/govdelivery/tms/instance_resource.rb', line 86

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



92
93
94
95
96
97
# File 'lib/govdelivery/tms/instance_resource.rb', line 92

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



99
100
101
102
103
104
105
106
107
# File 'lib/govdelivery/tms/instance_resource.rb', line 99

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