Class: Sinatra::Kagero::Command
- Inherits:
-
Object
- Object
- Sinatra::Kagero::Command
- Defined in:
- lib/sinatra/kagero/command.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
- .attribute(name, type = String, required: false, default: nil) ⇒ Object
- .inherited(subclass) ⇒ Object
- .kagero_attributes ⇒ Object
- .kagero_validations ⇒ Object
- .validates_length_of(name, maximum:, message: nil) ⇒ Object
- .validates_presence_of(*names, message: "is required") ⇒ Object
Instance Method Summary collapse
-
#initialize(input = {}) ⇒ Command
constructor
A new instance of Command.
- #invalid? ⇒ Boolean
- #to_h ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(input = {}) ⇒ Command
Returns a new instance of Command.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sinatra/kagero/command.rb', line 56 def initialize(input = {}) @errors = {} self.class.kagero_attributes.each do |attribute| name = attribute.fetch(:name) value = fetch_value(input, name) value = default_value(attribute.fetch(:default)) if missing?(value) value = coerce_value(value, attribute.fetch(:type)) instance_variable_set(:"@#{name}", value) end end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
54 55 56 |
# File 'lib/sinatra/kagero/command.rb', line 54 def errors @errors end |
Class Method Details
.attribute(name, type = String, required: false, default: nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/sinatra/kagero/command.rb', line 13 def attribute(name, type = String, required: false, default: nil) kagero_attributes << { name: name.to_sym, type: type, required: required, default: default } attr_reader(name) end |
.inherited(subclass) ⇒ Object
7 8 9 10 11 |
# File 'lib/sinatra/kagero/command.rb', line 7 def inherited(subclass) super subclass.instance_variable_set(:@kagero_attributes, kagero_attributes.dup) subclass.instance_variable_set(:@kagero_validations, kagero_validations.dup) end |
.kagero_attributes ⇒ Object
45 46 47 |
# File 'lib/sinatra/kagero/command.rb', line 45 def kagero_attributes @kagero_attributes ||= [] end |
.kagero_validations ⇒ Object
49 50 51 |
# File 'lib/sinatra/kagero/command.rb', line 49 def kagero_validations @kagero_validations ||= [] end |
.validates_length_of(name, maximum:, message: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/sinatra/kagero/command.rb', line 35 def validates_length_of(name, maximum:, message: nil) kagero_validations << { kind: :length, name: name.to_sym, maximum: maximum, message: || "must be #{maximum} characters or less" } end |
.validates_presence_of(*names, message: "is required") ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sinatra/kagero/command.rb', line 24 def validates_presence_of(*names, message: "is required") names.each do |name| kagero_validations << { kind: :presence, name: name.to_sym, message: } end end |
Instance Method Details
#invalid? ⇒ Boolean
73 74 75 |
# File 'lib/sinatra/kagero/command.rb', line 73 def invalid? !valid? end |
#to_h ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sinatra/kagero/command.rb', line 77 def to_h self .class .kagero_attributes .map do |attribute| name = attribute.fetch(:name) [name, instance_variable_get(:"@#{name}")] end .to_h end |
#valid? ⇒ Boolean
67 68 69 70 71 |
# File 'lib/sinatra/kagero/command.rb', line 67 def valid? @errors = {} self.class.kagero_validations.each { |validation| apply_validation(validation) } @errors.empty? end |