classx 0.0.5 → 0.0.6

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.
@@ -12,7 +12,7 @@ require 'classx/attributes'
12
12
  # end
13
13
  #
14
14
  # class Point3D < Point
15
- # has :z, :writable => true, :kind_of => Fixnum, :optional => true
15
+ # has :z, :kind_of => Fixnum, :optional => true
16
16
  # end
17
17
  #
18
18
  # Point.new(:x => 30, :y => 40) #=> <# Point @x=30, @y=40 >
@@ -181,7 +181,7 @@ module ClassX
181
181
  # It's because caching as instance_variable in @parent instance for performance.
182
182
  def set val
183
183
  val = self.class.coerce(val)
184
- raise ClassX::InvalidAttrArgument unless self.class.validate? val
184
+ raise ClassX::InvalidAttrArgument, "#{val.inspect} is not valid for #{self.inspect}" unless self.class.validate? val
185
185
  @data = val
186
186
 
187
187
  self.class.trigger(@parent, val)
@@ -82,10 +82,13 @@ module ClassX
82
82
  # class YourClass
83
83
  # include ClassX
84
84
  # add_attribute :x,
85
- # :writable => true, # defining accessor scope.
85
+ # :writable => false # defining accessor scope.
86
86
  # :optional => ture, # defining attribute is required in initialize.
87
87
  # :validate => proc {|val| val.respond_to? :to_s } # this attribute's value should adopted to this rule.
88
88
  # :default => proc {|mine| mine.class.to_s.split(/::/).last.downcase } # default value for this attribute.
89
+ # :handle => [ :foo, :bar, :baz ] # define method :foo, :bar, :baz to delegate x's method.
90
+ # :coerce => { String => proc {|val| val.to_i } } # when it set String value, force convert to Integer.
91
+ # :trigger => proc {|mine,val| mine.y = val } # when this attribute set value, also update y attribute.
89
92
  #
90
93
  # end
91
94
  #
@@ -118,12 +121,11 @@ module ClassX
118
121
 
119
122
  define_method "#{name}=" do |val|
120
123
  attr_instance = __send__ "attribute_of:#{name}"
121
- attr_instance.set val
122
124
  @__attribute_data_of ||= {}
123
- @__attribute_data_of[name] = val
125
+ @__attribute_data_of[name] = attr_instance.set val
124
126
  end
125
127
 
126
- unless attr_class.config[:writable]
128
+ if attr_class.config[:writable] == false
127
129
  private "#{name}="
128
130
  end
129
131
 
@@ -133,7 +133,7 @@ module ClassX
133
133
  @__class_attribute_data_of[name] = val
134
134
  end
135
135
 
136
- unless attr_class.config[:writable]
136
+ if attr_class.config[:writable] == false
137
137
  private "#{name}="
138
138
  end
139
139
 
@@ -20,20 +20,33 @@
20
20
  #
21
21
  module ClassX::Validate
22
22
  private
23
- def validate hash, &block #:doc:
23
+ def validate hash, options={}, &block #:doc:
24
24
  # FIXME: it's experimental feature for caching validate class.
25
25
  # it can use class variable because it use caller[0] as key.
26
- @@__validate_cached ||= {}
27
- uniq_key = caller[0]
28
- unless @@__validate_cached[uniq_key]
29
- @@__validate_cached[uniq_key] = Class.new
30
- @@__validate_cached[uniq_key].class_eval do
26
+ if options[:cache_key] != false
27
+ options[:cache_key] = caller[0]
28
+ end
29
+
30
+ if ( options[:cache_key] )
31
+ @@__validate_cached ||= {}
32
+ klass = @@__validate_cached[options[:cache_key]]
33
+ else
34
+ klass = nil
35
+ end
36
+
37
+ unless klass
38
+ klass = Class.new
39
+ klass.class_eval do
31
40
  include ::ClassX
32
41
  include ::ClassX::Bracketable
33
42
  end
34
- @@__validate_cached[uniq_key].class_eval(&block)
43
+ klass.class_eval(&block)
44
+
45
+ if options[:cache_key]
46
+ @@__validate_cached[options[:cache_key]] = klass
47
+ end
35
48
  end
36
- @@__validate_cached[uniq_key].new hash
49
+ klass.new hash
37
50
  end
38
51
 
39
52
  module_function :validate
@@ -90,7 +90,7 @@ describe ClassX do
90
90
  @class.class_eval do
91
91
  include ClassX
92
92
 
93
- has :x, :isa => Integer, :coerce => { String => proc {|val| val.to_i } }
93
+ has :x, :isa => Integer, :coerce => { String => proc {|val| val.to_i } }, :writable => true
94
94
  end
