Class: HaveAPI::Parameters::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/parameters/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#choicesObject (readonly)

Returns the value of attribute choices.



3
4
5
# File 'lib/haveapi/parameters/resource.rb', line 3

def choices
  @choices
end

#descObject (readonly)

Returns the value of attribute desc.



3
4
5
# File 'lib/haveapi/parameters/resource.rb', line 3

def desc
  @desc
end

#labelObject (readonly)

Returns the value of attribute label.



3
4
5
# File 'lib/haveapi/parameters/resource.rb', line 3

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/haveapi/parameters/resource.rb', line 3

def name
  @name
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/haveapi/parameters/resource.rb', line 3

def resource
  @resource
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/haveapi/parameters/resource.rb', line 3

def type
  @type
end

#value_idObject (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_labelObject (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_paramsObject (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_nameObject



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

Returns:



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

Returns:



28
29
30
# File 'lib/haveapi/parameters/resource.rb', line 28

def required?
  @required
end

#show_actionObject



36
37
38
# File 'lib/haveapi/parameters/resource.rb', line 36

def show_action
  @resource::Show
end

#show_indexObject



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_outputObject



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