Class: Knish::Model

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ActiveModel::Model
Defined in:
lib/knish/model.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = nil) ⇒ Model

Returns a new instance of Model.



13
14
15
16
# File 'lib/knish/model.rb', line 13

def initialize(attrs=nil)
  attrs ||= {}
  set(attrs)
end

Class Attribute Details

.configObject

Returns the value of attribute config.



51
52
53
# File 'lib/knish/model.rb', line 51

def config
  @config
end

Instance Attribute Details

#configObject



46
47
48
# File 'lib/knish/model.rb', line 46

def config
  @config ||= self.class.config.clone
end

Class Method Details

.model_nameObject



6
7
8
# File 'lib/knish/model.rb', line 6

def self.model_name
  ActiveModel::Name.new(self, omitted_namespace)
end

.omitted_namespaceObject



54
55
56
# File 'lib/knish/model.rb', line 54

def self.omitted_namespace
  config && config.omitted_namespace
end

Instance Method Details

#loadObject



29
30
31
32
33
# File 'lib/knish/model.rb', line 29

def load
  set(reader.get_json)
  set(reader.get_markdown)
  true
end

#saveObject



23
24
25
26
27
# File 'lib/knish/model.rb', line 23

def save
  writer.save_json(data_attributes)
  writer.save_markdown(markdown_attributes)
  true
end

#set(attrs) ⇒ Object



35
36
37
38
39
40
# 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) ⇒ Object



42
43
44
# File 'lib/knish/model.rb', line 42

def template(key)
  reader.template(key)
end