95
95
  end
96
96
 
@@ -101,6 +101,12 @@ describe ClassX do
101
101
  it 'attrubute :x should not convert Object to Integer' do
102
102
  lambda { @class.new(:x => Object.new ) }.should raise_error(ClassX::InvalidAttrArgument)
103
103
  end
104
+
105
+ it 'should convert Str to Integer when you rewrite attribute :x' do
106
+ instance = @class.new(:x => "10")
107
+ lambda { instance.x = "20" }.should_not raise_error(Exception)
108
+ instance.x.should == 20
109
+ end
104
110
  end
105
111
  end
106
112
  end
@@ -17,8 +17,10 @@ describe ClassX do
17
17
  lambda { @class.new }.should raise_error(ClassX::AttrRequiredError)
18
18
  end
19
19
 
20
- it 'should define #x= private method to class' do
21
- @class.private_instance_methods.map {|meth| meth.to_s }.should include("x=")
20
+ it 'should be able to rewrite :x attribute' do
21
+ instance =@class.new(:x => 10)
22
+ instance.x = 20
23
+ instance.x.should == 20
22
24
  end
23
25
  end
24
26
  end
@@ -13,8 +13,9 @@ describe ClassX::ClassAttributes do
13
13
  end
14
14
  end
15
15
 
16
- it 'should define class.x= private method to class' do
17
- @class.private_methods.map {|meth| meth.to_s }.should include("x=")
16
+ it 'should be able to rewrite :x attribute' do
17
+ @class.x = 10
18
+ @class.x.should == 10
18
19
  end
19
20
  end
20
21
  end
@@ -33,7 +33,7 @@ describe ClassX::ClassAttributes do
33
33
  # => This exception was caused by mistake of program. So, in general I think, you should not
34
34
  # rascue this error.
35
35
  it 'should raise RuntimeError using attr_name(val)' do
36
- pending "I don't know how to know call this method from class context or reciever context." do
36
+ pending do
37
37
  lambda { @class.x(20) }.should raise_error(RuntimeError)
38
38
  end
39
39
  end
@@ -2,7 +2,26 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
  require 'classx'
3
3
  require 'classx/validate'
4
4
 
5
- describe ClassX::Validate do
5
+ describe ClassX::Validate, :shared => true do
6
+ it 'should raise ArgumentError without param is not kind of Hash instance' do
7
+ lambda { @class.new.run([]) }.should raise_error(ArgumentError)
8
+ end
9
+
10
+ it 'should raise ClassX::AttrRequiredError' do
11
+ lambda { @class.new.run({}) }.should raise_error(ClassX::AttrRequiredError)
12
+ end
13
+
14
+ it 'should raise ClassX::InvalidArgumentError with InvalidAttrArgument' do
15
+ lambda { @class.new.run(:x => 'hoge', :y => 'fuga') }.should raise_error(ClassX::InvalidAttrArgument)
16
+ end
17
+
18
+ it 'should not raise Exception' do
19
+ lambda { @class.new.run(:x => 10, :y => 20) }.should_not raise_error(Exception)
20
+ end
21
+
22
+ end
23
+
24
+ describe ClassX::Validate, "with cache" do
6
25
  before do
7
26
  @class = Class.new
8
27
  @class.class_eval do
@@ -16,23 +35,30 @@ describe ClassX::Validate do
16
35
  end
17
36
  end
18
37
 
19
- it 'should raise ArgumentError without param is not kind of Hash instance' do
20
- lambda { @class.new.run([]) }.should raise_error(ArgumentError)
21
- end
38
+ it_should_behave_like "ClassX::Validate"
22
39
 
23
- it 'should raise ClassX::AttrRequiredError' do
24
- lambda { @class.new.run({}) }.should raise_error(ClassX::AttrRequiredError)
40
+ it 'should be cached auto generated class' do
41
+ @class.new.run(:x => 10, :y => 20).class.should == @class.new.run(:x => 11, :y => 21).class
25
42
  end
43
+ end
26
44
 
27
- it 'should raise ClassX::InvalidArgumentError with InvalidAttrArgument' do
28
- lambda { @class.new.run(:x => 'hoge', :y => 'fuga') }.should raise_error(ClassX::InvalidAttrArgument)
29
- end
45
+ describe ClassX::Validate, "with no cache" do
46
+ before do
47
+ @class = Class.new
48
+ @class.class_eval do
30
49
 
