Class: Brief::Model::Definition
- Inherits:
-
Object
- Object
- Brief::Model::Definition
show all
- Defined in:
- lib/brief/model/definition.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, options = {}) ⇒ Definition
Returns a new instance of Definition.
11
12
13
14
15
16
17
18
19
|
# File 'lib/brief/model/definition.rb', line 11
def initialize(name, options={})
@name = name
@options = options
@type_alias = options.fetch(:type_alias) { name.downcase.parameterize.gsub(/-/,'_') }
@metadata_schema = {}.to_mash
@section_mappings = {}.to_mash
@content_schema = {attributes:{}}.to_mash
@model_class = options[:model_class]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/brief/model/definition.rb', line 116
def method_missing(meth, *args, &block)
args = args.dup
if inside_content?
if meth.to_sym == :define_section
opts = args.
identifier = args.first
self.section_mappings[identifier] ||= Brief::Document::Section::Mapping.new(identifier, opts)
section_mapping(identifier).instance_eval(&block) if block
else
self.content_schema.attributes[meth] = {args: args, block: block}
end
elsif inside_meta?
if args.first.is_a?(Hash)
args.unshift(String)
end
args.unshift(meth)
self.metadata_schema[meth] = {args: args, block: block}
else
super
end
end
|
Instance Attribute Details
#content_schema ⇒ Object
Returns the value of attribute content_schema.
3
4
5
|
# File 'lib/brief/model/definition.rb', line 3
def content_schema
@content_schema
end
|
#defined_helpers ⇒ Object
Returns the value of attribute defined_helpers.
3
4
5
|
# File 'lib/brief/model/definition.rb', line 3
def defined_helpers
@defined_helpers
end
|
Returns the value of attribute metadata_schema.
3
4
5
|
# File 'lib/brief/model/definition.rb', line 3
def metadata_schema
@metadata_schema
end
|
#name ⇒ Object
Returns the value of attribute name.
3
4
5
|
# File 'lib/brief/model/definition.rb', line 3
def name
@name
end
|
#options ⇒ Object
Returns the value of attribute options.
3
4
5
|
# File 'lib/brief/model/definition.rb', line 3
def options
@options
end
|
#section_mappings ⇒ Object
Returns the value of attribute section_mappings.
3
4
5
|
# File 'lib/brief/model/definition.rb', line 3
def section_mappings
@section_mappings
end
|
#type_alias ⇒ Object
Returns the value of attribute type_alias.
3
4
5
|
# File 'lib/brief/model/definition.rb', line 3
def type_alias
@type_alias
end
|
Instance Method Details
#actions(&block) ⇒ Object
85
86
87
|
# File 'lib/brief/model/definition.rb', line 85
def actions(&block)
helpers(&block)
end
|
#apply_config ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/brief/model/definition.rb', line 44
def apply_config
metadata_schema.values.each do |settings|
model_class.send(:attribute, *(settings[:args]))
end
Array(self.defined_helpers).each {|mod| model_class.send(:include, mod) }
model_class.defined_actions += Array(self.defined_actions)
true
end
|
#content(options = {}, &block) ⇒ Object
76
77
78
79
|
# File 'lib/brief/model/definition.rb', line 76
def content(options={}, &block)
@current = :content
instance_eval(&block)
end
|
#create_model_class ⇒ Object
57
58
59
60
61
|
# File 'lib/brief/model/definition.rb', line 57
def create_model_class
unless (model_namespace.const_get(type_alias.camelize) rescue nil)
model_namespace.const_set(type_alias.camelize, Class.new)
end
end
|
#defined_actions ⇒ Object
89
90
91
|
# File 'lib/brief/model/definition.rb', line 89
def defined_actions
Array(defined_helpers).map(&:instance_methods).flatten
end
|
#has_actions? ⇒ Boolean
81
82
83
|
# File 'lib/brief/model/definition.rb', line 81
def has_actions?
!@defined_actions.empty?
end
|
#helpers(&block) ⇒ Object
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/brief/model/definition.rb', line 93
def helpers(&block)
self.defined_helpers ||= []
if block
mod = Module.new
mod.module_eval(&block)
self.defined_helpers << mod
end
end
|
#inside_content? ⇒ Boolean
108
109
110
|
# File 'lib/brief/model/definition.rb', line 108
def inside_content?
@current == :content
end
|
104
105
106
|
# File 'lib/brief/model/definition.rb', line 104
def inside_meta?
@current == :meta
end
|
71
72
73
74
|
# File 'lib/brief/model/definition.rb', line 71
def meta(options={}, &block)
@current = :meta
instance_eval(&block)
end
|
#model_class ⇒ Object
63
64
65
|
# File 'lib/brief/model/definition.rb', line 63
def model_class
@model_class || model_namespace.const_get(type_alias.camelize) rescue nil
end
|
#model_namespace ⇒ Object
67
68
69
|
# File 'lib/brief/model/definition.rb', line 67
def model_namespace
Brief.configuration.model_namespace || Brief::Model
end
|
#section_mapping(identifier) ⇒ Object
112
113
114
|
# File 'lib/brief/model/definition.rb', line 112
def section_mapping(identifier)
section_mappings.fetch(identifier)
end
|
#valid? ⇒ Boolean
21
22
23
|
# File 'lib/brief/model/definition.rb', line 21
def valid?
name.to_s.length > 0 && type_alias.to_s.length > 0
end
|
#validate! ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/brief/model/definition.rb', line 25
def validate!
definition = self
if valid?
create_model_class.tap do |k|
k.send(:include, Brief::Model)
k.definition ||= definition
k.name ||= name
k.type_alias ||= type_alias
Brief::Model.classes << k
end
apply_config
end
end
|