MarkdownRecord Docs

MarkdownRecord Models

The ::MarkdownRecord::Base model is the base class for all models used for interacting with the JSON content that is rendered by MarkdownRecord. It has the following attributes:

The id attribute doesn't have a type, but for ::MarkdownRecord::ContentFragment models it will always be a string value equal to the relative path of the file the model represents. You can set it to any value for the models you define, but each model should have an id that is unique among models of the same type.

The type attribute is the fully qualified class name of the model in underscore form (i.e. ::MarkdownRecord::ContentFragment would be markdown_record/content_fragment).

subdirectory and filename always match the relative path of the file the model was defined in, or in the case of content fragments, the file the model represents. These attributes are auto-populated during the rendering process.

Query Methods

::MarkdownRecord::Base provides several query methods to it's child classes. They are:

These methods all work similarly to ActiveRecord by design. For example, if we use the models defined in the previously in this guide, then we could do this:

DslCommand.find(4)
 => #<DslCommand description: "end_model tells MarkdownRecord to pop the top model of the stack. This command isn't necessary unless you are defining models in a way that requires you to assign attributes to a model that is no longer the top model on the stack.", filename: "content_dsl", id: 4, name: "end_model", subdirectory: "content/api_docs", type: "dsl_command">

or

DslCommand.all
 => 
[#<DslCommand description: "...", name: "model", ...>,
 #<DslCommand description: "...", name: "attribute", ...>
 ...
]

The where query method takes an optional hash of filters and returns a MarkdownRecord::Association object, which you can chain additional methods onto. The methods you can chain onto this object are:

MarkdownRecord::Association also supports the to_a, each, map, count, any?, empty?, first, last, second, third and fourth methods which get executed on the backing Array after loading the data with the most recent filters. For additional functionality, you may call all or to_a to work with the backing Array directly.

Filters

MarkdownRecord supports various types of filter values. Those types and what they mean are as follows:

In addition, you can pass :__and__ or :__or__ as filter keys that point to arrays of filter hashes. In the former case, all filter hashes must pass, and in the later case, only one of the filter hashes must pass.

Finally, you can pass :__not__ as a filter key as well, which should point to a filter hash where all the filters must not pass.

Examples:

# querying with a string value
DslCommand.where(:name => "model").all
 => [#<DslCommand ...>]
# querying for models that don't have "fragment" in the name.
DslCommand.where(__not__: { :name => /fragment/}).map(&:name)
 => ["model", "attribute", "end_attribute", "end_model", "use_layout"]
DslCommand.where(__or__: [{ :name => /fragment/}, {:name => :null}]).map(&:name)
 => ["model", "fragment", "directory_fragment"]
MarkdownRecord::ContentFragment.where(:meta => {:author => "Bryant Morrill"}).first
 => #<MarkdownRecord::ContentFragment ...>

Automatic Associations

::MarkdownRecord::Base provides several associations automatically to its child classes, which are all based on their relative relative location to each other. These associations are:

siblings, class_siblings and children are all methods that return a MarkdownRecord::Association object. The fragment method returns a MarkdownRecord::ContentFragment instance.

The above content was rendered from source files at: content/v_0_1/model_basics