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.

Parameters:

  • the (Array<Symbol>)

    properties to dump to JSON



51
52
53
# File 'lib/pupa/models/model.rb', line 51

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.

Parameters:

  • the (Array<Symbol>)

    class' foreign keys



61
62
63
# File 'lib/pupa/models/model.rb', line 61

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.

Parameters:

  • the (Array<Symbol>)

    class' foreign objects



75
76
77
# File 'lib/pupa/models/model.rb', line 75

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.

Parameters:

  • value (Hash, String)

    a hash or a relative or absolute path



83
84
85
86
87
88
89
90
91
# File 'lib/pupa/models/model.rb', line 83

def schema=(value)
  self.json_schema = if Hash === value
    value
  elsif Pathname.new(value).absolute?
    JSON.load(File.read(value))
  else
    JSON.load(File.read(File.expand_path(File.join('..', '..', '..', 'schemas', "#{value}.json"), __dir__)))
  end
end