infochimps-labs / gorillib Goto Github PK
View Code? Open in Web Editor NEWGorillib: infochimps lightweight subset of ruby convenience methods
Home Page: http://infochimps.com
License: Other
Gorillib: infochimps lightweight subset of ruby convenience methods
Home Page: http://infochimps.com
License: Other
A simple line that used to work (taken from the internal documentation in lib/gorillib/model/base.rb)
:
class Person
include Gorillib::Model
field :name, String, :doc => 'Full name of person'
field :height, Float, :doc => 'Height in meters'
end
now breaks with an apocryphal error message:
ArgumentError: visibility must be one of [:public, :private, :protected]: got nil
from /Users/travis/.rvm/gems/ruby-1.9.2-p290/gems/gorillib-0.5.0/lib/gorillib/model/named_schema.rb:30:in `define_meta_module_method'
from /Users/travis/.rvm/gems/ruby-1.9.2-p290/gems/gorillib-0.5.0/lib/gorillib/model/schema_magic.rb:103:in `define_attribute_reader'
from /Users/travis/.rvm/gems/ruby-1.9.2-p290/gems/gorillib-0.5.0/lib/gorillib/model/field.rb:102:in `inscribe_methods'
from /Users/travis/.rvm/gems/ruby-1.9.2-p290/gems/gorillib-0.5.0/lib/gorillib/model/schema_magic.rb:27:in `field'
Looking at the documentation (README.md
, the classes in lib/gorillib/model
) yields no answer. Is this a "feature" or a bug?
I'm not going to dive in too deep, but I am getting nasty stack traces from deep within multi json when calling to_wire on models without having required 'gorillib/serialization/to_wire'. Flip seems to understand what is going on.
Here's the stacktrace. Ignore anything in /usr/local/flume/infomart/{app,src}, because the code was all fixed before being committed.
NoMethodError: undefined method `delete' for #JSON::Ext::Generator::State:0x22426f2d
current_adapter at /usr/local/flume/infomart/bundle/jruby/1.9/gems/multi_json-1.3.7/lib/multi_json.rb:105
dump at /usr/local/flume/infomart/bundle/jruby/1.9/gems/multi_json-1.3.7/lib/multi_json.rb:114
to_json at /usr/local/flume/infomart/bundle/jruby/1.9/gems/gorillib-0.4.2/lib/gorillib/model/serialization.rb:18
to_json at json/ext/GeneratorMethods.java:71
dump at /usr/local/flume/infomart/bundle/jruby/1.9/gems/multi_json-1.3.7/lib/multi_json/adapters/json_common.rb:11
dump at /usr/local/flume/infomart/bundle/jruby/1.9/gems/multi_json-1.3.7/lib/multi_json.rb:115
to_json at /usr/local/flume/infomart/bundle/jruby/1.9/gems/gorillib-0.4.2/lib/gorillib/model/serialization.rb:18
emit at /usr/local/flume/infomart/src/main/ruby/base_processor.rb:43
process at /usr/local/flume/infomart/app/processors/entry_funnel.rb:119
_append at /wukong-flume/framework.rb:86
!! 0 returns true and this is counterintuitive. If I have explicitly set a field to be a Boolean, receiving a 0 should be understood as false.
nil
but set" attributes. This differs from attributes.compact, which is surprising behavior.compact_attributes
, and...attributes
and then compact_attributes
give different behavior; this should be addressed by...attribute_set?
should return true
for a field with a default.
Bug atually filed by flip, so if TD disagrees he may follow up...
Run the following script
require 'gorillib/model'
class Foo
include Gorillib::Model
field :boolsy, :boolean
end
[true, false, 'true', 'false', '0', '1', 0, 1,].each do |thing|
puts [thing.inspect, Foo.receive(boolsy: thing).boolsy.to_s].join("\t")
end
Using latest Gorillib (0.5.0) I see
$ ruby bool_test.rb
true true
false false
"true" true
"false" false
"0" true
"1" true
0 true
1 true
Shouldn't the behavior be to default to interpret 0
and "0"
as false
and 1
and "1"
as true
? Or is the belief that this pattern is "not common or reliable enough to encode in a library"?
Positional arguments break when the last argument is a Hash:
# This does not work as expected
class Foo
include Gorillib::Model
field :name, String, position: 0
field :info, Hash, position: 1
end
f = Foo.new('joebob', { boring: true })
f.name
#=> "joebob"
f.info
#=> nil
This can be solved by either not having a Hash as the last positional argument, or by passing in the (apparently not optional) options hash whenever using the #new
method with positional args. Neither is a great alternative:
# Never put Hashes last in positional argument lists
class Foo
include Gorillib::Model
field :name, String, position: 1
field :info, Hash, position: 0 # Reorganize the positions to avoid problems
end
f = Foo.new({ boring: true }, 'joebob')
f.name
#=> "joebob"
f.info
#=> {:boring=>true}
# Or alternatively
class Foo
include Gorillib::Model
field :name, String, position: 0
field :info, Hash, position: 1
end
f = Foo.new('joebob', { boring: true }, {}) # Always remember this empty options hash
f.name
#=> "joebob"
f.info
#=> {:boring=>true}
The culprit is the #extract_options!
method here.
(paging @kornypoet)
Making deep_compact preserve type -- {}.deep_compact should be {} not nil, to remain parallel with .compact.
What happens to undefined extra args in the receiving hash?
class Foo
include Receiver
rcvr_accessor :name, String
end
Foo.receive( { :name=>"Travis", :age=>26 } )
=> #<Foo:0x10059be78 @name="Travis">
Where did :age go? @mrflip
Why is this? I want #receive!
to return self like #update_attributes
and .receive
but it returns nil instead. What is the reasoning behind this? Also, why does #update_attributes
NOT handle extra attributes? I have to work around all of these issues.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.