Module: TMS::InstanceResource::ClassMethods

Defined in:
lib/tms_client/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'


60
61
62
63
64
# File 'lib/tms_client/instance_resource.rb', line 60

def collection_attribute(attr, tms_class)
  @collection_attributes ||= []
  @collection_attributes.push(attr).uniq!
  setup_collection(attr, 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


40
41
42
43
44
45
46
47
# File 'lib/tms_client/instance_resource.rb', line 40

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



49
50
51
# File 'lib/tms_client/instance_resource.rb', line 49

def custom_class_names
  @custom_class_names ||= {}
end

#readonly_attributes(*attrs) ⇒ Object

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



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

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


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

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

#setup_attributes(attrs, readonly = false) ⇒ Object



79
80
81
82
83
84
# File 'lib/tms_client/instance_resource.rb', line 79

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

#setup_collection(property, klass = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/tms_client/instance_resource.rb', line 86

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

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



12
13
14
15
16
17
18
19
# File 'lib/tms_client/instance_resource.rb', line 12

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