Project

statics

0.01
No release in over 3 years
Low commit activity in last 3 years
Base class and modules for static models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 0.12
~> 12.0
~> 3.0
~> 0.66
~> 0.16

Runtime

~> 1.0
~> 1.0
 Project Readme

Statics

Gem Depfu Inline docs Maintainability Test Coverage

Base class and modules for static models.

Links:

Requirements

  1. Ruby 3.0.0

Installation

To install, run:

gem install statics

Or add the following to your Gemfile:

gem "statics"

Usage

Setting the data path:

Statics.configure do |config|
  config.data_path = "data/"
end

Defining a static model:

class Post < Statics::Model
  filename "posts"

  attribute :title, Types::Strict::String
end
# data/posts.yml
---
post1:
  title: "Post 1"

post2:
  title: "Post 2"
Post.all
#=> #<Statics::Collection records=[#<Post key=:post1 title="Post 1">, #<Post key=:post2 title="Post 2">]>
Post.where(title: "Post 1")
#=> #<Statics::Collection records=[#<Post key=:post1 title="Post 1">]>
Post.where_not(title: "Post 1")
#=> #<Statics::Collection records=[#<Post key=:post2 title="Post 2">]>
Post.find_by(key: :post1)
#=> #<Post key=:post1 title="Post 1">
Post[:post1]
#=> #<Post key=:post1 title="Post 1">
Post.pluck(:title)
#=> ["Post 1", "Post 2"]
post = Post.first
#=> #<Post key=:post1 title="Post 1">
post.key
#=> :post1
post.title
#=> "Post 1"
post.attributes
#=> {:title=>"Post 1", :key=>:post1}

Defining translatable attributes:

class Post < Statics::Model
  include Statics::Translatable

  filename "posts"

  attribute :title, Types::Strict::String
  translatable_attribute :body
end
# data/posts.yml
---
post1:
  title: "Post 1"
  body:
    en: "Hello!"
    nl: "Hallo!"

post2:
  title: "Post 2"
  body:
    en: "Bye!"
    nl: "Doei!"
post = Post.first
# when I18n.locale is :en
post.body #=> "Hello!"
post.body(locale: :nl) #=> "Hallo!"

Defining omittable attributes with defaults:

class Post < Statics::Model
  include Statics::Translatable

  filename "posts"

  attribute :title, Types::Strict::String
  # With default
  attribute? :author, Types::Strict::String.default("Unknown")
  # Without default
  # attribute? :author, Types::Strict::String
end
# data/posts.yml
---
post1:
  title: "Post 1"
  author: "Rick Sanchez"

post2:
  title: "Post 2"
post1 = Post.first
post1.author #=> "Rick Sanchez"
post2 = Post.last
post2.author #=> "Unknown"

Check dry-types for documentation about the built-in types.

Caveats

If you have dates in your yaml-files, use the following format for them to be handled properly: YYYY-MM-DD

Tests

To test, run:

bundle exec rspec spec/

Versioning

Read Semantic Versioning for details. Briefly, it means:

  • Major (X.y.z) - Incremented for any backwards incompatible public API changes.
  • Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
  • Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.

License

Copyright 2018 Pablo Crivella. Read LICENSE for details.