Class: HaveAPI::Parameters::Resource
- Inherits:
-
Object
- Object
- HaveAPI::Parameters::Resource
- Defined in:
- lib/haveapi/parameters/resource.rb
Instance Attribute Summary collapse
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value_id ⇒ Object
readonly
Returns the value of attribute value_id.
-
#value_label ⇒ Object
readonly
Returns the value of attribute value_label.
-
#value_params ⇒ Object
readonly
Returns the value of attribute value_params.
Instance Method Summary collapse
- #clean(raw) ⇒ Object
- #db_name ⇒ Object
- #describe(context) ⇒ Object
- #format_output(v) ⇒ Object
-
#initialize(resource, name: nil, label: nil, desc: nil, choices: nil, value_id: :id, value_label: :label, required: nil, db_name: nil, fetch: nil) ⇒ Resource
constructor
A new instance of Resource.
- #optional? ⇒ Boolean
- #patch(attrs) ⇒ Object
- #required? ⇒ Boolean
- #show_action ⇒ Object
- #show_index ⇒ Object
- #validate(v, params) ⇒ Object
- #validate_build_output ⇒ Object
Constructor Details
#initialize(resource, name: nil, label: nil, desc: nil, choices: nil, value_id: :id, value_label: :label, required: nil, db_name: nil, fetch: nil) ⇒ Resource
Returns a new instance of Resource.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/haveapi/parameters/resource.rb', line 6 def initialize(resource, name: nil, label: nil, desc: nil, choices: nil, value_id: :id, value_label: :label, required: nil, db_name: nil, fetch: nil) @resource = resource @resource_path = build_resource_path(resource) @name = name || resource.resource_name.underscore.to_sym @label = label || (name && name.to_s.capitalize) || resource.resource_name @desc = desc @choices = choices || @resource::Index @value_id = value_id @value_label = value_label @required = required @db_name = db_name @extra = { fetch: } end |
Instance Attribute Details
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def choices @choices end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def desc @desc end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def name @name end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def resource @resource end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def type @type end |
#value_id ⇒ Object (readonly)
Returns the value of attribute value_id.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def value_id @value_id end |
#value_label ⇒ Object (readonly)
Returns the value of attribute value_label.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def value_label @value_label end |
#value_params ⇒ Object (readonly)
Returns the value of attribute value_params.
3 4 5 |
# File 'lib/haveapi/parameters/resource.rb', line 3 def value_params @value_params end |
Instance Method Details
#clean(raw) ⇒ Object
94 95 96 97 98 |
# File 'lib/haveapi/parameters/resource.rb', line 94 def clean(raw) ::HaveAPI::ModelAdapter.for( show_action.input.layout, @resource.model ).input_clean(@resource.model, raw, @extra) end |
#db_name ⇒ Object
24 25 26 |
# File 'lib/haveapi/parameters/resource.rb', line 24 def db_name @db_name || @name end |
#describe(context) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/haveapi/parameters/resource.rb', line 44 def describe(context) val_path = context.path_for( @resource::Show, context.endpoint && context.action_prepare && context.layout == :object && context.call_path_params(context.action, context.action_prepare) ) val_method = @resource::Index.http_method.to_s.upcase choices_path = context.path_for( @choices, context.endpoint && context.layout == :object && context.call_path_params(context.action, context.action_prepare) ) choices_method = @choices.http_method.to_s.upcase { required: required?, label: @label, description: @desc, type: 'Resource', resource: @resource_path, value_id: @value_id, value_label: @value_label, value: context.action_prepare && { path: val_path, method: val_method, help: "#{val_path}?method=#{val_method}" }, choices: { path: choices_path, method: choices_method, help: "#{choices_path}?method=#{choices_method}" } } end |
#format_output(v) ⇒ Object
104 105 106 |
# File 'lib/haveapi/parameters/resource.rb', line 104 def format_output(v) v end |
#optional? ⇒ Boolean
32 33 34 |
# File 'lib/haveapi/parameters/resource.rb', line 32 def optional? !@required end |
#patch(attrs) ⇒ Object
90 91 92 |
# File 'lib/haveapi/parameters/resource.rb', line 90 def patch(attrs) attrs.each { |k, v| instance_variable_set("@#{k}", v) } end |
#required? ⇒ Boolean
28 29 30 |
# File 'lib/haveapi/parameters/resource.rb', line 28 def required? @required end |
#show_action ⇒ Object
36 37 38 |
# File 'lib/haveapi/parameters/resource.rb', line 36 def show_action @resource::Show end |
#show_index ⇒ Object
40 41 42 |
# File 'lib/haveapi/parameters/resource.rb', line 40 def show_index @resource::Index end |
#validate(v, params) ⇒ Object
100 101 102 |
# File 'lib/haveapi/parameters/resource.rb', line 100 def validate(v, params) true end |
#validate_build_output ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/haveapi/parameters/resource.rb', line 78 def validate_build_output %i[value_id value_label].each do |name| v = instance_variable_get("@#{name}") [show_action, show_index].each do |klass| next unless klass.instance_variable_get('@output')[v].nil? raise "association to '#{@resource}': value_label '#{v}' is not an output parameter of '#{klass}'" end end end |