https://github.com/fixate/schemaless_field
Basic accessor methods for schemaless ORM fields.
https://github.com/fixate/schemaless_field
Last synced: 2 months ago
JSON representation
Basic accessor methods for schemaless ORM fields.
- Host: GitHub
- URL: https://github.com/fixate/schemaless_field
- Owner: fixate
- License: mit
- Created: 2014-05-29T13:18:08.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-10-15T14:05:03.000Z (over 10 years ago)
- Last Synced: 2025-01-04T08:37:14.414Z (4 months ago)
- Language: Ruby
- Size: 164 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# SchemalessField
Basic accessor methods for schemaless ORM fields.
For e.g. a JSON field in Postgres.## Installation
Add this line to your application's Gemfile:
gem 'schemaless_field'
And then execute:
$ bundle
Or install it yourself as:
$ gem install schemaless_field
## Usage
```ruby
class Item < ActiveRecord::Base # Any class with a json string or hash can be used
after_initialize :set_dataschemaless_field :data do
f.field :color, '$..color' # explicit path (not required)
f.field :array # implicit path
f.field :nested_field # nested implicit path i.e: $..nested.fieldf.first_thing, '$..things[0]' # explicit path
endprivate
def set_data
self.data ||= {
color: 'red',
nested: {
field: true
},
things: [
{name: 'car'},
{name: 'laptop'}
]
}
endend
```Now some field accessors will be available
```ruby
item = Item.new
item.color #= "red"
item.color = "blue" # data = { 'color' => 'blue', ... }
item.nested_field # true
item.first_thing # "car"
```## Contributing
1. Fork it ( http://github.com//schemaless_field/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request