Exception: JSONError::Base
- Inherits:
-
StandardError
- Object
- StandardError
- JSONError::Base
- Defined in:
- lib/json_error/base.rb
Constant Summary collapse
- DEFAULT_STATUS =
500
- NULL_TOKEN =
'~'
Class Attribute Summary collapse
-
.key_alias ⇒ Object
readonly
Returns the value of attribute key_alias.
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, options = {}) ⇒ Object
- .properties ⇒ Object
- .status(status = nil) ⇒ 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.
74 75 76 77 78 79 80 |
# File 'lib/json_error/base.rb', line 74 def initialize(attrs = {}) @key = self.class.key || attrs.fetch(:key) @context = attrs[:context] assign_custom_properties(attrs) (attrs) super() end |
Class Attribute Details
.key_alias ⇒ Object (readonly)
Returns the value of attribute key_alias.
9 10 11 |
# File 'lib/json_error/base.rb', line 9 def key_alias @key_alias 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
17 18 19 |
# File 'lib/json_error/base.rb', line 17 def base? self == JSONError.base end |
.custom_properties(*custom_properties) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/json_error/base.rb', line 58 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
44 45 46 |
# File 'lib/json_error/base.rb', line 44 def default_properties %i(status key) end |
.disallowed_property_setting!(property) ⇒ Object
21 22 23 |
# File 'lib/json_error/base.rb', line 21 def disallowed_property_setting!(property) raise(Errors::DisallowedPropertySetting, property: property, class: self) end |
.inherited(base) ⇒ Object
11 12 13 14 15 |
# File 'lib/json_error/base.rb', line 11 def inherited(base) base.instance_variable_set(:@translated_properties, translated_properties) base.instance_variable_set(:@custom_properties, custom_properties) base.instance_variable_set(:@key_alias, key_alias) end |
.key(key = nil, options = {}) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/json_error/base.rb', line 25 def key(key = nil, = {}) return @key if key.nil? disallowed_property_setting!(:key) if base? @key_alias = [:as] if [:as].is_a?(Symbol) @key ||= key end |
.properties ⇒ Object
40 41 42 |
# File 'lib/json_error/base.rb', line 40 def properties [*default_properties, *translated_properties, *custom_properties] end |
.status(status = nil) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/json_error/base.rb', line 33 def status(status = nil) return @status if status.nil? disallowed_property_setting!(:status) if base? @status = status end |
.translated_properties(*translated_properties) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/json_error/base.rb', line 48 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
66 67 68 69 70 71 |
# File 'lib/json_error/base.rb', line 66 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
82 83 84 |
# File 'lib/json_error/base.rb', line 82 def status self.class.status || 500 end |
#to_json(*args) ⇒ Object
86 87 88 |
# File 'lib/json_error/base.rb', line 86 def to_json(*args) visible_properties_hash.to_json(*args) end |