31
- it 'should not raise Exception' do
32
- lambda { @class.new.run(:x => 10, :y => 20) }.should_not raise_error(Exception)
50
+ def run params
51
+ ClassX::Validate.validate params, :cache_key => false do
52
+ has :x
53
+ has :y, :kind_of => Integer
54
+ end
55
+ end
56
+ end
33
57
  end
34
58
 
35
- it 'should be cached auto generated class' do
36
- @class.new.run(:x => 10, :y => 20).class.should == @class.new.run(:x => 11, :y => 21).class
59
+ it_should_behave_like "ClassX::Validate"
60
+
61
+ it 'should not be cached auto generated class' do
62
+ @class.new.run(:x => 10, :y => 20).class.should_not == @class.new.run(:x => 11, :y => 21).class
37
63
  end
38
64
  end
@@ -2,7 +2,7 @@ AUTHOR = "Keiji, Yoshimi"
2
2
  EMAIL = "walf443 at gmail.com"
3
3
  RUBYFORGE_PROJECT = "akasakarb"
4
4
  RUBYFORGE_PROJECT_ID = 4314
5
- HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
5
+ HOMEPATH = "http://walf443.github.com/classx/"
6
6
  RDOC_OPTS = [
7
7
  "--charset", "utf-8",
8
8
  "--opname", "index.html",
@@ -92,19 +92,24 @@ task :uninstall => [:clean] do
92
92
  sh %{sudo gem uninstall #{NAME}}
93
93
  end
94
94
 
95
-
96
- Rake::RDocTask.new do |rdoc|
97
- rdoc.rdoc_dir = 'html'
98
- rdoc.options += RDOC_OPTS
99
- rdoc.template = "resh"
100
- #rdoc.template = "#{ENV['template']}.rb" if ENV['template']
101
- if ENV['DOC_FILES']
102
- rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
103
- else
104
- rdoc.rdoc_files.include('README', 'ChangeLog')
105
- rdoc.rdoc_files.include('lib/**/*.rb')
106
- rdoc.rdoc_files.include('ext/**/*.c')
107
- end
95
+ begin
96
+ allison_path = `allison --path`.chomp
97
+ Rake::RDocTask.new do |rdoc|
98
+ rdoc.rdoc_dir = 'html'
99
+ rdoc.options += RDOC_OPTS
100
+ rdoc.template = allison_path
101
+ if ENV['DOC_FILES']
102
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
103
+ else
104
+ rdoc.rdoc_files.include('README', 'ChangeLog')
105
+ rdoc.rdoc_files.include('lib/**/*.rb')
106
+ rdoc.rdoc_files.include('ext/**/*.c')
107
+ end
108
+ end
109
+ rescue Exception => e
110
+ warn e
111
+ warn "skipping rdoc task"
112
+ ensure
108
113
  end
109
114
 
110
115
  desc "Publish to RubyForge"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keiji, Yoshimi
@@ -9,7 +9,7 @@ autorequire: ""
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-27 00:00:00 +09:00
12
+ date: 2008-12-26 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -78,6 +78,17 @@ files:
78
78
  - spec/classx_validate_spec.rb
79
79
  - spec/spec.opts
80
80
  - spec/spec_helper.rb
81
+ - doc/output
82
+ - doc/output/coverage
83
+ - doc/output/coverage/index.html
84
+ - doc/output/coverage/lib-classx-attribute_rb.html
85
+ - doc/output/coverage/lib-classx-attributes_rb.html
86
+ - doc/output/coverage/lib-classx-bracketable_rb.html
87
+ - doc/output/coverage/lib-classx-class_attributes_rb.html
88
+ - doc/output/coverage/lib-classx-commandable_rb.html
89
+ - doc/output/coverage/lib-classx-declare_rb.html
90
+ - doc/output/coverage/lib-classx-validate_rb.html
91
+ - doc/output/coverage/lib-classx_rb.html
81
92
  - bench/attribute_get.rb
82
93
  - bench/attribute_set.rb
83
94
  - bench/define_attribute.rb
@@ -86,7 +97,7 @@ files:
86
97
  - tasks/basic_config.rake
87
98
  - tasks/basic_tasks.rake
88
99
  has_rdoc: true
89
- homepage: http://akasakarb.rubyforge.org
100
+ homepage: http://walf443.github.com/classx/
90
101
  post_install_message:
91
102
  rdoc_options:
92
103
  - --charset
@@ -120,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
131
  requirements: []
121
132
 
122
133
  rubyforge_project: akasakarb
123
- rubygems_version: 1.2.0
134
+ rubygems_version: 1.3.0
124
135
  signing_key:
125
136
  specification_version: 2
126
137
  summary: Meta Framework extending and flexible attribute like Moose ( perl )