Class: Freebase::Base
- Inherits:
-
Api::FreebaseResult
- Object
- Api::FreebaseResult
- Freebase::Base
- Extended by:
- Api
- Defined in:
- lib/freebase.rb
Overview
This is the base class for all dynamically defined Freebase Types.
Constant Summary
Constants included from Api
Instance Attribute Summary
Attributes inherited from Api::FreebaseResult
Class Method Summary collapse
- .add_required_query_attributes(conditions) ⇒ Object
- .find(*args) ⇒ Object
-
.find_all(options = {}) ⇒ Object
Don’t to call this directly.
-
.find_first(options = {}) ⇒ Object
Don’t to call this directly.
- .freebase_type ⇒ Object
- .load_schema! ⇒ Object
- .schema_loaded? ⇒ Boolean
-
.to_s ⇒ Object
ActiveRecord:Base-like to_s for the class.
Instance Method Summary collapse
-
#method_missing(name, *args) ⇒ Object
access the properties of this object, lazy loading associations as required.
-
#reload ⇒ Object
(re)load all properties of this object.
-
#to_s ⇒ Object
If the object has a name, return it, otherwise the id.
Methods included from Api
handle_read_error, mqlread, service_url
Methods inherited from Api::FreebaseResult
#depluralize, #fb_type, #id, #initialize, #resultify, #resultify_array, #resultify_hash, #resultify_value, #value_type?
Constructor Details
This class inherits a constructor from Freebase::Api::FreebaseResult
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
access the properties of this object, lazy loading associations as required.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/freebase.rb', line 143 def method_missing(name,*args) if self.class.properties.has_key?(name) reload unless attributes.has_key?(name) resultify attributes[name] elsif self.class.properties.has_key?((singularized_name = name.to_s.singularize.to_sym)) reload unless attributes.has_key?(singularized_name) resultify attributes[singularized_name] else super end end |
Class Method Details
.add_required_query_attributes(conditions) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/freebase.rb', line 90 def self.add_required_query_attributes(conditions) case conditions when Array conditions.map! {|c| add_required_query_attributes(c)} when Hash if conditions.delete(:fb_object) conditions.reverse_merge!(:type => [], :id => nil) unless conditions.has_key?(:*) else conditions.reverse_merge!(:type => nil) unless conditions.has_key?(:*) end conditions.each {|k,v| add_required_query_attributes(v) unless k == :*} else conditions end end |
.find(*args) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/freebase.rb', line 79 def self.find(*args) = args. case args.first when :first raise ArgumentError.new("Too many arguments for find(:first)") if args.size > 1 find_first() when :all raise ArgumentError.new("Too many arguments for find(:all)") if args.size > 1 find_all() end end |
.find_all(options = {}) ⇒ Object
Don’t to call this directly. find(:all, options) will be dispatched here. This method is provided for extensibility
115 116 117 118 119 120 |
# File 'lib/freebase.rb', line 115 def self.find_all( = {}) query = .fetch(:conditions, {}).merge(:type => self.freebase_type, :name=>nil, :* => [{}]) query[:limit] = [:limit] if [:limit] add_required_query_attributes(query) mqlread([query], :raw => true).map{|i| self.new(i)} end |
.find_first(options = {}) ⇒ Object
Don’t to call this directly. find(:first, options) will be dispatched here. This method is provided for extensibility
107 108 109 110 111 |
# File 'lib/freebase.rb', line 107 def self.find_first( = {}) conditions = .fetch(:conditions, {}).reverse_merge(:type => self.freebase_type, :name=>nil, :* => [{}], :limit => 1) add_required_query_attributes(conditions) self.new(mqlread(conditions, :raw => true)) end |
.freebase_type ⇒ Object
68 69 70 |
# File 'lib/freebase.rb', line 68 def self.freebase_type @freebase_type ||= self.name["Freebase::Types".length..self.name.length].underscore end |
.load_schema! ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/freebase.rb', line 71 def self.load_schema! self.properties = {} propobjs = mqlread(:type => '/type/type', :id => self.freebase_type, :properties => [{:name => nil, :id => nil, :type => nil, :expected_type => nil}]).properties propobjs.each {|propobj| self.properties[propobj.id.split(/\//).last.to_sym] = propobj } self.schema_loaded = true end |
.schema_loaded? ⇒ Boolean
65 66 67 |
# File 'lib/freebase.rb', line 65 def self.schema_loaded? self.schema_loaded || false end |
.to_s ⇒ Object
ActiveRecord:Base-like to_s for the class
123 124 125 126 127 128 129 |
# File 'lib/freebase.rb', line 123 def self.to_s if respond_to?(:properties) && !self.properties.blank? %Q{#<#{name} #{self.properties.map{|k,v| "#{k}:#{v.expected_type}"}.join(", ")}>} else "#<#{name}>" end end |
Instance Method Details
#reload ⇒ Object
(re)load all properties of this object
132 133 134 135 136 137 138 139 140 |
# File 'lib/freebase.rb', line 132 def reload query = {:id => self.id, :type=>self.class.freebase_type, :name=> nil, :limit => 1} self.class.properties.each do |k,v| query[k] = [{}] unless query.has_key?(k) end @result = self.class.mqlread(query, :raw => true).symbolize_keys! Freebase::Api::Logger.trace { @result.inspect } return self end |
#to_s ⇒ Object
If the object has a name, return it, otherwise the id.
156 157 158 |
# File 'lib/freebase.rb', line 156 def to_s respond_to?(:name) ? name : id end |