Class: Brief::Model::Definition
- Inherits:
-
Object
- Object
- Brief::Model::Definition
- Defined in:
- lib/brief/model/definition.rb
Instance Attribute Summary collapse
-
#content_schema ⇒ Object
Returns the value of attribute content_schema.
-
#defined_actions ⇒ Object
Returns the value of attribute defined_actions.
-
#defined_helpers ⇒ Object
Returns the value of attribute defined_helpers.
-
#documentation_path ⇒ Object
Returns the value of attribute documentation_path.
-
#example_body ⇒ Object
Returns the value of attribute example_body.
-
#example_path ⇒ Object
Returns the value of attribute example_path.
-
#metadata_schema ⇒ Object
Returns the value of attribute metadata_schema.
-
#name ⇒ Object
Returns the value of attribute name.
-
#new_doc_name_block ⇒ Object
Returns the value of attribute new_doc_name_block.
-
#new_doc_template_block ⇒ Object
Returns the value of attribute new_doc_template_block.
-
#options ⇒ Object
Returns the value of attribute options.
-
#section_mappings ⇒ Object
Returns the value of attribute section_mappings.
-
#template_body ⇒ Object
Returns the value of attribute template_body.
-
#template_path ⇒ Object
Returns the value of attribute template_path.
-
#type_alias ⇒ Object
Returns the value of attribute type_alias.
Instance Method Summary collapse
- #actions(&block) ⇒ Object
- #apply_config ⇒ Object
- #content(_options = {}, &block) ⇒ Object
- #defined_helper_methods ⇒ Object
-
#defined_in(filename = nil, options = {}) ⇒ Object
TODO There is probably a way to inspect the filename of the code calling you which would be a better way of handling this that doesn’t require.
- #documentation(options = {}, &block) ⇒ Object
- #example(body = nil, _options = {}) ⇒ Object
- #has_actions? ⇒ Boolean
- #helpers(include_in_command_list = false, &block) ⇒ Object
-
#initialize(name, options = {}) ⇒ Definition
constructor
A new instance of Definition.
- #inside_content? ⇒ Boolean
- #inside_meta? ⇒ Boolean
- #meta(_options = {}, &block) ⇒ Object
- #method_missing(meth, *args, &block) ⇒ Object
- #model_class ⇒ Object
- #model_namespace ⇒ Object
- #section_mapping(identifier) ⇒ Object
- #template(body = nil, _options = {}) ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Definition
Returns a new instance of Definition.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/brief/model/definition.rb', line 19 def initialize(name, = {}) @name = name = @type_alias = .fetch(:type_alias) { name.downcase.parameterize.gsub(/-/, '_') } = {}.to_mash @section_mappings = {}.to_mash @content_schema = { attributes: {} }.to_mash @model_class = [:model_class] = {}.to_mash end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/brief/model/definition.rb', line 160 def method_missing(meth, *args, &block) args = args.dup if inside_content? if meth.to_sym == :define_section opts = args. identifier = args.first section_mappings[identifier] ||= Brief::Document::Section::Mapping.new(identifier, opts) section_mapping(identifier).instance_eval(&block) if block else content_schema.attributes[meth] = { args: args, block: block } end elsif if args.first.is_a?(Hash) args.unshift(String) end args.unshift(meth) [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_actions ⇒ Object
Returns the value of attribute defined_actions.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def defined_actions @defined_actions 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 |
#documentation_path ⇒ Object
Returns the value of attribute documentation_path.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def documentation_path @documentation_path end |
#example_body ⇒ Object
Returns the value of attribute example_body.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def example_body @example_body end |
#example_path ⇒ Object
Returns the value of attribute example_path.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def example_path @example_path end |
#metadata_schema ⇒ Object
Returns the value of attribute metadata_schema.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def name @name end |
#new_doc_name_block ⇒ Object
Returns the value of attribute new_doc_name_block.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def new_doc_name_block @new_doc_name_block end |
#new_doc_template_block ⇒ Object
Returns the value of attribute new_doc_template_block.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def new_doc_template_block @new_doc_template_block end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def 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 |
#template_body ⇒ Object
Returns the value of attribute template_body.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def template_body @template_body end |
#template_path ⇒ Object
Returns the value of attribute template_path.
3 4 5 |
# File 'lib/brief/model/definition.rb', line 3 def template_path @template_path 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
126 127 128 129 |
# File 'lib/brief/model/definition.rb', line 126 def actions(&block) self.defined_actions ||= [] helpers(true, &block) end |
#apply_config ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/brief/model/definition.rb', line 41 def apply_config # define a virtus attribute mapping .values.each do |settings| begin settings[:args] = Array(settings[:args]) settings[:args][1] = String if settings[:args][1] == '' model_class.send(:attribute, *(settings[:args])) rescue => e raise "Error in metadata schema definition.\n #{ settings.inspect } \n\n #{e.message}" end end # defined helpers adds an anonymous module include Array(self.defined_helpers).each { |mod| model_class.send(:include, mod) } true end |
#content(_options = {}, &block) ⇒ Object
101 102 103 104 |
# File 'lib/brief/model/definition.rb', line 101 def content( = {}, &block) @current = :content instance_eval(&block) end |
#defined_helper_methods ⇒ Object
59 60 61 |
# File 'lib/brief/model/definition.rb', line 59 def defined_helper_methods defined_helpers.map(&:instance_methods).flatten end |
#defined_in(filename = nil, options = {}) ⇒ Object
TODO There is probably a way to inspect the filename of the code calling you which would be a better way of handling this that doesn’t require
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/brief/model/definition.rb', line 74 def defined_in(filename=nil, ={}) if filename filename = Pathname(filename) [:defined_in] = filename self.example_path ||= .fetch(:example_path) do filename.parent.join("..","examples", filename.basename.to_s.gsub('.rb','.md')) end self.documentation_path ||= .fetch(:documentation_path) do filename.parent.join("..","documentation", filename.basename.to_s.gsub('.rb','.md')) end end [:defined_in] end |
#documentation(options = {}, &block) ⇒ Object
91 92 93 94 |
# File 'lib/brief/model/definition.rb', line 91 def documentation(={}, &block) .merge!() if end |
#example(body = nil, _options = {}) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/brief/model/definition.rb', line 106 def example(body = nil, = {}) if body.is_a?(Hash) = body elsif body.is_a?(String) self.example_body = body end end |
#has_actions? ⇒ Boolean
122 123 124 |
# File 'lib/brief/model/definition.rb', line 122 def has_actions? self.defined_actions.empty? end |
#helpers(include_in_command_list = false, &block) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/brief/model/definition.rb', line 131 def helpers(include_in_command_list=false, &block) self.defined_helpers ||= [] if block mod = Module.new mod.module_eval(&block) if include_in_command_list self.defined_actions ||= [] self.defined_actions += mod.instance_methods self.defined_actions.uniq! end self.defined_helpers << mod end end |
#inside_content? ⇒ Boolean
152 153 154 |
# File 'lib/brief/model/definition.rb', line 152 def inside_content? @current == :content end |
#inside_meta? ⇒ Boolean
148 149 150 |
# File 'lib/brief/model/definition.rb', line 148 def @current == :meta end |
#meta(_options = {}, &block) ⇒ Object
96 97 98 99 |
# File 'lib/brief/model/definition.rb', line 96 def ( = {}, &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 Brief.default_model_class 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
156 157 158 |
# File 'lib/brief/model/definition.rb', line 156 def section_mapping(identifier) section_mappings.fetch(identifier) end |
#template(body = nil, _options = {}) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/brief/model/definition.rb', line 114 def template(body = nil, = {}) if body.is_a?(Hash) = body elsif body.is_a?(String) self.template_body = body end end |
#valid? ⇒ Boolean
31 32 33 |
# File 'lib/brief/model/definition.rb', line 31 def valid? name.to_s.length > 0 && type_alias.to_s.length > 0 end |
#validate! ⇒ Object
35 36 37 38 39 |
# File 'lib/brief/model/definition.rb', line 35 def validate! if valid? apply_config end end |