summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBryan Duxbury <bryanduxbury@apache.org>2010-02-18 20:28:27 +0000
committerBryan Duxbury <bryanduxbury@apache.org>2010-02-18 20:28:27 +0000
commit0e4920c6b892ef46ae491fb1c65157ee6aab1367 (patch)
treeea29c136b62147ed20558badea98c4478bf8f867 /lib
parentbecaf536211a699f1fb936752262fdb7bcd36126 (diff)
downloadthrift-0e4920c6b892ef46ae491fb1c65157ee6aab1367.tar.gz
THRIFT-708. rb: Is set checking methods
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@911557 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'lib')
-rw-r--r--lib/rb/Rakefile2
-rw-r--r--lib/rb/lib/thrift/struct.rb26
-rw-r--r--lib/rb/lib/thrift/union.rb37
-rw-r--r--lib/rb/spec/ThriftSpec.thrift1
-rw-r--r--lib/rb/spec/struct_spec.rb7
-rw-r--r--lib/rb/spec/union_spec.rb7
6 files changed, 59 insertions, 21 deletions
diff --git a/lib/rb/Rakefile b/lib/rb/Rakefile
index 5453d58aa..39334f04e 100644
--- a/lib/rb/Rakefile
+++ b/lib/rb/Rakefile
@@ -82,7 +82,7 @@ begin
p.summary = "Ruby libraries for Thrift (a language-agnostic RPC system)"
p.url = "http://incubator.apache.org/thrift/"
p.include_rakefile = true
- p.version = "0.2.2"
+ p.version = "0.2.3"
p.rubygems_version = ">= 1.2.0"
end
diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb
index caa90647a..48353d30c 100644
--- a/lib/rb/lib/thrift/struct.rb
+++ b/lib/rb/lib/thrift/struct.rb
@@ -145,13 +145,25 @@ module Thrift
diffs
end
- def self.field_accessor(klass, *fields)
- fields.each do |field|
- klass.send :attr_reader, field
- klass.send :define_method, "#{field}=" do |value|
- Thrift.check_type(value, klass::FIELDS.values.find { |f| f[:name].to_s == field.to_s }, field) if Thrift.type_checking
- instance_variable_set("@#{field}", value)
- end
+ def self.field_accessor(klass, field_info)
+ field_name_sym = field_info[:name].to_sym
+ klass.send :attr_reader, field_name_sym
+ klass.send :define_method, "#{field_info[:name]}=" do |value|
+ Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking
+ instance_variable_set("@#{field_name_sym}", value)
+ end
+ end
+
+ def self.generate_accessors(klass)
+ klass::FIELDS.values.each do |field_info|
+ field_accessor(klass, field_info)
+ qmark_isset_method(klass, field_info)
+ end
+ end
+
+ def self.qmark_isset_method(klass, field_info)
+ klass.send :define_method, "#{field_info[:name]}?" do
+ !self.send(field_info[:name].to_sym).nil?
end
end
diff --git a/lib/rb/lib/thrift/union.rb b/lib/rb/lib/thrift/union.rb
index 4db820eee..640e78b80 100644
--- a/lib/rb/lib/thrift/union.rb
+++ b/lib/rb/lib/thrift/union.rb
@@ -90,21 +90,32 @@ module Thrift
[self.class.name, @setfield, @value].hash
end
- def self.field_accessor(klass, *fields)
- fields.each do |field|
- klass.send :define_method, "#{field}" do
- if field == @setfield
- @value
- else
- raise RuntimeError, "#{field} is not union's set field."
- end
+ def self.field_accessor(klass, field_info)
+ klass.send :define_method, field_info[:name] do
+ if field_info[:name].to_sym == @setfield
+ @value
+ else
+ raise RuntimeError, "#{field_info[:name]} is not union's set field."
end
+ end
- klass.send :define_method, "#{field}=" do |value|
- Thrift.check_type(value, klass::FIELDS.values.find {|f| f[:name].to_s == field.to_s }, field) if Thrift.type_checking
- @setfield = field
- @value = value
- end
+ klass.send :define_method, "#{field_info[:name]}=" do |value|
+ Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking
+ @setfield = field_info[:name].to_sym
+ @value = value
+ end
+ end
+
+ def self.qmark_isset_method(klass, field_info)
+ klass.send :define_method, "#{field_info[:name]}?" do
+ get_set_field == field_info[:name].to_sym && !get_value.nil?
+ end
+ end
+
+ def self.generate_accessors(klass)
+ klass::FIELDS.values.each do |field_info|
+ field_accessor(klass, field_info)
+ qmark_isset_method(klass, field_info)
end
end
diff --git a/lib/rb/spec/ThriftSpec.thrift b/lib/rb/spec/ThriftSpec.thrift
index 84111c168..b497a6033 100644
--- a/lib/rb/spec/ThriftSpec.thrift
+++ b/lib/rb/spec/ThriftSpec.thrift
@@ -69,6 +69,7 @@ struct Foo {
5: map<i32, map<string, double>> complex,
6: set<i16> shorts = [5, 17, 239],
7: optional string opt_string
+ 8: bool my_bool
}
struct BoolStruct {
diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb
index 045b96359..4b46284ac 100644
--- a/lib/rb/spec/struct_spec.rb
+++ b/lib/rb/spec/struct_spec.rb
@@ -68,6 +68,13 @@ class ThriftStructSpec < Spec::ExampleGroup
StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>"
end
+ it "should offer field? methods" do
+ Foo.new.opt_string?.should be_false
+ Foo.new(:simple => 52).simple?.should be_true
+ Foo.new(:my_bool => false).my_bool?.should be_true
+ Foo.new(:my_bool => true).my_bool?.should be_true
+ end
+
it "should read itself off the wire" do
struct = Foo.new
prot = BaseProtocol.new(mock("transport"))
diff --git a/lib/rb/spec/union_spec.rb b/lib/rb/spec/union_spec.rb
index f39d03372..f563b0b22 100644
--- a/lib/rb/spec/union_spec.rb
+++ b/lib/rb/spec/union_spec.rb
@@ -153,5 +153,12 @@ class ThriftUnionSpec < Spec::ExampleGroup
My_union.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>"
end
+
+ it "should offer field? methods" do
+ My_union.new.some_enum?.should be_false
+ My_union.new(:some_enum => SomeEnum::ONE).some_enum?.should be_true
+ My_union.new(:im_true => false).im_true?.should be_true
+ My_union.new(:im_true => true).im_true?.should be_true
+ end
end
end