fancystruct 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |s|
4
+ s.name = "fancystruct"
5
+ s.homepage = "http://github.com/quackingduck/fancystruct"
6
+ s.summary = "Like Struct, but fancy"
7
+ s.email = "myles@myles.id.au"
8
+ s.authors = ["Myles Byrne"]
9
+
10
+ s.add_development_dependency 'exemplor', '>= 2000.0.0'
11
+ end
12
+ Jeweler::GemcutterTasks.new
13
+ rescue LoadError
14
+ puts "Install jeweler to build gem"
15
+ end
16
+
17
+ task :default => [:examples]
18
+ task(:examples) { ruby "examples.rb" }
19
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.1
data/examples.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'exemplor'
2
+ require File.dirname(__FILE__)+'/lib/fancystruct'
3
+
4
+ eg.helpers do
5
+
6
+ def fn; "Myles" end
7
+ def ln; "Byrne" end
8
+ def em; "myles@myles.id.au" end
9
+
10
+ def set_attribs(fs)
11
+ fs.first_name = fn
12
+ fs.last_name = ln
13
+ end
14
+
15
+ def check_fancystruct(fs)
16
+ Check(fs.first_name).is(fn)
17
+ Check(fs.last_name).is(ln)
18
+ end
19
+
20
+ end
21
+
22
+ eg "the FancyStruct method returns a class" do
23
+ cls = FancyStruct :first_name, :last_name, :email
24
+ fs = cls.new
25
+ set_attribs fs
26
+ check_fancystruct fs
27
+ end
28
+
29
+ eg "takes a block for method definitions and such" do
30
+ cls = FancyStruct :first_name, :last_name, :email do
31
+ def full_name
32
+ first_name + ' ' + last_name
33
+ end
34
+ end
35
+ fs = cls.new
36
+ set_attribs fs
37
+ check_fancystruct fs
38
+ end
39
+
40
+ eg "the .new factory sets attributes via a single hash" do
41
+ cls = FancyStruct :first_name, :last_name, :email
42
+ fs = cls.new :first_name => fn, :last_name => ln, :email => em
43
+ check_fancystruct fs
44
+ end
45
+
46
+ eg "the .new factory sets attributes via an array of values" do
47
+ cls = FancyStruct :first_name, :last_name, :email
48
+ fs = cls.new fn, ln, em
49
+ check_fancystruct fs
50
+ end
51
+
52
+ class Person < FancyStruct :first_name, :last_name, :email
53
+
54
+ def full_name
55
+ first_name + ' ' + last_name
56
+ end
57
+
58
+ end
59
+
60
+ eg "can be declared with class-like syntax (plain old inheritance)" do
61
+ p = Person.new
62
+ set_attribs p
63
+ check_fancystruct p
64
+ end
65
+
66
+ eg "calling with a hash returns an instance of an anonymous FancyStruct" do
67
+ fs = FancyStruct :first_name => fn, :last_name => ln
68
+ check_fancystruct fs
69
+ end
@@ -0,0 +1,40 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
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{fancystruct}
8
+ s.version = "0.9.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Myles Byrne"]
12
+ s.date = %q{2009-11-10}
13
+ s.email = %q{myles@myles.id.au}
14
+ s.files = [
15
+ ".gitignore",
16
+ "Rakefile",
17
+ "VERSION",
18
+ "examples.rb",
19
+ "fancystruct.gemspec",
20
+ "lib/fancystruct.rb"
21
+ ]
22
+ s.homepage = %q{http://github.com/quackingduck/fancystruct}
23
+ s.rdoc_options = ["--charset=UTF-8"]
24
+ s.require_paths = ["lib"]
25
+ s.rubygems_version = %q{1.3.5}
26
+ s.summary = %q{Like Struct, but fancy}
27
+
28
+ if s.respond_to? :specification_version then
29
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
30
+ s.specification_version = 3
31
+
32
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
33
+ s.add_development_dependency(%q<exemplor>, [">= 2000.0.0"])
34
+ else
35
+ s.add_dependency(%q<exemplor>, [">= 2000.0.0"])
36
+ end
37
+ else
38
+ s.add_dependency(%q<exemplor>, [">= 2000.0.0"])
39
+ end
40
+ end
@@ -0,0 +1,41 @@
1
+ class FancyStruct
2
+
3
+ def self.attribs(*attribs)
4
+ @attribs ||= []
5
+ unless attribs.empty?
6
+ self.send :attr_accessor, *attribs
7
+ @attribs += attribs
8
+ end
9
+ @attribs
10
+ end
11
+
12
+ def initialize(*args)
13
+ if args.size == 1 && args.first.is_a?(Hash)
14
+ self.set(args.first)
15
+ else
16
+ self.class.attribs.each_with_index { |name, i| self.set_attrib(name, args[i]) if args[i] }
17
+ end
18
+ end
19
+
20
+ def set(attribs)
21
+ attribs.each { |name,val| self.set_attrib(name, val) }
22
+ self
23
+ end
24
+
25
+ def set_attrib(name,val)
26
+ self.send(name.to_s+'=', val)
27
+ end
28
+
29
+ end
30
+
31
+ def FancyStruct(*attribs, &blk)
32
+ if attribs.size == 1 && attribs.first.is_a?(Hash)
33
+ keys, values = attribs.first.to_a.transpose
34
+ FancyStruct(*keys).new(*values)
35
+ else
36
+ c = Class.new(FancyStruct)
37
+ c.attribs *attribs
38
+ c.class_eval(&blk) if blk
39
+ c
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fancystruct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Myles Byrne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-10 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: exemplor
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2000.0.0
24
+ version:
25
+ description:
26
+ email: myles@myles.id.au
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - .gitignore
35
+ - Rakefile
36
+ - VERSION
37
+ - examples.rb
38
+ - fancystruct.gemspec
39
+ - lib/fancystruct.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/quackingduck/fancystruct
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Like Struct, but fancy
68
+ test_files: []
69
+