ldap-relations 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/felixclack/ldap-relations.png)](https://travis-ci.org/felixclack/ldap-relations)
4
4
 
5
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/felixclack/ldap-relations)
6
+
5
7
  An Arel inspired library to provide a relational algebra for LDAP filters.
6
8
  I'm using it in conjunction with the jruby-ldap library because the
7
9
  ruby ldap libraries just don't cut it in Ruby 1.9.
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task :travis do
4
+ puts "Starting to run rspec spec"
5
+ system "bundle exec rspec spec"
6
+ raise "rspec spec failed!" unless $?.exitstatus == 0
7
+ end
8
+
9
+ task :default => :travis
@@ -18,4 +18,5 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'rake'
21
22
  end
@@ -1,6 +1,8 @@
1
1
  require "ldap-relations/version"
2
2
  require 'ldap-relations/relation_manager'
3
3
  require 'ldap-relations/relation'
4
+ require 'ldap-relations/and'
5
+ require 'ldap-relations/or'
4
6
 
5
7
  module Ldap
6
8
  module Relations
@@ -3,14 +3,13 @@ module Ldap::Relations
3
3
  # to be passed to an LDAP server as part of a search.
4
4
  class Relation
5
5
  def initialize params={}
6
- @operator = params.fetch :operator, :and
7
6
  @params = params
8
7
  end
9
8
 
10
- attr_accessor :operator, :params
9
+ attr_accessor :params
11
10
 
12
11
  def to_filter
13
- @params.map {|key, value| "#{key}=#{value}" }.join
12
+ "()".insert 1, @params.map {|key, value| "#{key}=#{value}" }.join
14
13
  end
15
14
  end
16
15
  end
@@ -3,30 +3,23 @@ module Ldap
3
3
  # Store a list of relations and provide methods
4
4
  # to output the list as an LDAP filter string.
5
5
  class RelationManager
6
- RELATION_OPERATOR_MAPPINGS = {
7
- or: '|',
8
- and: '&',
9
- not: '!'
10
- }
11
-
12
6
  def initialize *args
13
- @relations = []
7
+ self.relations = RelationGrouping.new
14
8
  end
15
9
 
16
10
  attr_accessor :relations
17
11
 
18
- # Public: convert the relations to a LDAP-style filter string.
12
+ # Public: convert the relations to an LDAP-style filter string.
19
13
  #
20
14
  # Returns String
21
15
  def to_filter
22
- relation = @relations.shift
23
- case @relations.size
24
- when 0
25
- "(#{relation.to_filter})"
26
- when 1
27
- "(" << RELATION_OPERATOR_MAPPINGS[@relations.first.operator] <<
28
- "(#{relation.to_filter})(#{@relations.first.to_filter}))"
29
- end
16
+ relations.to_filter
17
+ end
18
+ end
19
+
20
+ class RelationGrouping < Array
21
+ def to_filter
22
+ map(&:to_filter).join
30
23
  end
31
24
  end
32
25
  end
@@ -1,5 +1,5 @@
1
1
  module Ldap
2
2
  module Relations
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'ldap-relations'
3
+
4
+ # Example ActiveRecord style usage for LRel
5
+ #
6
+ # eg. Directory.where(sAMAccountName: 'test').all
7
+ class Directory
8
+ def self.where params
9
+ new.where(params)
10
+ end
11
+
12
+ def initialize params={}
13
+ self.manager = LRel::RelationManager.new
14
+ end
15
+
16
+ attr_accessor :manager
17
+
18
+ def where params
19
+ query = params.shift
20
+ manager.relations << LRel::Relation.new(query.first.to_s => query.last)
21
+ if !params.empty?
22
+ self.and(params)
23
+ end
24
+ self
25
+ end
26
+
27
+ def and params
28
+ query = params.shift
29
+ relation = LRel::Relation.new(query.first.to_s => query.last)
30
+ if params.any?
31
+ LRel::And.new(relation, self.and(params))
32
+ else
33
+ relation
34
+ end
35
+ end
36
+
37
+ def or params
38
+ manager.relations = LRel::Or.new(manager.relations, LRel::Relation.new(params))
39
+ self
40
+ end
41
+
42
+ def default_relation
43
+ LRel::Relation.new(objectCategory: 'person')
44
+ end
45
+
46
+ def to_filter
47
+ manager.relations = LRel::And.new(default_relation, manager.relations)
48
+ manager.to_filter
49
+ end
50
+
51
+ alias :all :to_filter
52
+ end
53
+
54
+ describe "integration" do
55
+ let(:directory) { Directory.where givenname: 'test' }
56
+
57
+ it 'combines the provided the filter with the default' do
58
+ directory.to_filter.should == "(&(objectCategory=person)(givenname=test))"
59
+ end
60
+
61
+ context 'chaining multiple or filters' do
62
+ it 'nests the filters in pairs' do
63
+ directory.or(sn: 'test').or(mail: 'test').to_filter.
64
+ should == "(&(objectCategory=person)(|(|(givenname=test)(sn=test))(mail=test)))"
65
+ end
66
+ end
67
+ end
@@ -4,8 +4,8 @@ require 'ldap-relations/relation_manager'
4
4
  module Ldap::Relations
