Class: Unchained::Client::Mixins::Resource::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/unchained/client/mixins/resource.rb

Overview

An ‘Attribute` is what `attribute` creates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, opts) ⇒ Attribute

Returns a new instance of Attribute.



27
28
29
30
31
# File 'lib/unchained/client/mixins/resource.rb', line 27

def initialize(name, type, opts)
  @name = name
  @type = type
  @opts = opts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/unchained/client/mixins/resource.rb', line 23

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



25
26
27
# File 'lib/unchained/client/mixins/resource.rb', line 25

def opts
  @opts
end

#typeObject (readonly)

Returns the value of attribute type.



24
25
26
# File 'lib/unchained/client/mixins/resource.rb', line 24

def type
  @type
end

Instance Method Details

#allow_nil?Boolean

Whether or not to allow nil, defaults to false.

Returns:

  • (Boolean)


48
49
50
# File 'lib/unchained/client/mixins/resource.rb', line 48

def allow_nil?
  @opts.fetch(:allow_nil, false)
end

#json_fieldObject

The field that maps to this attribute in the JSON.

Usage:

# json_field will return 'FOO'
attribute :foo, json: 'FOO'

# json_field will return 'foo_bar'
attribute :foo_bar


43
44
45
# File 'lib/unchained/client/mixins/resource.rb', line 43

def json_field
  @opts.fetch(:json, @name.to_s)
end