object-mapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "hash-utils", ">= 0.10.0"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ hash-utils (0.10.0)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.0.0)
17
+ hash-utils (>= 0.10.0)
18
+ jeweler (~> 1.5.2)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ Object Mapper
2
+ =============
3
+
4
+ **Object Mapper** provides interface for operations above set of objects
5
+ in single call. Binds method calls to all contained objects according to
6
+ specified rules. So, for example, it's possible to work with many hashes
7
+ by rather native way as with single one without necessity to merge them.
8
+ For example:
9
+
10
+ require "mapper"
11
+
12
+ h1 = {:a => :b}
13
+ h2 = {:c => :d}
14
+ h3 = {:a => :e}
15
+
16
+ hashes = Mapper[h1, h2, h3]
17
+ hashes.some.has_key? :a # will return true
18
+ hashes.some[:c] # will return :d
19
+
20
+ Conditions according which *some* reductor will return value can be
21
+ changed. (It simply converts returned value of hash to boolean and
22
+ returns it if conversion is `true`.) Let's change selector:
23
+
24
+ hashes.some{ |v| v == :e }[:a]
25
+
26
+ # will return :e, because :b conversion of first hash to boolean
27
+ # is false, so will continue to second and then to third hash where
28
+ # :a will return :e which is correct and therefore it will
29
+ # be returned
30
+
31
+ This example is slightly non-sense for practical use, of sure, but
32
+ demonstrates how selector works well. Second available reductor is
33
+ *all* which simply returns all results of method calls for each
34
+ hash (in array):
35
+
36
+ hashes.all.has_key? :a # will return [true, false, true]
37
+
38
+ Contributing
39
+ ------------
40
+
41
+ 1. Fork it.
42
+ 2. Create a branch (`git checkout -b 20101220-my-change`).
43
+ 3. Commit your changes (`git commit -am "Added something"`).
44
+ 4. Push to the branch (`git push origin 20101220-my-change`).
45
+ 5. Create an [Issue][2] with a link to your branch.
46
+ 6. Enjoy a refreshing Diet Coke and wait.
47
+
48
+ Copyright
49
+ ---------
50
+
51
+ Copyright © 2011 [Martin Kozák][3]. See `LICENSE.txt` for
52
+ further details.
53
+
54
+ [1]: http://optipng.sourceforge.net/
55
+ [2]: http://github.com/martinkozak/qrpc/issues
56
+ [3]: http://www.martinkozak.net/
57
+ [4]: http://rubyeventmachine.com/
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "object-mapper"
17
+ gem.homepage = "https://github.com/martinkozak/object-mapper"
18
+ gem.license = "MIT"
19
+ gem.summary = "Provides interface for operations above set of objects in single call. Binds method calls to all contained objects according to specified rules."
20
+ gem.email = "martinkozak@martinkozak.net"
21
+ gem.authors = ["Martin Kozák"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/rdoctask'
30
+ Rake::RDocTask.new do |rdoc|
31
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
32
+
33
+ rdoc.rdoc_dir = 'rdoc'
34
+ rdoc.title = "qrpc #{version}"
35
+ rdoc.rdoc_files.include('README*')
36
+ rdoc.rdoc_files.include('lib/**/*.rb')
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/mapper.rb ADDED
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "hash-utils/object"
5
+ require "hash-utils/array"
6
+ require "mapper/some"
7
+ require "mapper/all"
8
+
9
+ ##
10
+ # Represents mapped objects holder.
11
+ #
12
+
13
+ class Mapper < Array
14
+
15
+ ##
16
+ # ALL reductor instance.
17
+ #
18
+
19
+ @all
20
+
21
+ ##
22
+ # SOME reductor default instance.
23
+ #
24
+
25
+ @some
26
+
27
+ ##
28
+ # Alias for #new.
29
+ # @see {#initialize}
30
+ #
31
+
32
+ def self.[](*args)
33
+ self::new(*args)
34
+ end
35
+
36
+ ##
37
+ # Constructor.
38
+ #
39
+ # If only one argument, and it's Array, content will be treaten as
40
+ # matter for mapping.
41
+ #
42
+
43
+ def initialize(*objs)
44
+ if (objs.length == 1) and (objs.first.kind_of? Array)
45
+ self.merge! objs.first
46
+ else
47
+ self.merge! objs
48
+ end
49
+ end
50
+
51
+ ##
52
+ # Produces reductor which passes call with given arguments to
53
+ # each object.
54
+ #
55
+
56
+ def all
57
+ if @all.nil?
58
+ @all = All::new(self)
59
+ end
60
+
61
+ return @all
62
+ end
63
+
64
+ ##
65
+ # Produces reductor which passes call with given arguments to
66
+ # each object from oldest to newest in aggregator if the output of
67
+ # given block isn't +true+.
68
+ #
69
+
70
+ def some(&block)
71
+
72
+ # Returns from cache
73
+ if not @some.nil? and block.nil?
74
+ return @some
75
+ end
76
+
77
+ # In otherwise, creates new
78
+ default = false
79
+
80
+ if block.nil?
81
+ block = Proc::new { |v| v.to_b }
82
+ default = true
83
+ end
84
+
85
+ some = Some::new(self, &block)
86
+
87
+ if (default == true) and @some.nil?
88
+ @some = some
89
+ end
90
+
91
+ return some
92
+ end
93
+
94
+ end
data/lib/mapper/all.rb ADDED
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ ##
5
+ # Represents mapped objects holder.
6
+ #
7
+
8
+ class Mapper < Array
9
+
10
+ ##
11
+ # Reductor which simply returns array of results for all objects.
12
+ #
13
+
14
+ class All
15
+
16
+ ##
17
+ # Holds aggregated objects.
18
+ #
19
+
20
+ @objects
21
+
22
+ ##
23
+ # Constructor.
24
+ #
25
+
26
+ def initialize(objects)
27
+ @objects = objects
28
+ end
29
+
30
+ ##
31
+ # Handles calls. (Performs mapping.)
32
+ #
33
+
34
+ def method_missing(name, *args, &block)
35
+ result = @objects.map { |i| i.send(name, *args, &block) }
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ ##
5
+ # Represents mapped objects holder.
6
+ #
7
+
8
+ class Mapper < Array
9
+
10
+ ##
11
+ # "One after one" reductor.
12
+ #
13
+ # Passes call with given arguments to each object from oldest to
14
+ # newest in aggregator if the output of given block isn't +true+.
15
+ #
16
+
17
+ class Some
18
+
19
+ ##
20
+ # Holds aggregated objects.
21
+ #
22
+
23
+ @objects
24
+
25
+ ##
26
+ # Holds "some" condition.
27
+ #
28
+
29
+ @condition
30
+
31
+ ##
32
+ # Constructor.
33
+ #
34
+
35
+ def initialize(objects, &block)
36
+ @objects = objects
37
+ @condition = block
38
+ end
39
+
40
+ ##
41
+ # Handles calls. (Performs mapping.)
42
+ #
43
+ # Returns result of first one for which is condition +true+,
44
+ # or result of last object call.
45
+ #
46
+
47
+ def method_missing(name, *args, &block)
48
+ result = nil
49
+
50
+ @objects.each do |obj|
51
+ result = obj.send(name, *args, &block)
52
+
53
+ if @condition.call(result)
54
+ return result
55
+ end
56
+ end
57
+
58
+ return result
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{object-mapper}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Martin Kozák"]
12
+ s.date = %q{2011-02-23}
13
+ s.email = %q{martinkozak@martinkozak.net}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/mapper.rb",
27
+ "lib/mapper/all.rb",
28
+ "lib/mapper/some.rb",
29
+ "object-mapper.gemspec",
30
+ "test.rb"
31
+ ]
32
+ s.homepage = %q{https://github.com/martinkozak/object-mapper}
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.5.2}
36
+ s.summary = %q{Provides interface for operations above set of objects in single call. Binds method calls to all contained objects according to specified rules.}
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<hash-utils>, [">= 0.10.0"])
43
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
44
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
45
+ else
46
+ s.add_dependency(%q<hash-utils>, [">= 0.10.0"])
47
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<hash-utils>, [">= 0.10.0"])
52
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
54
+ end
55
+ end
56
+
data/test.rb ADDED
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ $:.push("./lib")
5
+ require "mapper"
6
+ require "riot"
7
+
8
+ context "Mapper" do
9
+ setup { Mapper[{:a => :b}, {:c => :d}] }
10
+ asserts("returns 'some' reductor") { topic.some.kind_of? Mapper::Some }
11
+ asserts("returns 'all' reductor") { topic.all.kind_of? Mapper::All }
12
+ end
13
+
14
+ context "Mapper::Some (default)" do
15
+ setup { Mapper[{:a => :b}, {:c => :d}].some }
16
+ asserts("existing key (should return true)") { topic.has_key?(:a) === true }
17
+ asserts("non-existing key (should return false)") { topic.has_key?(:d) === false }
18
+ asserts("non-existing pair under key (should return nil)") { topic[:d] === nil }
19
+ end
20
+
21
+ context "Mapper::Some (negative condition, special configuration)" do
22
+ setup do
23
+ h1 = {:a => :b}
24
+ h2 = {:c => :d}
25
+ h2.default = :default
26
+
27
+ Mapper[h1, h2].some { |v| !v }
28
+ end
29
+
30
+ asserts("existing key value (should return :default)") { topic[:a] === :default }
31
+ end
32
+
33
+ context "Mapper::All" do
34
+ setup { Mapper::new({:a => :b}, {:c => :d}).all }
35
+ asserts("existing key (should return [:b, nil])") { topic[:a] == [:b, nil] }
36
+ asserts("non-existing key (should return [nil, nil])") { topic[:d] == [nil, nil] }
37
+ end
38
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: object-mapper
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Martin Koz\xC3\xA1k"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-23 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hash-utils
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.10.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.2
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ description:
50
+ email: martinkozak@martinkozak.net
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE.txt
57
+ - README.md
58
+ files:
59
+ - .document
60
+ - Gemfile
61
+ - Gemfile.lock
62
+ - LICENSE.txt
63
+ - README.md
64
+ - Rakefile
65
+ - VERSION
66
+ - lib/mapper.rb
67
+ - lib/mapper/all.rb
68
+ - lib/mapper/some.rb
69
+ - object-mapper.gemspec
70
+ - test.rb
71
+ has_rdoc: true
72
+ homepage: https://github.com/martinkozak/object-mapper
73
+ licenses:
74
+ - MIT
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3178925756592808833
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.5.2
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Provides interface for operations above set of objects in single call. Binds method calls to all contained objects according to specified rules.
102
+ test_files: []
103
+