Class: ParamsReady::Format

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

Defined Under Namespace

Modules: Wrapper

Constant Summary collapse

OMIT_ALL =
%i(undefined nil default no_output).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(marshal:, naming_scheme:, remap:, omit:, local:, name: nil) ⇒ Format

Returns a new instance of Format.



25
26
27
28
29
30
31
32
33
34
# File 'lib/params_ready/format.rb', line 25

def initialize(marshal:, naming_scheme:, remap:, omit:, local:, name: nil)
  @marshal = Helpers::Rule(marshal)
  @naming_scheme = naming_scheme
  @remap = remap
  @omit = omit.to_set.freeze
  @local = local
  @name = name.nil? ? name : name.to_sym
  @hash = [@marshal, @naming_scheme, @remap, @omit, @local, @name].hash
  freeze
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



22
23
24
# File 'lib/params_ready/format.rb', line 22

def hash
  @hash
end

#marshalObject (readonly)

Returns the value of attribute marshal.



22
23
24
# File 'lib/params_ready/format.rb', line 22

def marshal
  @marshal
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/params_ready/format.rb', line 22

def name
  @name
end

#naming_schemeObject (readonly)

Returns the value of attribute naming_scheme.



22
23
24
# File 'lib/params_ready/format.rb', line 22

def naming_scheme
  @naming_scheme
end

Class Method Details

.define(name, format) ⇒ Object



109
110
111
112
113
# File 'lib/params_ready/format.rb', line 109

def self.define(name, format)
  @names = @names.dup
  @names[name] = format
  @names.freeze
end

.instance(name) ⇒ Object

Raises:



115
116
117
118
# File 'lib/params_ready/format.rb', line 115

def self.instance(name)
  raise ParamsReadyError, "Unknown format '#{name}'" unless @names.key? name
  @names[name]
end

.resolve(format_or_name) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/params_ready/format.rb', line 120

def self.resolve(format_or_name)
  if format_or_name.is_a? Format
    format_or_name
  elsif format_or_name.is_a? Symbol
    instance(format_or_name)
  elsif format_or_name.respond_to? :format
    format_or_name.format
  else
    raise ParamsReadyError, "Not an acceptable format: #{format_or_name}"
  end
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/params_ready/format.rb', line 36

def ==(other)
  return false unless other.is_a? Format
  return true if self.object_id == other.object_id
  return false unless marshal == other.marshal
  return false unless naming_scheme == other.naming_scheme
  return false unless remap? == other.remap?
  return false unless @omit == other.instance_variable_get(:@omit)
  return false unless local? == other.local?
  return false unless name == other.name

  true
end

#alternative?Boolean

Returns:

  • (Boolean)


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

def alternative?
  @naming_scheme == :alternative
end

#hash_key(parameter) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/params_ready/format.rb', line 61

def hash_key(parameter)
  case @naming_scheme
  when :standard then parameter.name
  when :alternative then parameter.altn
  else
    raise ParamsReadyError, "Unexpected option: #{@naming_scheme}"
  end
end

#local?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/params_ready/format.rb', line 78

def local?
  @local
end

#marshal?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

def marshal?(type)
  @marshal.include?(type)
end

#omit?(parameter) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/params_ready/format.rb', line 70

def omit?(parameter)
  return true if parameter.no_output?(self)
  return true if parameter.is_undefined? && @omit.member?(:undefined)
  return true if parameter.is_nil? && @omit.member?(:nil)
  return true if parameter.is_default? && @omit.member?(:default)
  false
end

#preserve?(parameter) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/params_ready/format.rb', line 82

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

#remap?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/params_ready/format.rb', line 57

def remap?
  @remap
end

#standard?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/params_ready/format.rb', line 53

def standard?
  @naming_scheme == :standard
end

#update(**opts) ⇒ Object



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

def update(**opts)
  opts = instance_variables.reject { |ivar| ivar == :@hash }.map do |ivar|
    value = instance_variable_get(ivar)
    name = ivar.to_s.gsub(/^@/, '').to_sym
    [name, value]
  end.to_h.merge(opts)

  Format.new(**opts)
end