Class: YamlRecord::Base
- Inherits:
-
Object
- Object
- YamlRecord::Base
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/yaml_record/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#is_created ⇒ Object
Returns the value of attribute is_created.
-
#is_destroyed ⇒ Object
Returns the value of attribute is_destroyed.
Class Method Summary collapse
-
.all ⇒ Object
Returns collection of all YamlRecord instances Caches results during request.
-
.create(attributes = {}) ⇒ Object
Initializes YamlRecord instance given an attribute hash and saves afterwards Returns instance if successfully saved; false otherwise.
-
.find_by_attribute(attribute, expected_value) ⇒ Object
Find YamlRecord instance given attribute name and expected value Supports checking inclusion for array based values Returns instance if found; false otherwise.
-
.find_by_id(value) ⇒ Object
(also: find)
Find YamlRecord instance given id Returns instance if found; false otherwise.
-
.first(limit = 1) ⇒ Object
Find first YamlRecord instance given a limit Returns an array of instances if found; empty otherwise.
-
.last(limit = 1) ⇒ Object
Find last YamlRecord instance given a limit Returns an array of instances if found; empty otherwise.
-
.properties(*names) ⇒ Object
Declares persisted attributes for YamlRecord class.
-
.source(file = nil) ⇒ Object
Declares source file for YamlRecord class.
Instance Method Summary collapse
-
#==(comparison_record) ⇒ Object
Overrides equality to match if matching ids.
-
#[](attribute) ⇒ Object
Accesses given attribute from YamlRecord instance.
-
#[]=(attribute, value) ⇒ Object
Assign given attribute from YamlRecord instance with specified value.
-
#column_names ⇒ Object
Returns array of instance attributes names; An attribute is a value stored for this record (persisted or not).
-
#destroy ⇒ Object
Remove a persisted YamlRecord object Returns true if destroyed; false otherwise.
-
#destroyed? ⇒ Boolean
Returns true if YamlRecord instance has been destroyed; false otherwise.
-
#initialize(attr_hash = {}) ⇒ Base
constructor
Constructs a new YamlRecord instance based on specified attribute hash.
-
#new_record? ⇒ Boolean
Returns true if YamlRecord instance hasn’t persisted; false otherwise.
-
#persisted_attributes ⇒ Object
Returns hash of attributes to be persisted to file.
-
#reload ⇒ Object
Reload YamlRecord instance attributes from file.
-
#save ⇒ Object
Saved YamlRecord instance to file Executes save and create callbacks Returns true if record saved; false otherwise.
-
#to_model ⇒ Object
Returns YamlRecord Instance Complies with ActiveModel api.
-
#to_param ⇒ Object
Returns the instance of a record as a parameter By default return an id.
-
#update_attributes(updated_attrs = {}) ⇒ Object
Update YamlRecord instance with specified attributes Returns true if record updated; false otherwise.
Constructor Details
#initialize(attr_hash = {}) ⇒ Base
Constructs a new YamlRecord instance based on specified attribute hash
Example:
class Post < YamlRecord::Base; properties :foo; end
Post.new(:foo => "bar")
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/yaml_record/base.rb', line 34 def initialize(attr_hash={}) attr_hash.symbolize_keys! attr_hash.reverse_merge!(self.class.properties.inject({}) { |result, key| result[key] = nil; result }) self.attributes ||= {} self.is_created = attr_hash.delete(:persisted) || false attr_hash.each do |k,v| self.send("#{k}=", v) # self.attributes[:media] = "foo" end @errors = ActiveModel::Errors.new(self) end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
23 24 25 |
# File 'lib/yaml_record/base.rb', line 23 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
24 25 26 |
# File 'lib/yaml_record/base.rb', line 24 def errors @errors end |
#is_created ⇒ Object
Returns the value of attribute is_created.
23 24 25 |
# File 'lib/yaml_record/base.rb', line 23 def is_created @is_created end |
#is_destroyed ⇒ Object
Returns the value of attribute is_destroyed.
23 24 25 |
# File 'lib/yaml_record/base.rb', line 23 def is_destroyed @is_destroyed end |
Class Method Details
.all ⇒ Object
Returns collection of all YamlRecord instances Caches results during request
Example:
Post.all => [@post1, @post2, ...]
Post.all(true) => (...force reload...)
263 264 265 266 267 268 269 270 271 |
# File 'lib/yaml_record/base.rb', line 263 def self.all begin raw_items = YAML.load_file(source) rescue Errno::ENOENT ensure raw_items ||= [] end raw_items.map { |item| self.new(item.merge(:persisted => true)) } end |
.create(attributes = {}) ⇒ Object
Initializes YamlRecord instance given an attribute hash and saves afterwards Returns instance if successfully saved; false otherwise
Example:
Post.create(:foo => "bar", :miso => "great") => @post
304 305 306 307 308 309 310 311 312 |
# File 'lib/yaml_record/base.rb', line 304 def self.create(attributes={}) @fs = self.new(attributes) if @fs.save == true @fs.is_created = true; @fs else false end end |
.find_by_attribute(attribute, expected_value) ⇒ Object
Find YamlRecord instance given attribute name and expected value Supports checking inclusion for array based values Returns instance if found; false otherwise
Example:
Post.find_by_attribute(:foo, "bar") => @post
Post.find_by_attribute(:some_list, "item") => @post
231 232 233 234 235 236 237 238 |
# File 'lib/yaml_record/base.rb', line 231 def self.find_by_attribute(attribute, expected_value) self.all.find do |record| value = record.send(attribute) if record.respond_to?(attribute) value.is_a?(Array) ? value.include?(expected_value) : value == expected_value end end |
.find_by_id(value) ⇒ Object Also known as: find
Find YamlRecord instance given id Returns instance if found; false otherwise
Example:
Post.find_by_id("a1b2c3") => @post
249 250 251 |
# File 'lib/yaml_record/base.rb', line 249 def find_by_id(value) self.find_by_attribute(:id, value) end |
.first(limit = 1) ⇒ Object
Find first YamlRecord instance given a limit Returns an array of instances if found; empty otherwise
Example:
Post.first => @post
Post.first(3) => [@p1, @p2, @p3]
293 294 295 |
# File 'lib/yaml_record/base.rb', line 293 def self.first(limit=1) limit == 1 ? self.all.first : self.all.first(limit) end |
.last(limit = 1) ⇒ Object
Find last YamlRecord instance given a limit Returns an array of instances if found; empty otherwise
Example:
Post.last => @post6
Post.last(3) => [@p4, @p5, @p6]
281 282 283 |
# File 'lib/yaml_record/base.rb', line 281 def self.last(limit=1) limit == 1 ? self.all.last : self.all.last(limit) end |
.properties(*names) ⇒ Object
Declares persisted attributes for YamlRecord class
Example:
class Post < YamlRecord::Base; properties :foo, :miso; end
Post.create(:foo => "bar", :miso => "great") => @post
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/yaml_record/base.rb', line 321 def self.properties(*names) @_properties ||= [] if names.size == 0 # getter @_properties elsif names.size > 0 # setter names = names | [:id] setup_properties!(*names) @_properties += names end end |
.source(file = nil) ⇒ Object
Declares source file for YamlRecord class
Example:
class Post < YamlRecord::Base
source "path/to/yaml/file"
end
340 341 342 |
# File 'lib/yaml_record/base.rb', line 340 def self.source(file = nil) @@file ||= file ? file.to_s : File.join(YamlRecord.root_path, "#{model_name.name}.yml") end |
Instance Method Details
#==(comparison_record) ⇒ Object
Overrides equality to match if matching ids
346 347 348 |
# File 'lib/yaml_record/base.rb', line 346 def ==(comparison_record) self.id == comparison_record.id end |
#[](attribute) ⇒ Object
Accesses given attribute from YamlRecord instance
Example:
@post[:foo] => "bar"
53 54 55 |
# File 'lib/yaml_record/base.rb', line 53 def [](attribute) self.attributes[attribute] end |
#[]=(attribute, value) ⇒ Object
Assign given attribute from YamlRecord instance with specified value
Example:
@post[:foo] = "baz"
63 64 65 |
# File 'lib/yaml_record/base.rb', line 63 def []=(attribute, value) self.attributes[attribute] = value end |
#column_names ⇒ Object
Returns array of instance attributes names; An attribute is a value stored for this record (persisted or not)
Example:
@post.column_names => ["foo", "miso"]
116 117 118 119 120 |
# File 'lib/yaml_record/base.rb', line 116 def column_names array = [] self.attributes.each_key { |k| array << k.to_s } array end |
#destroy ⇒ Object
Remove a persisted YamlRecord object Returns true if destroyed; false otherwise
Example:
@post = Post.create(:foo => "bar", :miso => "great")
Post.all.size => 1
@post.destroy => true
Post.all.size => 0
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/yaml_record/base.rb', line 172 def destroy run_callbacks(:destroy) do new_data = self.class.all .reject { |item| item.persisted_attributes == self.persisted_attributes } .map { |item| item.persisted_attributes } self.class.write_contents(new_data) self.is_destroyed = true end true rescue IOError false end |
#destroyed? ⇒ Boolean
Returns true if YamlRecord instance has been destroyed; false otherwise
Example:
@post = Post.new(:foo => "bar", :miso => "great")
@post.destroyed? => false
@post.save
@post.destroy => true
@post.destroyed? => true
158 159 160 |
# File 'lib/yaml_record/base.rb', line 158 def destroyed? self.is_destroyed end |
#new_record? ⇒ Boolean
Returns true if YamlRecord instance hasn’t persisted; false otherwise
Example:
@post = Post.new(:foo => "bar", :miso => "great")
@post.new_record? => true
@post.save => true
@post.new_record? => false
144 145 146 |
# File 'lib/yaml_record/base.rb', line 144 def new_record? !self.is_created end |
#persisted_attributes ⇒ Object
Returns hash of attributes to be persisted to file. A persisted attribute is a value stored in the file (specified with the properties declaration)
Example:
class Post < YamlRecord::Base; properties :foo, :miso; end
@post = Post.create(:foo => "bar", :miso => "great")
@post.persisted_attributes => { :id => "a1b2c3", :foo => "bar", :miso => "great" }
131 132 133 |
# File 'lib/yaml_record/base.rb', line 131 def persisted_attributes self.attributes.slice(*self.class.properties).reject { |k, v| v.nil? } end |
#reload ⇒ Object
Reload YamlRecord instance attributes from file
Example:
@post = Post.create(:foo => "bar", :miso => "great")
@post.foo = "bazz"
@post.reload
@post.foo => "bar"
216 217 218 219 220 |
# File 'lib/yaml_record/base.rb', line 216 def reload record = self.class.find(self.id) self.attributes = record.attributes record end |
#save ⇒ Object
Saved YamlRecord instance to file Executes save and create callbacks Returns true if record saved; false otherwise
Example:
@post.save => true
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/yaml_record/base.rb', line 75 def save block = lambda do run_callbacks(:save) do existing_items = self.class.all if self.new_record? existing_items << self else # update existing record updated_item = existing_items.find { |item| item.id == self.id } return false unless updated_item updated_item.attributes = self.attributes end raw_data = existing_items ? existing_items.map { |item| item.persisted_attributes } : [] self.class.write_contents(raw_data) if self.valid? end end self.is_created ? block.call : run_callbacks(:create) { block.call } true rescue IOError false end |
#to_model ⇒ Object
Returns YamlRecord Instance Complies with ActiveModel api
Example:
@post.to_model => @post
192 193 194 |
# File 'lib/yaml_record/base.rb', line 192 def to_model self end |
#to_param ⇒ Object
Returns the instance of a record as a parameter By default return an id
Example:
@post.to_param => <id>
203 204 205 |
# File 'lib/yaml_record/base.rb', line 203 def to_param self.id end |
#update_attributes(updated_attrs = {}) ⇒ Object
Update YamlRecord instance with specified attributes Returns true if record updated; false otherwise
Example:
@post.update_attributes(:foo => "baz", :miso => "awesome") => true
105 106 107 108 |
# File 'lib/yaml_record/base.rb', line 105 def update_attributes(updated_attrs={}) updated_attrs.each { |k,v| self.send("#{k}=", v) } self.save end |