class Knish::Model

Attributes

config[RW]
config[W]

Public Class Methods

model_name() click to toggle source
# File lib/knish/model.rb, line 6
def self.model_name
  ActiveModel::Name.new(self, omitted_namespace)
end
new(attrs=nil) click to toggle source
# File lib/knish/model.rb, line 13
def initialize(attrs=nil)
  attrs ||= {}
  set(attrs)
end
omitted_namespace() click to toggle source
# File lib/knish/model.rb, line 54
def self.omitted_namespace
  config && config.omitted_namespace
end

Public Instance Methods

config() click to toggle source
# File lib/knish/model.rb, line 46
def config
  @config ||= self.class.config.clone
end
load() click to toggle source
# File lib/knish/model.rb, line 29
def load
  set(reader.get_json)
  set(reader.get_markdown)
  true
end
save() click to toggle source
# File lib/knish/model.rb, line 23
def save
  writer.save_json(data_attributes)
  writer.save_markdown(markdown_attributes)
  true
end
set(attrs) click to toggle source
# File lib/knish/model.rb, line 35
def set(attrs)
  attrs.each do |key, value|
    next if key.to_sym == config.type_key.to_sym
    public_send("#{key}=", value)
  end
end
template(key) click to toggle source
# File lib/knish/model.rb, line 42
def template(key)
  reader.template(key)
end

Private Instance Methods

collections() click to toggle source
# File lib/knish/model.rb, line 60
def collections
  config.collections.map {|collection| send(collection) }
end
data_attributes() click to toggle source
# File lib/knish/model.rb, line 64
def data_attributes
  extract_local_attributes(config.data_attributes).merge(___type: self.class.to_s)
end
extract_local_attributes(keys) click to toggle source
# File lib/knish/model.rb, line 72
def extract_local_attributes(keys)
  keys.inject({}) do |hash, key|
    hash[key] = public_send(key)
    hash
  end
end
markdown_attributes() click to toggle source
# File lib/knish/model.rb, line 68
def markdown_attributes
  extract_local_attributes(config.markdown_attributes)
end
reader() click to toggle source
# File lib/knish/model.rb, line 83
def reader
  @reader ||= Reader.new(config)
end
writer() click to toggle source
# File lib/knish/model.rb, line 79
def writer
  @writer ||= Writer.new(config)
end