Class: ZendeskAppsSupport::Validations::ValidationError
- Inherits:
-
Object
- Object
- ZendeskAppsSupport::Validations::ValidationError
- Defined in:
- lib/zendesk_apps_support/validations/validation_error.rb
Direct Known Subclasses
Defined Under Namespace
Classes: DeserializationError
Constant Summary collapse
- KEY_PREFIX =
'txt.apps.admin.error.app_build.'.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
- .from_hash(hash) ⇒ Object
-
.from_json(json) ⇒ Object
Turn a JSON string into a ValidationError.
-
.vivify(hash) ⇒ Object
Turn a Hash into a ValidationError.
Instance Method Summary collapse
- #as_json(*options) ⇒ Object
-
#initialize(key, data = nil) ⇒ ValidationError
constructor
A new instance of ValidationError.
- #to_json(*options) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key, data = nil) ⇒ ValidationError
Returns a new instance of ValidationError.
47 48 49 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 47 def initialize(key, data = nil) @key, @data = key, symbolize_keys(data || {}) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
45 46 47 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 45 def data @data end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
45 46 47 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 45 def key @key end |
Class Method Details
.from_hash(hash) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 26 def from_hash(hash) raise DeserializationError.new(hash) unless hash['class'] klass = constantize(hash['class']) raise DeserializationError.new(hash) unless klass <= self klass.vivify(hash) end |
.from_json(json) ⇒ Object
Turn a JSON string into a ValidationError.
18 19 20 21 22 23 24 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 18 def from_json(json) hash = MultiJson.decode(json) raise DeserializationError.new(json) unless hash.is_a?(Hash) from_hash(hash) rescue MultiJson::DecodeError, NameError raise DeserializationError.new(json) end |
.vivify(hash) ⇒ Object
Turn a Hash into a ValidationError. Used within from_json.
34 35 36 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 34 def vivify(hash) new(hash['key'], hash['data']) end |
Instance Method Details
#as_json(*options) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 59 def as_json(*) { 'class' => self.class.to_s, 'key' => key, 'data' => data } end |
#to_json(*options) ⇒ Object
55 56 57 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 55 def to_json(*) MultiJson.encode(as_json) end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/zendesk_apps_support/validations/validation_error.rb', line 51 def to_s ZendeskAppsSupport::I18n.t("#{KEY_PREFIX}#{key}", data) end |