nicotaing / yaml_record Goto Github PK
View Code? Open in Web Editor NEWPersisted data with yaml
License: MIT License
Persisted data with yaml
License: MIT License
Old property declaration is:
class Post < YamlRecord::Base
# Declare your properties
properties :title, :body, :user_id
end
which leaves the properties as whatever types they are inside the YAML. But we should support type casting as well:
class Post < YamlRecord::Base
# Declare your properties
property :title, String
property :body, String
property :user_id, Integer
property :enabled, Boolean
property :created_at, DateTime
end
This should typecast for String
, Integer
, DateTime
, and Boolean
. This means that it will serialize and deserialize and properly cast types:
p = Post.new(:title => "Test", :user_id => "6", :enabled => "1", :created_at => "6/18/2012")
p.title # => "Test"
p.user_id # => 6
p.enabled # => true
p.created_at # => <Time ...>
and also properly write types out:
---
title: "Test"
user_id : 6
enabled : true
created_at: "2011-11-05T08:15:30Z"
We should add association support:
class Post < YamlRecord::Base
belongs_to :episode
# auto-adds `property :episode_id, Integer`
end
This adds a episode_id
integer foreign key reference. You can then do stuff like:
p = Post.new
p.episode = Episode.find(5)
p.save
p.episode # => <Episode id=5>
p.episode_id # => 5
or:
p = Post.new
p.episode_id = 5
p.save
p.episode # => <Episode id=5>
p.episode_id # => 5
Also adds embeds_many
support:
class Post < YamlRecord::Base
embeds_many :users
# adds property :user_ids, Array, Integer
end
and allows for:
p = Post.new
p.user_ids = "1,2,3"
p.save
p.users # => [<User 1>, <User 2>, <User 3>]
Add validation support to yaml_record. Simplest will be to be able to define valid?
and then have it run on update_attributes
and save
:
class Post < YamlRecord::Base
def valid?
errors add :title, "cannot be blank" if title.blank?
errors.add :description, "cannot be blank" if self.description.blank?
end
end
and then:
p = Post.new.save => false
p.errors # => { :title => ["cannot be blank"], :description => ["cannot be blank"] }
We can also add helpers as well:
class Post < YamlRecord::Base
validates_presence_of :title
validates_presence_of :description
end
which will automatically set:
p = Post.new.save => false
p.errors # => { :title => ["cannot be blank"], :description => ["cannot be blank"] }
Once we have typecasting, we should support array as a type:
class Post < YamlRecord::Base
property :names, Array
property :user_ids, Array[Integer]
end
and that should allow:
p = Post.new
p.names = ["Bob", "Jane"]
p.user_ids = [1,2,3,4,5]
p.save
to
---
names: ["Bob", "Jane"]
user_id: [1,2,3,4,5]
but also support lists in string form:
p = Post.new
p.names = "Bob, Jane"
p.user_ids = "1, 2, 3, 4, 5"
p.save
p.names # => ["Bob", "Jane"]
p.user_ids # => [1,2,3,4,5]
We should add this project to travis-ci http://travis-ci.org/ . Register and account and follow the steps, its easy.
Hello,
i tried to add yaml_record to a rails 4.1 project and i got that error:
yaml_record (>= 0) ruby depends on
activesupport (~> 2.3.11) ruby
My activesupport version is actually 4.1.4
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.