Class: Babel::HashFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/babel/hash_filter.rb

Constant Summary collapse

NO_MODEL =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(context_or_options = nil) ⇒ HashFilter

Returns a new instance of HashFilter.



4
5
6
# File 'lib/babel/hash_filter.rb', line 4

def initialize(context_or_options = nil)
  use(context_or_options)
end

Instance Method Details

#[](key) ⇒ Object



25
26
27
# File 'lib/babel/hash_filter.rb', line 25

def [](key)
  context[key.to_sym] if key
end

#[]=(key, options) ⇒ Object



21
22
23
# File 'lib/babel/hash_filter.rb', line 21

def []=(key, options)
  context[key.to_sym] = options if key
end

#default_context_key(default = nil) ⇒ Object



16
17
18
19
# File 'lib/babel/hash_filter.rb', line 16

def default_context_key(default = nil)
  @default = default if default
  @default
end

#filter(hash = {}, model = NO_MODEL, &block) ⇒ Object



50
51
52
# File 'lib/babel/hash_filter.rb', line 50

def filter(hash = {}, model = NO_MODEL, &block)
  filter_data(model, hash, options, &block) if hash
end

#optionsObject



54
55
56
# File 'lib/babel/hash_filter.rb', line 54

def options
  @options || context[default_context_key] || {}
end

#use(context_or_options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/babel/hash_filter.rb', line 29

def use(context_or_options)
  if context_or_options
    case context_or_options
    when Symbol  
      if opts = context[context_or_options]
        @options = opts.dup
      end
    when Hash
      @options = context_or_options
    end
  else
    @options = nil
  end
  self
end