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:
id
type<String>
subdirectory<String>
filename<String>
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.
::MarkdownRecord::Base
provides several query methods to it's child classes. They are:
find
all
where
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:
where
: subsequent calls to this query method will merge the passed filters into the previously provided filter hash.not
: this method takes a hash of filters that is negated, so it will only return models that don't pass filters.fragmentize
: this method turns the association into a fragment association, meaning it will filter and return MarkdownRecord::ContentFragment
models only.to_fragments
: this method queries for MarkdownRecord models but returns their corresponding content fragments instead. This will often result in duplicates due to models being defined in the same file.all
: this method simply returns an Array of models, filtered according to whatever filters have been provided to the association object.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.
MarkdownRecord supports various types of filter values. Those types and what they mean are as follows:
Array
: the attribute value must be included in the array.Hash
: the attribute value must be a Hash which the filter value (a Hash of nested filters) will be applied against.nil
: the attribute value must be nil/null.Regexp
: the attribute value must match the Regex expression.:not_null
: the attribute value must not be nil/null.:null
: the attribute value must be nil/null.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 ...>
::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
: all models generated from files in the same directory and at the same level as the current model.class_siblings
: same as siblings
but only models of the same type.children
: all models generated from files in nested directories within the directory the current model is defined in.fragment
: the MarkdownRecord::ContentFragment
instance representing the file where the model was defined.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