Class: Restapi::ParamDescription
- Inherits:
-
Object
- Object
- Restapi::ParamDescription
- Defined in:
- lib/restapi/param_description.rb
Overview
method parameter description
name - method name (show) desc - description required - boolean if required validator - Validator::BaseValidator subclass
Instance Attribute Summary collapse
-
#allow_nil ⇒ Object
readonly
Returns the value of attribute allow_nil.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
- #full_name ⇒ Object
-
#initialize(name, *args, &block) ⇒ ParamDescription
constructor
A new instance of ParamDescription.
-
#parents_and_self ⇒ Object
returns an array of all the parents: starting with the root parent ending with itself.
- #to_json ⇒ Object
- #validate(value) ⇒ Object
Constructor Details
#initialize(name, *args, &block) ⇒ ParamDescription
Returns a new instance of ParamDescription.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/restapi/param_description.rb', line 15 def initialize(name, *args, &block) if args.size > 1 || !args.first.is_a?(Hash) validator_type = args.shift || nil else validator_type = nil end = args.pop || {} @name = name @desc = Restapi.markup_to_html([:desc] || '') @required = [:required] || false @allow_nil = [:allow_nil] || false @validator = nil unless validator_type.nil? @validator = Validator::BaseValidator.find(self, validator_type, , block) raise "Validator not found." unless validator end end |
Instance Attribute Details
#allow_nil ⇒ Object (readonly)
Returns the value of attribute allow_nil.
11 12 13 |
# File 'lib/restapi/param_description.rb', line 11 def allow_nil @allow_nil end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
11 12 13 |
# File 'lib/restapi/param_description.rb', line 11 def desc @desc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/restapi/param_description.rb', line 11 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
13 14 15 |
# File 'lib/restapi/param_description.rb', line 13 def parent @parent end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
11 12 13 |
# File 'lib/restapi/param_description.rb', line 11 def required @required end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
11 12 13 |
# File 'lib/restapi/param_description.rb', line 11 def validator @validator end |
Instance Method Details
#full_name ⇒ Object
44 45 46 47 |
# File 'lib/restapi/param_description.rb', line 44 def full_name name_parts = parents_and_self.map(&:name) return ([name_parts.first] + name_parts[1..-1].map { |n| "[#{n}]" }).join("") end |
#parents_and_self ⇒ Object
returns an array of all the parents: starting with the root parent ending with itself
51 52 53 54 55 56 57 58 |
# File 'lib/restapi/param_description.rb', line 51 def parents_and_self ret = [] if self.parent ret.concat(self.parent.parents_and_self) end ret << self ret end |
#to_json ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/restapi/param_description.rb', line 60 def to_json if validator.is_a? Restapi::Validator::HashValidator { :name => name.to_s, :full_name => full_name, :description => desc, :required => required, :allow_nil => allow_nil, :validator => validator.to_s, :expected_type => validator.expected_type, :params => validator.hash_params_ordered.map(&:to_json) } else { :name => name.to_s, :full_name => full_name, :description => desc, :required => required, :allow_nil => allow_nil, :validator => validator.to_s, :expected_type => validator.expected_type } end end |
#validate(value) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/restapi/param_description.rb', line 37 def validate(value) return true if @allow_nil && value.nil? unless @validator.valid?(value) raise ArgumentError.new(@validator.error) end end |