How do I put datamapper definition into a external file - sinatra

I'm am working with datamapper in a classic Sinatra app and my file is getting to big to manage and troubleshoot. I was wondering if it would be possible to put my datamapper model definitions into one file the processing algorithms into another file and my call them both into my main index.rb. I've only been working with sinatra for a month or so but them must be a way to modularize your app into smaller maintainable files the classic way. right?
Here is the gist of what I'm trying to do:
Load sinatra app (index.rb).
The app loads the datamaper definitions (defineDB.rb).
The app loads the CRUD algorithms (proceesDB.rb).
So if a put request is received:
put '/protected/person/:id' do
#p = Person.first(:id => params[:id])
p.update(
#update table row
)
end

This is just basic Ruby, so you can use require or require_relative.
The exact organisation of your files is up to you, but here is my preference:
|- app.rb # This merely require_relative's all the other ruby-files, see below.
|- README.md
|- rackup.ru
|- lib/
| |- env.rb #contains settings and environment variables and such.
| |-
|- models/
| |- person.rb # Defines datamapper fields, helper and validators for Person.
|- controllers/
| |- person_controller.rb # Defines `put '/protected/person/:id' do` and other routing.
\- views/
|- layout.haml
\- person_show.haml
Again, it is up to you. And I would strongly advise against premature over-organisation. For example, the controllers could be omitted for as long as they fit well in app.rb. Once they grow out of there and you wish to split them up, only then introduce the controllers folder.
app.rb would be a file that does not much more then requiring all the libraries:
require "sinatra"
require "datamapper"
Dir.glob(File.join("{lib,models,controllers}", "*.rb")).each{|f| require File.realpath(f)}
This will load all files directly under lib, models and controllers. But you may want more control about when and how files are loaded; in that case, requiring them one-per-line is a better option.
I would advise against putting Datamapper definitions all in one file. You are probably better off when you group them as models: Person gets one file where all the definitions and crud actions for Person live. That way Person is isolated and self-contained, instead of spread over two files.

Related

Prevent dynamic owl:import in Jena OntModel

I'm trying to read an owl file in jena.
I would like related owl files specified by the property owl:import not being automatically imported.
Is that possible ?
I'm reading the model like this:
OntModel onto = ModelFactory.createOntologyModel();
onto.read(rdfURL);
with rdfURL being "https://<mymodel>.owl"
Here triples importing other owl files:
<rdf:RDF xml:base="http://www.ontologydesignpatterns.org/cp/owl/agentrole.owl">
<owl:Ontology rdf:about="http://www.ontologydesignpatterns.org/cp/owl/agentrole.owl">
<owl:imports rdf:resource="http://www.ontologydesignpatterns.org/cp/owl/objectrole.owl"/>
<owl:imports rdf:resource="http://www.ontologydesignpatterns.org/schemas/cpannotationschema.owl"/>
</owl:Ontology>
</rdf:RDF>
The reason for this is that i don't want Classes and Properties defined in imported models to be in the result of queries against the read one.
There are different way that may work here:
1 - Control imports via the OntModel document manager:
onto.getDocumentManager().setProcessImports(false);
before reading into the model.
2 - Depending on what information to make visible, it might be more useful to modify the data: read into a plain RDF model, remove the imports statements and then put into an OntModel.

Meteor, coffeescript files running on every page

I am just getting started with Meteor and have encountered something that isn't necessarily an issue but something that I just don't understand. I have the following code in a file called chat.coffee...
Meteor.setInterval ( ->
console.log "Hello " + roomName
Meteor.call('keepAlive', Meteor.user(), roomName)
return
), 5000
I originally was under the impression that coffee-script files only ran on their associated html files. This doesn't seem to be the case here as this code runs on every single page regardless of the file name. Is this the intended way things are supposed to work, and if so, is there a way to enforce that only certain code runs on certain pages.
One thing to mention is that this code is running in the client side folder.
On the client side, Meteor will associate your templates with their javascript functions and helpers based upon shared template names, but that is not inherently tied to your file names.
By way of example, if you have a template named "chat" in an html file as follows:
<template name="chat"></template>
Meteor will run scripts such as Template.chat.helpers({}) or Template.chat.events({}) only in connection with the "chat" template. But that is not dependent on your file naming conventions. It could be placed in a file name chat.js for organization and convention, but could equally well reside in a file named client.js or any other arbitrarily named .js file.
Similarly, your <template name="chat"> could reside in a file named chat.html, or client.html, or an arbitrary name of your choosing.
Your setInterval function is not tied to a specific template so it will run on every page, even if it resides in a file named chat.js.
Correct.
Meteor merges all your javascript ( via coffeescript ) and all the html, which it stores in its own special way. It merges all the html in heads and body etc into a page and serves that up, and it will then render templates as you specify.
To have a more "page" oriented app you can use something like iron router.

