Class: Objective::Filter
- Inherits:
-
Object
show all
- Defined in:
- lib/objective/filter.rb
Direct Known Subclasses
Objective::Filters::AnyFilter, Objective::Filters::ArrayFilter, Objective::Filters::BooleanFilter, Objective::Filters::DateFilter, Objective::Filters::DecimalFilter, Objective::Filters::DuckFilter, Objective::Filters::FileFilter, Objective::Filters::FloatFilter, Objective::Filters::HashFilter, Objective::Filters::IntegerFilter, Objective::Filters::ModelFilter, Objective::Filters::RootFilter, Objective::Filters::StringFilter, Objective::Filters::TimeFilter
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.
20
21
22
23
24
25
26
|
# File 'lib/objective/filter.rb', line 20
def initialize(key = nil, opts = {}, &block)
@key = key
@given_options = opts
@sub_filters ||= []
instance_eval(&block) if block_given?
end
|
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
17
18
19
|
# File 'lib/objective/filter.rb', line 17
def key
@key
end
|
#sub_filters ⇒ Object
Returns the value of attribute sub_filters.
18
19
20
|
# File 'lib/objective/filter.rb', line 18
def sub_filters
@sub_filters
end
|
Class Method Details
.inherited(child_class) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/objective/filter.rb', line 5
def self.inherited(child_class)
filter_match = child_class.name.match(/\AObjective::Filters::(.*)Filter\z/)
raise "invalid class name for filter: #{child_class.name}" unless filter_match
filter_name = filter_match[1].gsub(/(.)([A-Z])/, '\1_\2').downcase
define_method(filter_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
115
116
117
|
# File 'lib/objective/filter.rb', line 115
def coerce(raw)
raw
end
|
#coerce_error(coerced) ⇒ Object
119
120
|
# File 'lib/objective/filter.rb', line 119
def coerce_error(coerced)
end
|
#default ⇒ Object
46
47
48
|
# File 'lib/objective/filter.rb', line 46
def default
options.default
end
|
#default? ⇒ Boolean
42
43
44
|
# File 'lib/objective/filter.rb', line 42
def default?
options.to_h.key?(:default)
end
|
#dup ⇒ Object
36
37
38
39
40
|
# File 'lib/objective/filter.rb', line 36
def dup
dupped = self.class.new
sub_filters.each { |sf| dupped.sub_filters.push(sf) }
dupped
end
|
#feed(raw) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/objective/filter.rb', line 50
def feed(raw)
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
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/objective/filter.rb', line 93
def feed_empty(raw, coerced)
case options.empty
when Objective::ALLOW
errors = nil
when Objective::DENY
errors = :empty
else
coerced = options.empty
end
feed_result(errors, raw, coerced)
end
|
#feed_invalid(errors, raw, coerced) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/objective/filter.rb', line 79
def feed_invalid(errors, raw, coerced)
case options.invalid
when Objective::ALLOW
errors = nil
when Objective::DENY
nil
else
errors = nil
coerced = options.invalid
end
feed_result(errors, raw, coerced)
end
|
#feed_nil ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/objective/filter.rb', line 63
def feed_nil
case options.nils
when Objective::ALLOW
errors = nil
coerced = nil
when Objective::DENY
errors = :nils
coerced = nil
else
errors = nil
coerced = options.nils
end
feed_result(errors, nil, coerced)
end
|
#feed_result(errors, raw, coerced) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/objective/filter.rb', line 106
def feed_result(errors, raw, coerced)
OpenStruct.new(
errors: errors,
raw: raw,
coerced: coerced,
inputs: coerced
)
end
|
#handle_errors(given_error) ⇒ Object
#options ⇒ Object
28
29
30
|
# File 'lib/objective/filter.rb', line 28
def options
@options ||= OpenStruct.new(self.class.const_get('Options').to_h.merge(@given_options))
end
|
#sub_filters_hash ⇒ Object
32
33
34
|
# File 'lib/objective/filter.rb', line 32
def sub_filters_hash
sub_filters.each_with_object({}) { |sf, result| result[sf.key] = sf }
end
|
#validate(coerced) ⇒ Object
122
123
|
# File 'lib/objective/filter.rb', line 122
def validate(coerced)
end
|