Is it possible - yes.
In Rails you have ActiveModel::Model
which is a module that includes validations, attribute assignment, form integration, polymorphic routing etc. Pretty much everything except the actual persistence layer which is usually provided by ActiveRecord. You can also pick and choose features by just including the submodules.
Starting with Rails 5 you also have ActiveModel::Attributes
which provides a public api to features that used to be internal. It provides type casting and a good way to define default values.
class MyModel
include ActiveModel::Model
include ActiveModel::Attributes # not included in the above
attribute :awesomeness, :integer, default: 0
# ...
end
But how do I use it with a JSON file?
You can create a class method or a separate respiratory class that loads and parses the JSON file and creates model instances:
class MyModel
include ActiveModel::Model
include ActiveModel::Attributes # not included in the above
attribute :awesomeness, :integer, default: 0
# ...
def self.all
data = JSON.parse(File.open('/path/to/file.json'))
data.map do |hash|
new(hash)
end
end
end
Is it a good idea?
Not really unless your use case is very simplistic. To find a single entry you have to parse and load the entire file into memory. And then you have to use the basic array manipulation from enumerable. Any sort of database system is optimized for doing this far more effectively.
If you are deploying to any kind of ephemeral file system like Heroku it has the same cons as SQLite. Any changes to the file will be completely overwritten regularly.
If you are deploying via git it also means that you have to check your data into your codebase. This is rarely a good idea as it greatly increases the churn and the amount of noise you have to sift through to find actual changes in the codebase.
Any nosql or sql database can really do this better.