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.



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

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#sub_filtersObject (readonly)

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

#defaultObject



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

def default
  options.default
end

#default?Boolean

Returns:

  • (Boolean)


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

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

#dupObject



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_nilObject



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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/objective/filter.rb', line 125

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



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_hashObject



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