Module: Pupa::Model::ClassMethods

Defined in:
lib/pupa/models/model.rb

Instance Method Summary collapse

Instance Method Details

#dump(*attributes) ⇒ Object

Declare which properties should be dumped to JSON after a scraping task is complete. A subset of these will be imported to the database.



48
49
50
# File 'lib/pupa/models/model.rb', line 48

def dump(*attributes)
  self.properties += attributes # use assignment to not overwrite the parent's attribute
end

#foreign_key(*attributes) ⇒ Object

Declare the class' foreign keys.

When importing scraped objects, the foreign keys will be used to draw a dependency graph and derive an evaluation order.



58
59
60
# File 'lib/pupa/models/model.rb', line 58

def foreign_key(*attributes)
  self.foreign_keys += attributes
end

#foreign_object(*attributes) ⇒ Object

Declare the class' foreign objects.

If some cases, you may not know the ID of an existing foreign object, but you may have other information to identify the object. In that case, put the information you have in a property named after the foreign key without the _id suffix: for example, person for person_id. Before saving the object to the database, Pupa.rb will use this information to identify the foreign object.



72
73
74
# File 'lib/pupa/models/model.rb', line 72

def foreign_object(*attributes)
  self.foreign_objects += attributes
end

#schema=(value) ⇒ Object

Note:

JSON::Validator#initialize_schema runs fastest if given a hash.

Sets the class' schema.



80
81
82
83
84
85
86
# File 'lib/pupa/models/model.rb', line 80

def schema=(value)
  self.json_schema = value
  self.validator = JSON::Validator.new(self.json_schema, {}, {
    clear_cache: false,
    parse_data: false,
  })
end