Class: ParamsReady::AbstractBuilder

Inherits:
Object
  • Object
show all
Extended by:
Extensions::Registry
Defined in:
lib/params_ready/builder.rb

Direct Known Subclasses

Builder, Query::AbstractPredicateBuilder

Defined Under Namespace

Modules: HavingArelTable, HavingModel, HavingValue

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Registry

human_string, registry

Constructor Details

#initialize(definition) ⇒ AbstractBuilder

Returns a new instance of AbstractBuilder.



74
75
76
# File 'lib/params_ready/builder.rb', line 74

def initialize(definition)
  @definition = definition
end

Class Method Details

.builder(name) ⇒ Object



42
43
44
45
46
# File 'lib/params_ready/builder.rb', line 42

def self.builder(name)
  use = @deprecated[name] if @deprecated&.key? name
  warn "Builder name deprecated: #{name}, use: #{use}" unless use.nil?
  fetch_builder(name)
end

.define_parameter(type, *args, **opts, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/params_ready/builder.rb', line 48

def self.define_parameter(type, *args, **opts, &block)
  builder_class = builder(type)
  builder = builder_class.instance(*args, **opts)
  builder.include(&block) unless block.nil?
  builder.build
end

.define_registered_parameter(name, *args, **opts, &block) ⇒ Object



55
56
57
58
# File 'lib/params_ready/builder.rb', line 55

def self.define_registered_parameter(name, *args, **opts, &block)
  full_name = "define_#{name}"
  send(full_name, *args, **opts, &block)
end

.fetch_builderObject



39
# File 'lib/params_ready/builder.rb', line 39

alias_method :fetch_builder, :builder

.instance(*args, **opts) ⇒ Object



68
69
70
# File 'lib/params_ready/builder.rb', line 68

def self.instance(*args, **opts)
  new *args, **opts
end

.register(name) ⇒ Object



27
28
29
# File 'lib/params_ready/builder.rb', line 27

def self.register(name)
  register_builder(name, self)
end

.register_deprecated(name, use:) ⇒ Object

Raises:



31
32
33
34
35
36
# File 'lib/params_ready/builder.rb', line 31

def self.register_deprecated(name, use:)
  raise ParamsReadyError, 'Recommended replacement must not be nil' if use.nil?
  @deprecated ||= {}
  @deprecated[name] = use.to_s.freeze
  register_builder(name, self)
end

.resolve(input, *args, **opts, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/params_ready/builder.rb', line 60

def self.resolve(input, *args, **opts, &block)
  if input.is_a? Parameter::AbstractDefinition
    input
  else
    define_registered_parameter(input, *args, **opts, &block)
  end
end

Instance Method Details

#buildObject



89
90
91
92
# File 'lib/params_ready/builder.rb', line 89

def build
  @definition.finish
  @definition
end

#fetchObject



83
84
85
86
87
# File 'lib/params_ready/builder.rb', line 83

def fetch
  d = @definition
  @definition = nil
  d
end

#include(&block) ⇒ Object



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

def include(&block)
  instance_eval(&block)
  self
end

#open?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/params_ready/builder.rb', line 94

def open?
  return false if @definition.nil?
  return false if @definition.frozen?

  true
end