ObjectField
Support for object persistence.
Save the object to data store via instance method.
Installation
Add this line to your application's Gemfile:
gem 'object_field'
And then execute:
$ bundle
Or install it yourself as:
$ gem install object_field
Usage
Include ObjectField::Jsonizer
or ObjectField::Serializer
.
Jsonizer
.#jsonize
defines accessor of JSON field.
class InJsonValue
include ObjectField::Jsonizer
attr_accessor :field1_json, :field2, :field3_json
jsonize :field1_json, :field2
jsonize :field3_json, as: :attributes
end
Setter is save the value to JSON.
Getter returns parsed value.
Name of accessor can be specified with as
option.
It is set field1
, field2_as_json
if not specified.
object = InJsonValue.new
object.field1 = [1, 2, 3]
p object.field1_json # => "[1,2,3]"
object.field1.each {|i| p i}
Serializer
.#serialize
defines accessor of marshal data field.
Like marshal option of redis-object.
class InSerializedValue
include ObjectField::Serializer
attr_accessor :field_data
serialize :field_data
end
Setter is save the serialized value.
Can not be saved, Proc object and anonymous class. (To raise TypeError)
Getter returns deserialized value.
Name of accessor can be specified with as
option.
It is set field
if not specified.
class SerializedObject; end
object = InSerializedValue.new
object.field = SerializedObject.new
p object.field.class # => SerializedObject
Contributing
- Fork it ( https://github.com/i2bskn/object_field/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request