Merging doxygen modules

I have a large amount of code that I'm running doxygen against. To improve performance I'm trying to break it into modules and merge the result into one set of docs. I thought tag files would do the trick, but either I have it configured wrong or I'm misunderstanding how it works.
The directories are laid out:
root +
|-src+
| |-a
|
|-doc+
|-a.dox
|-main.dox
|-main.md
|-output+
|-a+
| |-html
|-main+
|-html
In addition to 'a' there are other peer directories but am starting with one.
a.dox generates output and a tag file into root/doc/output
OUTPUT_DIRECTORY=output/a
GENERATE_TAGFILE = output/a/a.tag
INPUT=../src/a
main.dox just inputs the markdown file that has a mainpage tag and refers to the other projects tag file.
OUTPUT_DIRECTORY=output/main
INPUT = main.md
TAGFILES=output/a/a.tag=output/a/html
Should this merge or link all the docs under main where I can browse 'a' globals, modules, pages, etc? Or does this only generate links to 'a' if I explicitly cross-reference a documented entity in 'a' from inside of 'main'?
If this should work, any thoughts on where my syntax is incorrect? I've tried various ways to define TAGFILES, is the output directory relative to the main.dox file? To the a.tag file? Or to the a/html directory?
If I'm off base an TAGFILES don't work this way, is there another way to merge sets of doxygen directories into one?
Thanks.
I suggest you read this topic on how I recommend to use tag files and the conditions that should apply: https://stackoverflow.com/a/8247993/784672
To answer your first question: doxygen will in general not merge the various index files together (then no performance would be gained). Although for a part you can still get external members in the index by setting ALLEXTERNALS to YES.
Doxygen will (auto)link symbols from other sources imported via a tag file. So in general you should divide your code into more or less self-contained modules/components/libraries, and if one such module depends on another, then import its tag file so that doxygen can link to the other documentation set. If you run doxygen twice (once for the tag file and once for the documentation) you can also resolve cyclic dependencies if you have them.
In my case I made a custom index page with links to all modules, and made a custom entry in the menu of each generated page that linked back to this index (see http://www.doxygen.nl/manual/customize.html#layout) how to add a user defined entry to the navigation menu/tree.

Entity Framework: Each type name in a schema must be unique

I'm doing a very non-standard build of Entity Framework. I've used EdmGen2 to generate edmx off of a db, and split the component csdl, msdl and ssdl files into their own files. The metadata in the connection string to them looks like this:
C:\Downloads\EDM | filename.csdl | filename.msdl | filename.ssdl
I have a unit test that does nothing but try to open the connection, and I get this error (along with a lot of other chaff):
"Each type name in a schema must be unique"
If I go into the csdl manually and add a "1" to the names, it eventually moves onto the msdl file and starts complaining about it. Clearly, somehow the schema is getting double-defined in the open operation...
There is no reference to the edmx in the test or dependent project. In fact, there are no references to any of those, as this is a project for generating all this stuff dynamically at run-time.
I've seen the Julie Lehrman / Don't Be Iffy post, and it doesn't appear to be that problem.
TIA...
Figured it out...the Metadata workspace is apparently hard coded to look for the three files (which makes sense), and when I removed the directory specification in the metadata tag, it all started working. My metadata attribute now looks like this:
C:\Downloads\filename.csdl | C:\Downloads\filename.msdl | C:\Downloads\filename.ssdl
So I think it's an either / or proposition: either specify the directory where the files are located, or the individual file locations.

Storing resources into "sub-bundle"?

I'd like to know if it is possible to store resources data into "sub-bundles" or "sub-packages" that I could put into my main AppBundle.
Indeed, I'd like to create a kind of player that reads "content packages", which are all organized the same way, with a standard hierarchical organization :
Package1:
- index.txt
- credits.txt
- Pictures/
-- Pic1.png
-- Pic2.png
- Movies/
-- intro.mov
-- outro.mov
My problem is that I can't find any way to make benefit of a hierarchical organisation into my Application package - I mean that I don't know how to distinguish "Folder1/index.txt" from "Folder2/index.txt", because I just use the "index.txt" identifier when I try to load the content of the file ...
I hope someone could help me, by the way I apologize for my poor english,
Cheers,
First, make sure that you're copying the files into subdirectories of Resources. One way to do this is to add your Package1 as a "folder reference" (select "Create Folder References" instead of "Recursively create groups" when you add it to the project). Another way to do it is to create a new Copy Files build action that copies the files into a subdirectory of Resources.
Once you've done that, you can use NSBundle's -pathForResource:ofType:inDirectory: to find the particular file you want. There's no need to go to the trouble of creating a sub-bundle (which is possible, but more complex).