Class: Ymodel::Base

Inherits:
Object
  • Object
show all
Extended by:
Loadable, Queryable, Relatable, Triggerable
Defined in:
lib/ymodel/base.rb

Overview

This is used to wrap a YAML file in a similar manner to ActiveRecord wrapping a database.

Instance Method Summary collapse

Methods included from Relatable

belongs_to, has_many, has_one

Methods included from Loadable

default_attribute, define_reader, index_on, load_records!, pears_subject, source_file, source_files

Methods included from Queryable

all, find, find!, find_by, find_by!, find_by_key, find_by_key!, raise_record_not_found_exception!, sanitize_attributes, where, where!

Constructor Details

#initialize(record = {}) ⇒ Base



17
18
19
20
21
# File 'lib/ymodel/base.rb', line 17

def initialize(record = {})
  record.each do |k, v|
    instance_variable_set "@#{k}", v if attribute?(k)
  end
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
# File 'lib/ymodel/base.rb', line 23

def ==(other)
  other.respond_to?(:attributes) && attributes == other.attributes
end

#[](property) ⇒ Object



33
34
35
# File 'lib/ymodel/base.rb', line 33

def [](property)
  instance_variable_get("@#{property}")
end

#attribute?(key) ⇒ Boolean



37
38
39
40
# File 'lib/ymodel/base.rb', line 37

def attribute?(key)
  # definitive schema
  schema.include?(key) || key == :id
end

#attributesObject



27
28
29
30
31
# File 'lib/ymodel/base.rb', line 27

def attributes
  # definitive schema
  schema.attributes
    .each_with_object({}) { |attr, memo| memo[attr] = send(attr) }
end

#indexObject



42
43
44
# File 'lib/ymodel/base.rb', line 42

def index
  self[index_key]
end