Class: ParamsReady::OutputParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/output_parameters.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameter, intent, scoped_name = nil, scoped_id = nil) ⇒ OutputParameters

Returns a new instance of OutputParameters.

Raises:



40
41
42
43
44
45
46
47
# File 'lib/params_ready/output_parameters.rb', line 40

def initialize(parameter, intent, scoped_name = nil, scoped_id = nil)
  raise ParamsReadyError, "Expected parameter '#{parameter.name}' to be frozen" unless parameter.frozen?
  @parameter = parameter
  @intent = Intent.resolve(intent)
  @tree = {}
  @scoped_name = scoped_name || @intent.hash_key(parameter).to_s
  @scoped_id = scoped_id || @intent.hash_key(parameter).to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/params_ready/output_parameters.rb', line 7

def method_missing(name, *args, &block)
  if @parameter.respond_to? name, false
    @parameter.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#parameterObject (readonly)

Returns the value of attribute parameter.



5
6
7
# File 'lib/params_ready/output_parameters.rb', line 5

def parameter
  @parameter
end

#scoped_idObject (readonly)

Returns the value of attribute scoped_id.



5
6
7
# File 'lib/params_ready/output_parameters.rb', line 5

def scoped_id
  @scoped_id
end

Class Method Details

.decorate(parameter, *args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/params_ready/output_parameters.rb', line 23

def self.decorate(parameter, *args)
  intent = case args.length
  when 0
    Intent.instance(:frontend)
  when 1
    Intent.resolve(args[0])
  when 2
    format = args[0]
    restriction = args[1]
    Intent.new format, restriction
  else
    msg = "ArgumentError: wrong number of arguments (given #{args.length + 1}, expected 1..3)"
    raise ParamsReadyError, msg
  end
  new parameter, intent
end

.flatten_hash(hash, scope) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/params_ready/output_parameters.rb', line 90

def self.flatten_hash(hash, scope)
  hash.flat_map do |key, value|
    nested = scope.empty? ? key.to_s : "#{scope}[#{key}]"
    if value.is_a? Hash
      flatten_hash(value, nested)
    else
      [[nested, value]]
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/params_ready/output_parameters.rb', line 54

def [](key)
  if @tree.key? key
    @tree[key]
  elsif @parameter.respond_to? :[]
    child = @parameter[key]
    formatted_name = if @parameter.definition.is_a? Parameter::ArrayParameterDefinition::ArrayLike
      key.to_s
    else
      @intent.hash_key(child).to_s
    end
    child_scoped_name = @scoped_name.empty? ? formatted_name : "#{@scoped_name}[#{formatted_name}]"
    child_scoped_id = @scoped_id.empty? ? formatted_name : "#{@scoped_id}_#{formatted_name}"
    intent = @parameter.intent_for_children(@intent)
    decorated = OutputParameters.new(child, intent, child_scoped_name, child_scoped_id)
    @tree[key] = decorated
    decorated
  else
    raise ParamsReadyError, "Parameter '#{@parameter.name}' doesn't support square brackets access"
  end
end

#build_relation(context: @intent.restriction, **opts) ⇒ Object



130
131
132
# File 'lib/params_ready/output_parameters.rb', line 130

def build_relation(context: @intent.restriction, **opts)
  @parameter.build_relation(context: context, **opts)
end

#build_select(context: @intent.restriction, **opts) ⇒ Object



126
127
128
# File 'lib/params_ready/output_parameters.rb', line 126

def build_select(context: @intent.restriction, **opts)
  @parameter.build_select(context: context, **opts)
end

#flat_pairs(format = @intent.format, restriction: @intent.restriction, data: @intent.data) ⇒ Object



86
87
88
# File 'lib/params_ready/output_parameters.rb', line 86

def flat_pairs(format = @intent.format, restriction: @intent.restriction, data: @intent.data)
  self.class.flatten_hash(for_output(format, restriction: restriction, data: data), scoped_name)
end

#for_frontend(restriction: @intent.restriction, data: @intent.data) ⇒ Object



114
115
116
# File 'lib/params_ready/output_parameters.rb', line 114

def for_frontend(restriction: @intent.restriction, data: @intent.data)
  @parameter.for_frontend(restriction: restriction, data: data)
end

#for_model(format = :update, restriction: @intent.restriction) ⇒ Object



118
119
120
# File 'lib/params_ready/output_parameters.rb', line 118

def for_model(format = :update, restriction: @intent.restriction)
  @parameter.for_model(format, restriction: restriction)
end

#for_output(format = @intent.format, restriction: @intent.restriction, data: @intent.data) ⇒ Object



110
111
112
# File 'lib/params_ready/output_parameters.rb', line 110

def for_output(format = @intent.format, restriction: @intent.restriction, data: @intent.data)
  @parameter.for_output(format, restriction: restriction, data: data)
end

#format(format = @intent) ⇒ Object



122
123
124
# File 'lib/params_ready/output_parameters.rb', line 122

def format(format = @intent)
  @parameter.format(format)
end

#perform_count(context: @intent.restriction, **opts) ⇒ Object



134
135
136
# File 'lib/params_ready/output_parameters.rb', line 134

def perform_count(context: @intent.restriction, **opts)
  @parameter.perform_count(context: context, **opts)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/params_ready/output_parameters.rb', line 15

def respond_to_missing?(name, include_private = false)
  if @parameter.respond_to? name, include_private
    true
  else
    super
  end
end

#scoped_name(multiple: false) ⇒ Object



49
50
51
52
# File 'lib/params_ready/output_parameters.rb', line 49

def scoped_name(multiple: false)
  return @scoped_name unless multiple
  @scoped_name + "[]"
end

#to_aObject



76
77
78
79
80
81
82
83
84
# File 'lib/params_ready/output_parameters.rb', line 76

def to_a
  if @parameter.definition.is_a? Parameter::ArrayParameterDefinition
    (0...@parameter.length).map do |n|
      self[n]
    end
  else
    raise ParamsReadyError, "Unimplemented method 'to_a' for #{@parameter.definition.class.name}"
  end
end

#to_hash(format = @intent.format, restriction: nil, data: @intent.data) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/params_ready/output_parameters.rb', line 101

def to_hash(format = @intent.format, restriction: nil, data: @intent.data)
  restriction = if restriction.nil?
    Restriction.permit(name => @intent.restriction)
  else
    restriction
  end
  @parameter.to_hash(format, restriction: restriction, data: data)
end