Module: Daylight::ReadOnly

Included in:
API
Defined in:
lib/daylight/read_only.rb

Overview

Support for read_only attributes.

Attributes that are read_only are specified in the metadata from the response from the API.

Uses that information to keep from sending those read_only attributes in subsequent requests to the API.

This is useful for computational values that are served that do not have corresponding column in the data store.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object (private)

Extends ‘method_missing` to raise an error when attempting to set a read_only attribute.

Otherwise it continues with the ‘ActiveResource::Base#method_missing` functionality.



67
68
69
70
71
72
73
74
# File 'lib/daylight/read_only.rb', line 67

def method_missing(method_name, *arguments)
  if read_only?(method_name)
    logger.warn "Cannot set read_only attribute: #{method_name[0...-1]}" if logger
    raise NoMethodError, "Cannot set read_only attribute: #{method_name[0...-1]}"
  end

  super
end

Instance Method Details

#read_onlyObject

Get the list of read_only attributes from the metadata attribute. If there are none then an empty array is supplied.

See: metadata



21
22
23
# File 'lib/daylight/read_only.rb', line 21

def read_only
  [:read_only] || []
end

#respond_to?(method_name, include_priv = false) ⇒ Boolean

Writers for read_only attributes are not included as methods – This is how we continue to prevent these read_only attributes to be set internally by removing ActiveResource’s ability to set their values

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/daylight/read_only.rb', line 54

def respond_to?(method_name, include_priv = false)
  return false if read_only?(method_name)
  super
end

#serializable_hash(options = nil) ⇒ Object

Adds API specific options when generating a serializable hash. Removes read_only attributes for requests.

See except_read_only



32
33
34
# File 'lib/daylight/read_only.rb', line 32

def serializable_hash(options=nil)
  super(except_read_only(options || {}))
end

#to_xml(options = {}) ⇒ Object

Adds API specific options when generating xml. Removes read_only attributes for requests.

See except_read_only



43
44
45
# File 'lib/daylight/read_only.rb', line 43

def to_xml(options={})
  super(except_read_only(options))
end