Class: ZendeskRails::Resource

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
app/models/zendesk_rails/resource.rb

Direct Known Subclasses

Comment, Ticket

Defined Under Namespace

Classes: NotFoundException

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



38
39
40
# File 'app/models/zendesk_rails/resource.rb', line 38

def initialize(attributes = {})
  @attributes = attributes.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'app/models/zendesk_rails/resource.rb', line 50

def method_missing(meth, *args, &block)
  if attributes.key?(meth)
    attributes[meth]
  elsif known_fields.include?(meth.to_s)
    nil
  else
    super
  end
end

Class Attribute Details

.known_fieldsObject (readonly)

Returns the value of attribute known_fields.



23
24
25
# File 'app/models/zendesk_rails/resource.rb', line 23

def known_fields
  @known_fields
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



36
37
38
# File 'app/models/zendesk_rails/resource.rb', line 36

def attributes
  @attributes
end

Class Method Details

.has_fields(*names) ⇒ Object



25
26
27
28
# File 'app/models/zendesk_rails/resource.rb', line 25

def has_fields(*names)
  @known_fields ||= []
  @known_fields |= names.map(&:to_s)
end

.resource_not_found!(id) ⇒ Object



30
31
32
# File 'app/models/zendesk_rails/resource.rb', line 30

def resource_not_found!(id)
  fail NotFoundException.new(id, self)
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/zendesk_rails/resource.rb', line 42

def persisted?
  false
end

#respond_to_missing?(meth) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/zendesk_rails/resource.rb', line 46

def respond_to_missing?(meth, *)
  known_fields.include?(meth.to_s) || super
end