Exception: JSONError::Base
- Inherits:
-
StandardError
- Object
- StandardError
- JSONError::Base
- Defined in:
- lib/json_error/base.rb
Constant Summary collapse
- DEFAULT_STATUS =
500
- NULL_TOKEN =
'~'
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .base? ⇒ Boolean
- .custom_properties(*custom_properties) ⇒ Object
- .default_properties ⇒ Object
- .disallowed_property_setting!(property) ⇒ Object
- .inherited(base) ⇒ Object
- .key(key = nil) ⇒ Object
- .properties ⇒ Object
- .translated_properties(*translated_properties) ⇒ Object
- .visible_properties(*visible_properties) ⇒ Object
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ Base
constructor
A new instance of Base.
- #status ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
63 64 65 66 67 68 69 |
# File 'lib/json_error/base.rb', line 63 def initialize(attrs = {}) @key = self.class.key || attrs.fetch(:key) @context = attrs[:context] assign_custom_properties(attrs) (attrs) super() end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/json_error/base.rb', line 6 def context @context end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/json_error/base.rb', line 6 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/json_error/base.rb', line 6 def @options end |
Class Method Details
.base? ⇒ Boolean
14 15 16 |
# File 'lib/json_error/base.rb', line 14 def base? self == JSONError.base end |
.custom_properties(*custom_properties) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/json_error/base.rb', line 47 def custom_properties(*custom_properties) return @custom_properties if custom_properties.empty? disallowed_property_setting!(:custom_properties) if base? attr_accessor(*custom_properties) @custom_properties = [*@custom_properties, *custom_properties].uniq end |
.default_properties ⇒ Object
33 34 35 |
# File 'lib/json_error/base.rb', line 33 def default_properties %i(status key) end |
.disallowed_property_setting!(property) ⇒ Object
18 19 20 |
# File 'lib/json_error/base.rb', line 18 def disallowed_property_setting!(property) raise(Errors::DisallowedPropertySetting, property: property, class: self) end |
.inherited(base) ⇒ Object
9 10 11 12 |
# File 'lib/json_error/base.rb', line 9 def inherited(base) base.instance_variable_set(:@translated_properties, translated_properties) base.instance_variable_set(:@custom_properties, custom_properties) end |
.key(key = nil) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/json_error/base.rb', line 22 def key(key = nil) return @key if key.nil? disallowed_property_setting!(:key) if base? @key ||= key end |
.properties ⇒ Object
29 30 31 |
# File 'lib/json_error/base.rb', line 29 def properties [*default_properties, *translated_properties, *custom_properties] end |
.translated_properties(*translated_properties) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/json_error/base.rb', line 37 def translated_properties(*translated_properties) return @translated_properties if translated_properties.empty? disallowed_property_setting!(:translated_properties) if base? translated_properties.each do |property| define_method(property) { translate(property) } end @translated_properties = [*@translated_properties, *translated_properties].uniq end |
.visible_properties(*visible_properties) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/json_error/base.rb', line 55 def visible_properties(*visible_properties) return(@visible_properties || properties) if visible_properties.empty? disallowed_property_setting!(:visible_properties) if base? @visible_properties = visible_properties.uniq end |
Instance Method Details
#status ⇒ Object
71 72 73 |
# File 'lib/json_error/base.rb', line 71 def status translate(:status, default: DEFAULT_STATUS) end |
#to_json(*args) ⇒ Object
75 76 77 |
# File 'lib/json_error/base.rb', line 75 def to_json(*args) visible_properties_hash.to_json(*args) end |