Class: ParamsReady::Intent

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Format::Wrapper, Restriction::Wrapper
Defined in:
lib/params_ready/intent.rb

Instance Attribute Summary collapse

Attributes included from Format::Wrapper

#format

Attributes included from Restriction::Wrapper

#restriction

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Restriction::Wrapper

#delegate, #for_children, #permit, #permit_all, #prohibit, #to_restriction

Constructor Details

#initialize(format, restriction = Restriction.blanket_permission, data: nil) ⇒ Intent

Returns a new instance of Intent.

Raises:



18
19
20
21
22
23
24
25
# File 'lib/params_ready/intent.rb', line 18

def initialize(format, restriction = Restriction.blanket_permission, data: nil)
  @format = Format.resolve(format).freeze
  raise ParamsReadyError, "Restriction expected, got: #{restriction.inspect}" unless restriction.is_a? Restriction
  @restriction = restriction
  @data = check_data(data)
  @hash = [@format, @restriction, @data].hash
  freeze
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/params_ready/intent.rb', line 16

def data
  @data
end

#hashObject (readonly)

Returns the value of attribute hash.



16
17
18
# File 'lib/params_ready/intent.rb', line 16

def hash
  @hash
end

Class Method Details

.instance(name) ⇒ Object



47
48
49
50
# File 'lib/params_ready/intent.rb', line 47

def self.instance(name)
  format = Format.instance(name)
  Intent.new(format)
end

.resolve(intent_or_name) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/params_ready/intent.rb', line 52

def self.resolve(intent_or_name)
  if intent_or_name.is_a? Intent
    intent_or_name
  else
    instance(intent_or_name)
  end
end

Instance Method Details

#==(other) ⇒ Object



60
61
62
63
64
# File 'lib/params_ready/intent.rb', line 60

def ==(other)
  return false unless other.is_a?(Intent)
  return true if object_id == other.object_id
  restriction == other.restriction && format == other.format && data == other.data
end

#check_data(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/params_ready/intent.rb', line 27

def check_data(data)
  return if data.nil?
  # The reason we require data object to be
  # a Parameter is that it must be deep frozen
  # for the output memoizing feature to work properly.
  raise 'Data object must be a parameter' unless data.is_a? Parameter::Parameter
  raise 'Data object must be frozen' unless data.frozen?

  data
end

#clone(restriction:) ⇒ Object



12
13
14
# File 'lib/params_ready/intent.rb', line 12

def clone(restriction:)
  Intent.new @format, restriction, data: @data
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/params_ready/intent.rb', line 66

def eql?(other)
  self == other
end

#omit?(parameter) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/params_ready/intent.rb', line 38

def omit?(parameter)
  return true unless permitted?(parameter)
  @format.omit?(parameter)
end

#preserve?(parameter) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/params_ready/intent.rb', line 43

def preserve?(parameter)
  !omit?(parameter)
end