Class: Objective::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/objective/filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, opts = {}, &block) ⇒ Filter

Returns a new instance of Filter.



23
24
25
26
27
28
29
# File 'lib/objective/filter.rb', line 23

def initialize(key = nil, opts = {}, &block)
  @key = key
  @given_options = opts
  @sub_filters ||= []

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



20
21
22
# File 'lib/objective/filter.rb', line 20

def key
  @key
end

#sub_filtersObject (readonly)

Returns the value of attribute sub_filters.



21
22
23
# File 'lib/objective/filter.rb', line 21

def sub_filters
  @sub_filters
end

Class Method Details

.filter_name(klass = self) ⇒ Object



14
15
16
17
18
# File 'lib/objective/filter.rb', line 14

def self.filter_name(klass = self)
  filter_name = klass.name.match(/\AObjective::Filters::(.*)Filter\z/)&.[](1)&.underscore
  raise 'filename error in filters folder' unless filter_name
  filter_name
end

.inherited(child_class) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/objective/filter.rb', line 4

def self.inherited(child_class)
  method_name = filter_name(child_class)

  define_method(method_name) do |*args, &block|
    args.unshift(nil) if args[0].is_a?(Hash)
    new_filter = child_class.new(*args, &block)
    sub_filters.push(new_filter)
  end
end

Instance Method Details

#coerce(raw) ⇒ Object



141
142
143
# File 'lib/objective/filter.rb', line 141

def coerce(raw)
  raw
end

#coerce_error(coerced) ⇒ Object



145
146
# File 'lib/objective/filter.rb', line 145

def coerce_error(coerced)
end

#defaultObject



53
54
55
# File 'lib/objective/filter.rb', line 53

def default
  options.default
end

#default?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/objective/filter.rb', line 49

def default?
  options.to_h.key?(:default)
end

#dupObject



43
44
45
46
47
# File 'lib/objective/filter.rb', line 43

def dup
  dupped = self.class.new
  sub_filters.each { |sf| dupped.sub_filters.push(sf) }
  dupped
end

#feed(raw) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/objective/filter.rb', line 57

def feed(raw)
  return feed_none if raw == Objective::NONE
  return feed_nil if raw.nil?

  coerced = options.strict == true ? raw : coerce(raw)
  errors = coerce_error(coerced)
  return feed_invalid(errors, raw, raw) if errors

  errors = validate(coerced)
  return feed_empty(raw, coerced) if errors == :empty

  feed_result(errors, raw, coerced)
end

#feed_empty(raw, coerced) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/objective/filter.rb', line 117

def feed_empty(raw, coerced)
  case options.empty
  when Objective::ALLOW
    errors = nil
  when Objective::DENY
    errors = :empty
  when Objective::DISCARD
    return Objective::DISCARD
  else
    coerced = options.empty
  end

  feed_result(errors, raw, coerced)
end

#feed_invalid(errors, raw, coerced) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/objective/filter.rb', line 104

def feed_invalid(errors, raw, coerced)
  case options.invalid
  when Objective::DENY
    # nothing
  when Objective::DISCARD
    return Objective::DISCARD
  else
    coerced = options.invalid
  end

  feed_result(errors, raw, coerced)
end

#feed_nilObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/objective/filter.rb', line 87

def feed_nil
  case options.nils
  when Objective::ALLOW
    coerced = nil
    errors = nil
  when Objective::DENY
    coerced = nil
    errors = :nils
  when Objective::DISCARD
    return Objective::DISCARD
  else
    coerced = options.nils
  end

  feed_result(errors, nil, coerced)
end

#feed_noneObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/objective/filter.rb', line 71

def feed_none
  case options.none
  when Objective::ALLOW
    return Objective::DISCARD
  when Objective::DENY
    coerced = Objective::NONE
    errors = :required
  when Objective::DISCARD
    raise 'the none option cannot be discarded — did you mean to use allow instead?'
  else
    coerced = options.none
  end

  feed_result(errors, Objective::NONE, coerced)
end

#feed_result(errors, raw, coerced) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/objective/filter.rb', line 132

def feed_result(errors, raw, coerced)
  OpenStruct.new(
    errors: errors,
    raw: raw,
    coerced: coerced,
    inputs: coerced
  )
end

#handle_errors(given_error) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/objective/filter.rb', line 151

def handle_errors(given_error)
  return if given_error.nil?

  case given_error
  when Objective::Errors::ErrorHash
    given_error
  when Objective::Errors::ErrorArray
    given_error
  when Objective::Errors::ErrorAtom
    given_error
  else
    Objective::Errors::ErrorAtom.new(key, given_error)
  end
end

#optionsObject



31
32
33
# File 'lib/objective/filter.rb', line 31

def options
  @options ||= OpenStruct.new(type_specific_options_hash.merge(@given_options))
end

#sub_filters_hashObject



39
40
41
# File 'lib/objective/filter.rb', line 39

def sub_filters_hash
  sub_filters.each_with_object({}) { |sf, result| result[sf.key] = sf }
end

#type_specific_options_hashObject



35
36
37
# File 'lib/objective/filter.rb', line 35

def type_specific_options_hash
  Objective::Filters::Config[self.class.filter_name].to_h
end

#validate(coerced) ⇒ Object



148
149
# File 'lib/objective/filter.rb', line 148

def validate(coerced)
end