Class: TextRecord::Base
- Inherits:
-
Object
- Object
- TextRecord::Base
- Extended by:
- Forwardable
- Defined in:
- lib/text_record/base.rb
Class Attribute Summary collapse
-
.attributes ⇒ Object
readonly
aka @attributes in this block.
Instance Attribute Summary collapse
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
- .all ⇒ Object
- .attribute(name, options = {}) ⇒ Object
- .directory ⇒ Object
- .find(slug) ⇒ Object
- .where(options) ⇒ Object
Instance Method Summary collapse
- #==(other_model) ⇒ Object
- #configuration ⇒ Object
- #directory ⇒ Object
- #eql?(other_model) ⇒ Boolean
-
#initialize(path) ⇒ Base
constructor
Instance Methods.
- #yml ⇒ Object
- #yml_file ⇒ Object
Constructor Details
#initialize(path) ⇒ Base
Instance Methods
54 55 56 57 58 59 60 61 62 |
# File 'lib/text_record/base.rb', line 54 def initialize(path) @slug = path raise SlugNotFoundError.new("#{directory} could not be found.") unless File.directory?(directory) self.class.attributes.each do |attribute| # class attributes, attr_readers to be added to object attribute.parse(self) end end |
Class Attribute Details
.attributes ⇒ Object (readonly)
aka @attributes in this block
16 17 18 |
# File 'lib/text_record/base.rb', line 16 def attributes @attributes end |
Instance Attribute Details
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
8 9 10 |
# File 'lib/text_record/base.rb', line 8 def slug @slug end |
Class Method Details
.all ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/text_record/base.rb', line 35 def all items = Collection.new Dir["#{directory}/*"].map do |dir| slug = File.basename dir items.push(new(slug)) end items end |
.attribute(name, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/text_record/base.rb', line 18 def attribute(name, = {}) .assert_valid_keys :from @attributes ||= [] [:from] ||= :yml self.class_eval do attr_reader name.to_s.underscore.to_sym end @attributes << Attribute.().new(name, ) end |
.directory ⇒ Object
31 32 33 |
# File 'lib/text_record/base.rb', line 31 def directory File.join(TextRecord.configuration.content_path, self.name.pluralize.underscore) end |
.find(slug) ⇒ Object
48 49 50 |
# File 'lib/text_record/base.rb', line 48 def find(slug) new(slug) end |
.where(options) ⇒ Object
44 45 46 |
# File 'lib/text_record/base.rb', line 44 def where() all.where() end |
Instance Method Details
#==(other_model) ⇒ Object
64 65 66 |
# File 'lib/text_record/base.rb', line 64 def ==(other_model) !other_model.nil? && (other_model.class == self.class) && self.slug.eql?(other_model.slug) end |
#configuration ⇒ Object
72 73 74 |
# File 'lib/text_record/base.rb', line 72 def configuration TextRecord.configuration end |
#directory ⇒ Object
76 77 78 79 |
# File 'lib/text_record/base.rb', line 76 def directory name = self.class.name.underscore.pluralize File.join(content_path, name, slug) end |
#eql?(other_model) ⇒ Boolean
68 69 70 |
# File 'lib/text_record/base.rb', line 68 def eql?(other_model) self == other_model end |
#yml ⇒ Object
85 86 87 |
# File 'lib/text_record/base.rb', line 85 def yml @yml ||= YAML.load_file(yml_file) end |
#yml_file ⇒ Object
81 82 83 |
# File 'lib/text_record/base.rb', line 81 def yml_file File.join(directory, attribute_file) end |