Class: Brief::Model::Definition

Inherits:
Object
  • Object
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.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/brief/model/definition.rb', line 14

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]

  @doc_options = {}.to_mash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/brief/model/definition.rb', line 147

def method_missing(meth, *args, &block)
  args = args.dup

  if inside_content?
    if meth.to_sym == :define_section
      opts = args.extract_options!
      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 inside_meta?
    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_schemaObject

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_actionsObject

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_helpersObject

Returns the value of attribute defined_helpers.



3
4
5
# File 'lib/brief/model/definition.rb', line 3

def defined_helpers
  @defined_helpers
end

#example_bodyObject

Returns the value of attribute example_body.



3
4
5
# File 'lib/brief/model/definition.rb', line 3

def example_body
  @example_body
end

#metadata_schemaObject

Returns the value of attribute metadata_schema.



3
4
5
# File 'lib/brief/model/definition.rb', line 3

def 
  @metadata_schema
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/brief/model/definition.rb', line 3

def name
  @name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/brief/model/definition.rb', line 3

def options
  @options
end

#section_mappingsObject

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_bodyObject

Returns the value of attribute template_body.



3
4
5
# File 'lib/brief/model/definition.rb', line 3

def template_body
  @template_body
end

#type_aliasObject

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



113
114
115
116
# File 'lib/brief/model/definition.rb', line 113

def actions(&block)
  self.defined_actions ||= []
  helpers(true, &block)
end

#apply_configObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brief/model/definition.rb', line 36

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



88
89
90
91
# File 'lib/brief/model/definition.rb', line 88

def content(_options = {}, &block)
  @current = :content
  instance_eval(&block)
end

#defined_helper_methodsObject



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

def defined_helper_methods
  defined_helpers.map(&:instance_methods).flatten
end

#defined_in(filename = nil) ⇒ 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



69
70
71
72
73
74
75
76
# File 'lib/brief/model/definition.rb', line 69

def defined_in(filename=nil)
  if filename
    filename = Pathname(filename)
    @doc_options[:defined_in] = filename
  end

  @doc_options[:defined_in]
end

#documentation(options = {}, &block) ⇒ Object



78
79
80
81
# File 'lib/brief/model/definition.rb', line 78

def documentation(options={}, &block)
  @doc_options.merge!(options) if options
  @doc_options
end

#example(body = nil, _options = {}) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/brief/model/definition.rb', line 93

def example(body = nil, _options = {})
  if body.is_a?(Hash)
    options = body
  elsif body.is_a?(String)
    self.example_body = body
  end
end

#has_actions?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/brief/model/definition.rb', line 109

def has_actions?
  self.defined_actions.empty?
end

#helpers(include_in_command_list = false, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/brief/model/definition.rb', line 118

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

Returns:

  • (Boolean)


139
140
141
# File 'lib/brief/model/definition.rb', line 139

def inside_content?
  @current == :content
end

#inside_meta?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/brief/model/definition.rb', line 135

def inside_meta?
  @current == :meta
end

#meta(_options = {}, &block) ⇒ Object



83
84
85
86
# File 'lib/brief/model/definition.rb', line 83

def meta(_options = {}, &block)
  @current = :meta
  instance_eval(&block)
end

#model_classObject



58
59
60
# File 'lib/brief/model/definition.rb', line 58

def model_class
  @model_class || model_namespace.const_get(type_alias.camelize) rescue Brief.default_model_class
end

#model_namespaceObject



62
63
64
# File 'lib/brief/model/definition.rb', line 62

def model_namespace
  Brief.configuration.model_namespace || Brief::Model
end

#section_mapping(identifier) ⇒ Object



143
144
145
# File 'lib/brief/model/definition.rb', line 143

def section_mapping(identifier)
  section_mappings.fetch(identifier)
end

#template(body = nil, _options = {}) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/brief/model/definition.rb', line 101

def template(body = nil, _options = {})
  if body.is_a?(Hash)
    options = body
  elsif body.is_a?(String)
    self.template_body = body
  end
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/brief/model/definition.rb', line 26

def valid?
  name.to_s.length > 0 && type_alias.to_s.length > 0
end

#validate!Object



30
31
32
33
34
# File 'lib/brief/model/definition.rb', line 30

def validate!
  if valid?
    apply_config
  end
end