Class: Brief::Briefcase

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/brief/briefcase.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

#action, #define, #view

Constructor Details

#initialize(options = {}) ⇒ Briefcase

Returns a new instance of Briefcase.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/brief/briefcase.rb', line 8

def initialize(options = {})
  @options = options.to_mash

  load_configuration
  load_model_definitions

  if Brief.case.nil?
    Brief.case = self
  end

  Brief.cases[root] ||= self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



167
168
169
170
171
172
173
174
175
176
# File 'lib/brief/briefcase.rb', line 167

def method_missing(meth, *args, &block)
  if Brief.views.key?(meth.to_sym)
    block = Brief.views[meth.to_sym]
    block.call(self, args.extract_options!)
  elsif repository.respond_to?(meth)
    repository.send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#model_definitionsObject (readonly)

Returns the value of attribute model_definitions.



5
6
7
# File 'lib/brief/briefcase.rb', line 5

def model_definitions
  @model_definitions
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/brief/briefcase.rb', line 5

def options
  @options
end

Instance Method Details

#app_pathObject



119
120
121
# File 'lib/brief/briefcase.rb', line 119

def app_path
  uses_app? && Brief.apps_path.join(options[:app])
end

#as_default(params = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/brief/briefcase.rb', line 41

def as_default(params={})
  params.symbolize_keys!

  model_settings = {
    docs_path: docs_path
  }

  model_settings[:rendered] = !!(params.key?(:rendered))
  model_settings[:content] = !!(params.key?(:content))

  all = all_models.compact

  schema = all.map(&:class).uniq.compact
             .map(&:to_schema)
             .reduce({}) {|m, k| m[k[:type_alias]] = k; m }

  models = all.map {|m| m.as_json(model_settings) }

  {
    views: Brief.views.keys,
    key: briefcase.folder_name.to_s.parameterize,
    name: briefcase.folder_name.to_s.titlecase,
    schema: schema,
    models: models,
    settings: settings
  }
end

#as_full_exportObject



69
70
71
# File 'lib/brief/briefcase.rb', line 69

def as_full_export
  as_default(content: true, rendered: true)
end

#configObject



83
84
85
# File 'lib/brief/briefcase.rb', line 83

def config
  Brief::Configuration.instance
end

#docs_pathObject



147
148
149
# File 'lib/brief/briefcase.rb', line 147

def docs_path
  root.join options.fetch(:docs_path) { config.docs_path }
end

#folder_nameObject



91
92
93
# File 'lib/brief/briefcase.rb', line 91

def folder_name
  root.basename
end

#load_configurationObject

Loads the configuration for this briefcase, either from the current working directory or the configured path for the configuration file.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/brief/briefcase.rb', line 97

def load_configuration
  config_path = options.fetch(:config_path) do
    root.join('brief.rb')
  end

  if config_path.is_a?(String)
    config_path = root.join(config_path)
  end

  if uses_app?
    instance_eval(app_path.join("config.rb").read)
  end

  if config_path.exist?
    instance_eval(config_path.read) rescue nil
  end
end

#load_model_definitionsObject



123
124
125
126
127
128
129
130
# File 'lib/brief/briefcase.rb', line 123

def load_model_definitions
  if uses_app?
    Brief.load_modules_from(app_path.join("models"))
  end

  Brief.load_modules_from(models_path) if models_path.exist?
  Brief::Model.finalize
end

#model(name_or_type) ⇒ Object

Returns a model name by its human readable description or its type alias



133
134
135
136
137
138
139
140
141
# File 'lib/brief/briefcase.rb', line 133

def model(name_or_type)
  table = Brief::Model.table

  table.fetch(name_or_type) do
    table.values.find do |k|
      k.name == name_or_type
    end
  end
end

#models_pathObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/brief/briefcase.rb', line 151

def models_path
  value = options.fetch(:models_path) { config.models_path }

  if value.to_s.match(/\./)
    Pathname(Dir.pwd).join(value)
  elsif value.to_s.match(/\//)
    Pathname(value)
  else
    root.join(value)
  end
end

#present(style = "default", params = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/brief/briefcase.rb', line 21

def present(style="default", params={})
  if respond_to?("as_#{style}")
    send("as_#{style}", params)
  elsif Brief.views.key?(style.to_sym)
    block = Brief.views[style.to_sym]
    block.call(self, params)
  end
end

#repositoryObject



163
164
165
# File 'lib/brief/briefcase.rb', line 163

def repository
  @repository ||= Brief::Repository.new(self, options)
end

#rootObject



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

def root
  Pathname(options.fetch(:root) { Dir.pwd })
end

#server(options = {}) ⇒ Object



87
88
89
# File 'lib/brief/briefcase.rb', line 87

def server(options={})
  @server ||= Brief::Server.new(self, options)
end

#settingsObject



30
31
32
# File 'lib/brief/briefcase.rb', line 30

def settings
  @settings ||= settings!
end

#settings!Object



34
35
36
37
38
39
# File 'lib/brief/briefcase.rb', line 34

def settings!
  if root.join("settings.yml").exist?
    y = YAML.load(root.join("settings.yml").read) rescue nil
    (y || {}).to_mash
  end
end

#use(module_type = :app, module_id) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/brief/briefcase.rb', line 73

def use(module_type=:app, module_id)
  if module_type == :app && apps_path.join(module_id).exist?
    config = module_type.join("config.rb")
    models = module_type.join("models")

    instance_eval(config.read) if config.exist?
    Brief.load_modules_from(models) if models.exist?
  end
end

#uses_app?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/brief/briefcase.rb', line 115

def uses_app?
  options.key?(:app) && Brief.apps_path.join(options[:app]).exist?
end