Module: Ixtlan::Babel::HashFilter

Included in:
IdFilter, UpdatedAtFilter
Defined in:
lib/ixtlan/babel/hash_filter.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



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

def method_missing( name, *args )
  key = name.to_sym
  if self.class.hiddens.key?( key )
    to_attribute( key, 
                  @hidden[ key ] || @hidden[ key.to_s ], 
                  self.class.hiddens )
  elsif self.class.attributes.key?( key )
    to_attribute( key,
                  @attributes[ key ] || @attributes[ key.to_s ],
                  self.class.attributes )      
  else
    super
  end
end

Class Method Details

.included(model) ⇒ Object



6
7
8
# File 'lib/ixtlan/babel/hash_filter.rb', line 6

def self.included( model )
  model.extend( ClassMethods )
end

Instance Method Details

#attributesObject Also known as: params



18
19
20
# File 'lib/ixtlan/babel/hash_filter.rb', line 18

def attributes
  @attributes
end

#do_filter(data, ref) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ixtlan/babel/hash_filter.rb', line 52

def do_filter( data, ref )
  data.reject do |k,v|
    case v
    when Hash
      if babel = ref[ k.to_sym ]
        v.replace babel.replace( v ).attributes
        false
      else
        true
      end
    when ::Array
      if babel = ref[ k.to_sym ]
        v.each do |vv|
          vv.replace babel.replace( vv ).attributes
        end
        false
      else
        true
      end        
    else
      not ref.member?( k.to_sym ) 
    end
  end
end

#filter(data) ⇒ Object



10
11
12
# File 'lib/ixtlan/babel/hash_filter.rb', line 10

def filter( data )
  @attributes = do_filter( data, self.class.attributes )
end

#hidden(data) ⇒ Object



14
15
16
# File 'lib/ixtlan/babel/hash_filter.rb', line 14

def hidden( data )
  @hidden = do_filter( data, self.class.hiddens )
end

#initializeObject



77
78
79
80
# File 'lib/ixtlan/babel/hash_filter.rb', line 77

def initialize
  super()
  replace( {} )
end

#replace(data) ⇒ Object



82
83
84
85
86
87
# File 'lib/ixtlan/babel/hash_filter.rb', line 82

def replace( data )
  data = deep_dup( data )
  filter( data )
  hidden( data )
  self
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/ixtlan/babel/hash_filter.rb', line 23

def respond_to? name
  key = name.to_sym
  self.class.hiddens.key?( key ) || self.class.attributes.key?( key ) || super
end

#to_attribute(key, v, ref) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/ixtlan/babel/hash_filter.rb', line 43

def to_attribute( key, v, ref )
  case v
  when Hash
    ref[ key ].replace( v )
  else
    v
  end
end