Module: Freeb::Models::ClassMethods
- Defined in:
- lib/freeb/models.rb
Instance Method Summary collapse
- #fcreate(freebase_id) ⇒ Object
- #fcreate_all ⇒ Object
- #fcreate_by_name(name) ⇒ Object
- #ffind_or_create(hash) ⇒ Object
- #fnew(freebase_id) ⇒ Object
- #fnew_by_name(name) ⇒ Object
- #freeb(&block) ⇒ Object
- #initialize_has_many_associations(options) ⇒ Object
- #initialize_topic_associations(options) ⇒ Object
Instance Method Details
#fcreate(freebase_id) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/freeb/models.rb', line 77 def fcreate(freebase_id) return nil if freebase_id.nil? return fcreate_with_array(freebase_id) if freebase_id.is_a?(Array) existing = find_by_freebase_id(freebase_id) return existing unless existing.blank? begin topic = Converter.freebase_id_to_topic(freebase_id, self) rescue ResponseException return nil end return nil if topic.blank? create(Converter.topic_to_record_attributes(topic, self)) end |
#fcreate_all ⇒ Object
108 109 110 |
# File 'lib/freeb/models.rb', line 108 def fcreate_all return fcreate([{}]) end |
#fcreate_by_name(name) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/freeb/models.rb', line 91 def fcreate_by_name(name) return nil if name.nil? if name.is_a?(Array) names = name return names.collect { |name| fcreate_by_name(name) } end existing = find_by_name(name) return existing unless existing.blank? begin topic = Converter.name_to_topic(name, self) rescue ResponseException return nil end return nil if topic.blank? create(Converter.topic_to_record_attributes(topic, self)) end |
#ffind_or_create(hash) ⇒ Object
112 113 114 |
# File 'lib/freeb/models.rb', line 112 def ffind_or_create(hash) find_or_create_by_freebase_id(hash) end |
#fnew(freebase_id) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/freeb/models.rb', line 63 def fnew(freebase_id) return nil if freebase_id.nil? return fnew_with_array(freebase_id) if freebase_id.is_a?(Array) topic = Converter.freebase_id_to_topic(freebase_id, self) new(Converter.topic_to_record_attributes(topic, self)) end |
#fnew_by_name(name) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/freeb/models.rb', line 70 def fnew_by_name(name) return nil if name.nil? return names.collect { |name| fnew_by_name(name) } if name.is_a?(Array) topic = Converter.name_to_topic(name, self) new(Converter.topic_to_record_attributes(topic, self)) end |
#freeb(&block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/freeb/models.rb', line 4 def freeb(&block) include InstanceMethods dsl = DSL.new dsl.instance_eval(&block) = ModelConfig.register(self, dsl.config) accessible_attributes = [:freebase_id, :name] accessible_attributes += ([:properties].merge([:topics]).merge([:has_many])).keys attr_reader :freeb_config attr_accessible *accessible_attributes validates_presence_of :freebase_id, :name initialize_topic_associations() initialize_has_many_associations() @freeb_config = ModelConfig.get(self) end |
#initialize_has_many_associations(options) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/freeb/models.rb', line 41 def initialize_has_many_associations() return if [:has_many].blank? [:has_many].each do |key, association| # Two-sided polymorphic has_many relationships are supported by Rails, so we'll do this with a HABTM and # custom insert SQL. Is there a better approach? join_table = "freebase_model_relations" has_and_belongs_to_many key, :join_table => join_table, :foreign_key => "subject_id", :association_foreign_key => "object_id", :conditions => { "#{join_table}.subject_type" => self.name, "#{join_table}.object_type" => association[:class_name], "#{join_table}.property" => association[:id] }, :insert_sql => proc { |object| %{INSERT INTO `#{join_table}` (`subject_type`, `subject_id`, `property`, `object_type`, `object_id`) VALUES ("#{self.class.name}", "#{id}", "#{association[:id]}", "#{association[:class_name]}", "#{object.id}" )} } end end |
#initialize_topic_associations(options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/freeb/models.rb', line 23 def initialize_topic_associations() return if [:topics].blank? [:topics].each do |key, value| join_association = :"#{key}_freebase_topic_relations" has_many join_association, :as => :subject, :class_name => "FreebaseTopicRelation", :conditions => {:property => value[:id]}, :before_add => :"before_add_#{join_association}" define_method :"before_add_#{join_association}" do |record| record.property = value[:id] end has_many key, :through => join_association, :source => :freebase_topic join_association = :"freebase_topic_relations_#{key}" FreebaseTopic.has_many join_association, :class_name => "FreebaseTopicRelation", :conditions => {:property => value[:id]} FreebaseTopic.has_many self.name.tableize, :through => join_association, :source => :subject, :source_type => self.name end end |