Skip to content

Commit

Permalink
filter should now be working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cfeduke committed Jan 24, 2012
1 parent bedf04e commit fa50a50
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/active_avro/complex/record.rb
Expand Up @@ -7,7 +7,7 @@ class Record
attr_accessor :name, :fields
attr_reader :node

def initialize(klass, node = nil)
def initialize(klass, node = nil, options = {})
@name = klass.name
@node = Tree::TreeNode.new((node.nil? ? klass.name : "#{node.name}\\#{klass.name}"), klass)
if node.nil?
Expand All @@ -19,7 +19,12 @@ def initialize(klass, node = nil)
if @node.node_depth > 0
return if @node.parentage.any?{ |n| n.content == klass }
end
@fields = []
filter = options[:filter] || Filter.new
if klass.respond_to?(:columns)
klass.columns.each do |c|
@fields << Field.from_column(c) if filter.exclude?(class: c.type, attribute: c.name)
end
@fields = klass.columns.map { |c| Field.from_column(c) }
end
if klass.respond_to?(:reflections)
Expand Down
8 changes: 8 additions & 0 deletions lib/active_avro/filter.rb
Expand Up @@ -5,6 +5,14 @@ class Filter < Hash
def initialize()
self.default_proc = Proc.new {|h,k| h[k] = []}
end
def exclude?(options = {})
false if empty? # shortcut
key = options[:class].to_s || '*'
value = options[:attribute] || '*'
is_match = Proc.new { |e| (e =~ value) != nil }
binding.pry if key == 'Person' && value == 'parent_id'
self[key].empty? || self[key].any?{|e| is_match.call(e)} || (key != '*' && self['*'].any?{|e| is_match.call(e)})
end
# the format is 'class#attribute'
# the attribute can be a regular expression string
# use * as a wildcard for class names, i.e. '*#created_by' to nix all created_by attributes
Expand Down
4 changes: 2 additions & 2 deletions lib/active_avro/schema.rb
Expand Up @@ -11,9 +11,9 @@ def initialize(klass, options = { })
raise ArgumentError.new("klass.columns must be an Array") unless klass.columns.is_a?(Array)
@klass = klass

filter = Filter.build(options[:filter] || [])


@record = ActiveAvro::Complex::Record.new(@klass)
@record = ActiveAvro::Complex::Record.new(@klass, nil, :filter => filter)
end

def to_json
Expand Down
29 changes: 27 additions & 2 deletions spec/active_avro/filter_spec.rb
Expand Up @@ -2,15 +2,40 @@

module ActiveAvro
describe Filter do
subject { Filter.build(['Person#created_at', 'Person#updated_at', 'Pet', '*#.*_id$', 'Blah#*']) }
describe 'self#build' do
subject { Filter.build(['Person#created_at', 'Person#updated_at', 'Pet', '*#.*_id', 'Blah#*']) }
it "should only have two elements for the 'Person' key" do
subject['Person'].length.should == 2
end
its(['Person']){ should include /created_at/ }
its(['Person']){ should include /updated_at/ }
its(['*']){ should include /.*_id/ }
its(['*']){ should include /.*_id$/ }
its(['Pet']){ should be_empty }
its(['Blah']){ should be_empty }
end

describe '#exclude?' do
it "should exclude class 'Blah'" do
subject.exclude?(:class => 'Blah').should be_true
end
it "should exclude class 'Pet'" do
subject.exclude?(:class => 'Pet').should be_true
end
it "should include class 'Person'" do
subject.exclude?(:class => 'Person').should be_false
end
it "should exclude any attribute ending in '_id'" do
subject.exclude?(:attribute => 'parent_id').should be_true
end
it "should exclude 'Person#created_at'" do
subject.exclude?(:class => 'Person', :attribute => 'created_at')
end
it "should not exclude 'Person#id'" do
subject.exclude?(:class => 'Person', :attribute => 'id')
end
it "should exclude 'Person#parent_id' due to '*#.*_id$'" do
subject.exclude?(:class => 'Person', :attribute => 'parent_id').should be_true
end
end

describe 'Filter::Entry' do
Expand Down

0 comments on commit fa50a50

Please sign in to comment.