propertybuilder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ === 1.0.0 / 2008-03-25
2
+
3
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/property_builder.rb
6
+ lib/propertybuilder.rb
7
+ test/test_property_builder.rb
data/README.txt ADDED
@@ -0,0 +1,67 @@
1
+ = propertybuilder
2
+
3
+ * http://propertybuilder.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ Property Builder is a small library for building chains of symbols that will silently fail on nil. This is meant as an alternative to Object#andand and the ilk. This is mostly a theoretical approach, meant to encourage discussion.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ Two main components:
12
+
13
+ Object#is to build a PropertyBuilder chain
14
+
15
+ Object#build_on to build the PropertyBuilder on the object and return the specified property
16
+
17
+ == SYNOPSIS:
18
+ 
19
+ class TestClass
20
+ attr_accessor :attachment
21
+ end
22
+
23
+ class TestAttachment
24
+ def a_method(an_argument="argument")
25
+ an_argument
26
+ end
27
+ end
28
+
29
+ @test = TestClass.new
30
+ @test.attachment = TestAttachment.new
31
+ # same as @test.attachment.a_method, but will fail silently if @test.attachment is nil
32
+ @test.build_on(is(:attachment, :a_method))
33
+ # same as @test.attachment.a_method("test_argument")
34
+ @test.build_on(is(:attachment, [:a_method, "test_argument"]))
35
+
36
+ == REQUIREMENTS:
37
+
38
+ ActiveSupport
39
+
40
+ == INSTALL:
41
+
42
+ gem install propertybuilder
43
+
44
+ == LICENSE:
45
+
46
+ (The MIT License)
47
+
48
+ Copyright (c) 2008 FIX
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining
51
+ a copy of this software and associated documentation files (the
52
+ 'Software'), to deal in the Software without restriction, including
53
+ without limitation the rights to use, copy, modify, merge, publish,
54
+ distribute, sublicense, and/or sell copies of the Software, and to
55
+ permit persons to whom the Software is furnished to do so, subject to
56
+ the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be
59
+ included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
65
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
66
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
67
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/property_builder'
6
+
7
+ Hoe.new('propertybuilder', PropertyBuilder::VERSION) do |p|
8
+ p.rubyforge_name = 'propertybuilder' # if different than lowercase project name
9
+ p.developer('Erik Peterson', 'erik@subwindow.com')
10
+ end
11
+
12
+ # vim: syntax=Ruby
@@ -0,0 +1,85 @@
1
+ module PropertyBuilder
2
+ VERSION = '1.0.0'
3
+ class PropertyList < Hash
4
+ attr_accessor :key_order
5
+ def initialize(&block)
6
+ @key_order = []
7
+ yield(self)
8
+ end
9
+
10
+ def each
11
+ @key_order.each do |key|
12
+ yield(key,self[key])
13
+ end
14
+ end
15
+
16
+ def first
17
+ self[@key_order.first]
18
+ end
19
+
20
+ def [](position)
21
+ if position.is_a?(Integer) && !@key_order.include?(position)
22
+ self[@key_order[position]]
23
+ else
24
+ super
25
+ end
26
+ end
27
+
28
+ def []= (key,val)
29
+ if not @key_order.include? key
30
+ @key_order << key
31
+ end
32
+ eval("
33
+ def #{key}(value=nil)
34
+ self[:#{key}]=(value.is_a?(Array) ? value.first : value)||is(:#{key})
35
+ end
36
+ ")
37
+ super key,val
38
+ end
39
+
40
+ def method_missing(name, *args)
41
+ self[name] = nil
42
+ self.send(name, args)
43
+ end
44
+ end
45
+
46
+ class PropertyDefinition
47
+ attr_accessor :chain, :options
48
+ def initialize(options)
49
+ @chain = options.select{|a|!a.is_a?(Hash)}
50
+ @options = options.last.is_a?(Hash) ? options.last : {}
51
+ end
52
+
53
+ def build(obj)
54
+ PropertyDefinition.build(obj, @chain.dup)
55
+ end
56
+
57
+ def self.build(level, chain)
58
+ if !chain.nil? && chain.is_a?(Array) && chain.size > 0
59
+ sender = chain.shift
60
+ if sender.is_a?(Array) #Only supports one argument for now
61
+ result = level.send(sender.first, sender.last)
62
+ elsif sender.is_a?(String)
63
+ result = level.instance_eval(sender)
64
+ else
65
+ result = level.send(sender)
66
+ end
67
+ chain.size == 0 ? result : (result.nil? ? result : build(result, chain))
68
+ elsif chain.is_a?(String)
69
+ chain
70
+ elsif chain.is_a?(Symbol)
71
+ level.send(chain)
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ class Object
78
+ def is(*args)
79
+ PropertyBuilder::PropertyDefinition.new(args)
80
+ end
81
+
82
+ def build_on(builder)
83
+ builder.build(self)
84
+ end
85
+ end
@@ -0,0 +1 @@
1
+ require 'property_builder'
@@ -0,0 +1,39 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/property_builder'
3
+
4
+ class TestClass
5
+ attr_accessor :attachment
6
+ end
7
+
8
+ class TestAttachment
9
+ def a_method(an_argument="argument")
10
+ an_argument
11
+ end
12
+ end
13
+
14
+ class PropertyBuilderTest < Test::Unit::TestCase
15
+ def setup
16
+ @test = TestClass.new
17
+ @test.attachment = TestAttachment.new
18
+ end
19
+
20
+ def test_object_is
21
+ chain = is(:attachment)
22
+ assert_equal PropertyBuilder::PropertyDefinition, chain.class
23
+ end
24
+
25
+ def test_chain_build_no_arguments
26
+ chain = is(:attachment, :a_method)
27
+ assert_equal "argument", chain.build(@test)
28
+ end
29
+
30
+ def test_chain_build_with_argument
31
+ chain = is(:attachment, [:a_method, "test_argument"])
32
+ assert_equal "test_argument", chain.build(@test)
33
+ end
34
+
35
+ def test_build_on
36
+ assert_equal "argument", @test.build_on(is(:attachment, :a_method))
37
+ end
38
+ end
39
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: propertybuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Erik Peterson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-25 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.1
23
+ version:
24
+ description: Property Builder is a small library for building chains of symbols that will silently fail on nil. This is meant as an alternative to Object#andand and the ilk. This is mostly a theoretical approach, meant to encourage discussion.
25
+ email:
26
+ - erik@subwindow.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.txt
39
+ - Rakefile
40
+ - lib/property_builder.rb
41
+ - lib/propertybuilder.rb
42
+ - test/test_property_builder.rb
43
+ has_rdoc: true
44
+ homepage: http://propertybuilder.rubyforge.org
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --main
48
+ - README.txt
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: propertybuilder
66
+ rubygems_version: 1.0.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Property Builder is a small library for building chains of symbols that will silently fail on nil
70
+ test_files:
71
+ - test/test_property_builder.rb