Class: TextRecord::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/text_record/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Base

Instance Methods

Raises:



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

.attributesObject (readonly)

aka @attributes in this block



16
17
18
# File 'lib/text_record/base.rb', line 16

def attributes
  @attributes
end

Instance Attribute Details

#slugObject (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

.allObject



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, options = {})
  options.assert_valid_keys :from

  @attributes ||= []  
  options[:from] ||= :yml   

  self.class_eval do
    attr_reader name.to_s.underscore.to_sym
  end

  @attributes << Attribute.for_options(options).new(name, options)
end

.directoryObject



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(options)
  all.where(options)
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

#configurationObject



72
73
74
# File 'lib/text_record/base.rb', line 72

def configuration
  TextRecord.configuration
end

#directoryObject



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

Returns:

  • (Boolean)


68
69
70
# File 'lib/text_record/base.rb', line 68

def eql?(other_model)
  self == other_model
end

#ymlObject



85
86
87
# File 'lib/text_record/base.rb', line 85

def yml
  @yml ||= YAML.load_file(yml_file)
end

#yml_fileObject



81
82
83
# File 'lib/text_record/base.rb', line 81

def yml_file
  File.join(directory, attribute_file)
end