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'


62
63
64
65
66
# File 'lib/tms_client/instance_resource.rb', line 62

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


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

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



51
52
53
# File 'lib/tms_client/instance_resource.rb', line 51

def custom_class_names
  @custom_class_names ||= {}
end

#readonly_attributes(*attrs) ⇒ Object

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



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

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


75
76
77
78
79
# File 'lib/tms_client/instance_resource.rb', line 75

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



81
82
83
84
85
86
# File 'lib/tms_client/instance_resource.rb', line 81

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



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

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.



14
15
16
17
18
19
20
21
# File 'lib/tms_client/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