5
5
  describe RelationManager do
6
6
  let(:relation_manager) { RelationManager.new }
7
- let(:relation) { stub 'Relation', to_filter: 'objectCategory=person', operator: :and }
8
- let(:or_relation) { stub 'Relation', to_filter: 'sAMAccountName=test', operator: :or }
7
+ let(:relation) { stub 'Relation', to_filter: '(objectCategory=person)' }
8
+ let(:or_relation) { stub 'Or', to_filter: '(|(sAMAccountName=test)(mail=test))' }
9
9
 
10
10
  describe '#relations' do
11
11
  it 'stores a list of relations' do
@@ -23,16 +23,10 @@ module Ldap::Relations
23
23
  it { should == "(objectCategory=person)" }
24
24
  end
25
25
 
26
- context 'with 2 relations' do
27
- before { relation_manager.relations << relation << relation }
26
+ context 'with a Relation grouping' do
27
+ before { relation_manager.relations << or_relation }
28
28
 
29
- it { should == "(&(objectCategory=person)(objectCategory=person))" }
30
- end
31
-
32
- context 'with an AND relation and an OR relation' do
33
- before { relation_manager.relations << relation << or_relation }
34
-
35
- it { should == "(|(objectCategory=person)(sAMAccountName=test))"}
29
+ it { should == "(|(sAMAccountName=test)(mail=test))" }
36
30
  end
37
31
  end
38
32
  end
@@ -12,23 +12,7 @@ module Ldap::Relations
12
12
  describe '#to_filter' do
13
13
  subject { relation.to_filter }
14
14
 
15
- it { should == 'sAMAccountName=test' }
16
- end
17
-
18
- describe '#operator' do
19
- context 'by default' do
20
- subject { relation.operator }
21
-
22
- it { should == :and }
23
- end
24
-
25
- context 'when set to :or' do
26
- let(:or_relation) { Relation.new sAMAccountName: 'test', operator: :or }
27
-
28
- subject { or_relation.operator }
29
-
30
- it { should == :or }
31
- end
15
+ it { should == '(sAMAccountName=test)' }
32
16
  end
33
17
  end
34
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ldap-relations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-07 00:00:00.000000000 Z
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  description: ''
31
47
  email:
32
48
  - felixclack@gmail.com
@@ -46,6 +62,7 @@ files:
46
62
  - lib/ldap-relations/relation_manager.rb
47
63
  - lib/ldap-relations/version.rb
48
64
  - spec/.rspec
65
+ - spec/ldap-relations/integration_spec.rb
49
66
  - spec/ldap-relations/relation_manager_spec.rb
50
67
  - spec/ldap-relations/relation_spec.rb
51
68
  - spec/spec_helper.rb
@@ -75,6 +92,7 @@ specification_version: 3
75
92
  summary: Abstraction of filters to use with an LDAP library.
76
93
  test_files:
77
94
  - spec/.rspec
95
+ - spec/ldap-relations/integration_spec.rb
78
96
  - spec/ldap-relations/relation_manager_spec.rb
79
97
  - spec/ldap-relations/relation_spec.rb
80
98
  - spec/spec_helper.rb