Class: Sitepress::Model
- Inherits:
-
Object
- Object
- Sitepress::Model
- Defined in:
- lib/sitepress/model.rb
Overview
Wraps a page in a class, which makes it much easier to decorate and validate.
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Class Method Summary collapse
-
.collection(name = Models::Collection::DEFAULT_NAME, glob:, **kwargs) ⇒ Object
Defines a class method that may be called later to return a collection of objects.
- .data(*keys, default: nil) ⇒ Object
-
.get(page) ⇒ Object
(also: find)
Wraps a page in a class if given a string that represents the path or a page object itself.
-
.glob(glob, **kwargs) ⇒ Object
Adhoc querying of models via ‘Model.glob(“foo/bar”).all`.
- .site ⇒ Object
Instance Method Summary collapse
-
#==(model) ⇒ Object
Treat as equal if the resource and model class are the same.
-
#initialize(page) ⇒ Model
constructor
A new instance of Model.
Constructor Details
#initialize(page) ⇒ Model
Returns a new instance of Model.
12 13 14 |
# File 'lib/sitepress/model.rb', line 12 def initialize(page) @page = page end |
Instance Attribute Details
#page ⇒ Object (readonly)
Returns the value of attribute page.
4 5 6 |
# File 'lib/sitepress/model.rb', line 4 def page @page end |
Class Method Details
.collection(name = Models::Collection::DEFAULT_NAME, glob:, **kwargs) ⇒ Object
Defines a class method that may be called later to return a collection of objects. The default glob, for example, is named ‘:all`, which defines `MyModel.all` on the class.
29 30 31 32 33 |
# File 'lib/sitepress/model.rb', line 29 def collection(name = Models::Collection::DEFAULT_NAME, glob:, **kwargs) define_singleton_method name do self.glob glob, **kwargs end end |
.data(*keys, default: nil) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/sitepress/model.rb', line 56 def data(*keys, default: nil) keys.each do |key| define_method key do self.data.fetch key.to_s, default end end end |
.get(page) ⇒ Object Also known as: find
Wraps a page in a class if given a string that represents the path or a page object itself.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sitepress/model.rb', line 42 def get(page) case page when Model page when String new site.get page when Sitepress::Resource new page else raise ModelNotFoundError, "#{self.inspect} could not find #{page.inspect}" end end |
.glob(glob, **kwargs) ⇒ Object
Adhoc querying of models via ‘Model.glob(“foo/bar”).all`
36 37 38 |
# File 'lib/sitepress/model.rb', line 36 def glob(glob, **kwargs) Models::Collection.new model: self, site: site, glob: glob, **kwargs end |
Instance Method Details
#==(model) ⇒ Object
Treat as equal if the resource and model class are the same.
17 18 19 |
# File 'lib/sitepress/model.rb', line 17 def ==(model) self.page == model.page and self.class == model.class end |