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.



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

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.



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

def key
  @key
end

#sub_filtersObject (readonly)

Returns the value of attribute sub_filters.



23
24
25
# File 'lib/objective/filter.rb', line 23

def sub_filters
  @sub_filters
end

Class Method Details

.filter_name(klass = self) ⇒ Object



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

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

.inherited(child_class) ⇒ Object



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

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



139
140
141
# File 'lib/objective/filter.rb', line 139

def coerce(raw)
  raw
end

#coerce_error(coerced) ⇒ Object



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

def coerce_error(coerced)
end

#defaultObject



51
52
53
# File 'lib/objective/filter.rb', line 51

def default
  options.default
end

#default?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/objective/filter.rb', line 47

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

#dupObject



41
42
43
44
45
# File 'lib/objective/filter.rb', line 41

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

#feed(raw) ⇒ Object



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

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



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

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



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

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

  feed_result(errors, raw, coerced)
end

#feed_nilObject



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

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



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

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



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

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

#handle_errors(given_error) ⇒ Object



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

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



33
34
35
# File 'lib/objective/filter.rb', line 33

def options
  @options ||= OpenStruct.new(self.class.const_get('Options').to_h.merge(@given_options))
end

#sub_filters_hashObject



37
38
39
# File 'lib/objective/filter.rb', line 37

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

#validate(coerced) ⇒ Object



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

def validate(coerced)
end