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.
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
#key ⇒ Object
Returns the value of attribute key.
22
23
24
|
# File 'lib/objective/filter.rb', line 22
def key
@key
end
|
#sub_filters ⇒ Object
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
120
121
122
|
# File 'lib/objective/filter.rb', line 120
def coerce(raw)
raw
end
|
#coerce_error(coerced) ⇒ Object
124
125
|
# File 'lib/objective/filter.rb', line 124
def coerce_error(coerced)
end
|
#default ⇒ Object
51
52
53
|
# File 'lib/objective/filter.rb', line 51
def default
options.default
end
|
#default? ⇒ Boolean
47
48
49
|
# File 'lib/objective/filter.rb', line 47
def default?
options.to_h.key?(:default)
end
|
#dup ⇒ Object
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
|
# File 'lib/objective/filter.rb', line 55
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
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/objective/filter.rb', line 98
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
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/objective/filter.rb', line 84
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/objective/filter.rb', line 68
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
111
112
113
114
115
116
117
118
|
# File 'lib/objective/filter.rb', line 111
def feed_result(errors, raw, coerced)
OpenStruct.new(
errors: errors,
raw: raw,
coerced: coerced,
inputs: coerced
)
end
|
#handle_errors(given_error) ⇒ Object
#options ⇒ Object
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_hash ⇒ Object
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
127
128
|
# File 'lib/objective/filter.rb', line 127
def validate(coerced)
end
|