Class: Knish::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/knish/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Reader

Returns a new instance of Reader.



5
6
7
# File 'lib/knish/reader.rb', line 5

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/knish/reader.rb', line 3

def config
  @config
end

Instance Method Details

#get_collectionsObject



20
21
22
23
24
25
26
27
# File 'lib/knish/reader.rb', line 20

def get_collections
  config.collections.inject({}) do |hash, collection_name|
    collection = Collection.new(collection_name, config)
    collection.load
    hash[collection_name] = collection
    hash
  end
end

#get_jsonObject



9
10
11
# File 'lib/knish/reader.rb', line 9

def get_json
  JSON.parse(read_file(config.data_filename) || '{}')
end

#get_markdownObject



13
14
15
16
17
18
# File 'lib/knish/reader.rb', line 13

def get_markdown
  config.markdown_attributes.inject({}) do |hash, key|
    hash[key] = read_markdown(key)
    hash
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/knish/reader.rb', line 33

def persisted?
  !!read_file(config.data_filename)
end

#read_file(filename) ⇒ Object



37
38
39
# File 'lib/knish/reader.rb', line 37

def read_file(filename)
  File.read("#{config.model_root}/#{filename}") rescue nil
end

#template(key) ⇒ Object



29
30
31
# File 'lib/knish/reader.rb', line 29

def template(key)
  "#{config.template_path}/#{key}"
end