Class: Katapult::ApplicationModel

Inherits:
Object
  • Object
show all
Defined in:
lib/katapult/application_model.rb

Constant Summary collapse

NotFound =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplicationModel

Returns a new instance of ApplicationModel.



22
23
24
25
26
# File 'lib/katapult/application_model.rb', line 22

def initialize
  @models = []
  @associations = []
  @web_uis = []
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



14
15
16
# File 'lib/katapult/application_model.rb', line 14

def associations
  @associations
end

#authenticationObject (readonly)

Returns the value of attribute authentication.



14
15
16
# File 'lib/katapult/application_model.rb', line 14

def authentication
  @authentication
end

#modelsObject (readonly)

Returns the value of attribute models.



14
15
16
# File 'lib/katapult/application_model.rb', line 14

def models
  @models
end

Returns the value of attribute nav.



14
15
16
# File 'lib/katapult/application_model.rb', line 14

def nav
  @nav
end

#web_uisObject (readonly)

Returns the value of attribute web_uis.



14
15
16
# File 'lib/katapult/application_model.rb', line 14

def web_uis
  @web_uis
end

Class Method Details

.parse(application_model_string, path_to_model = '') ⇒ Object



16
17
18
19
20
# File 'lib/katapult/application_model.rb', line 16

def self.parse(application_model_string, path_to_model = '')
  new.tap do |model|
    model.instance_eval application_model_string, path_to_model
  end
end

Instance Method Details

#association(name, options = {}) ⇒ Object



57
58
59
60
# File 'lib/katapult/application_model.rb', line 57

def association(name, options = {})
  options[:application_model] = self
  associations << Association.new(name, options)
end

#authenticate(user_model_name, system_email:) ⇒ Object

DSL



45
46
47
48
# File 'lib/katapult/application_model.rb', line 45

def authenticate(user_model_name, system_email:)
  @authentication = Authentication.new(user_model_name,
    system_email: system_email, application_model: self)
end

#crud(name, &block) ⇒ Object

DSL



51
52
53
54
# File 'lib/katapult/application_model.rb', line 51

def crud(name, &block)
  model name, &block
  web_ui name, &:crud
end

#get_belongs_tos_for(model_name) ⇒ Object

Returns all models that ‘model_name` belongs_to



72
73
74
# File 'lib/katapult/application_model.rb', line 72

def get_belongs_tos_for(model_name)
  associations.select { |a| a.name == model_name }.map(&:belongs_to_model)
end

#get_has_manys_for(model_name) ⇒ Object

Returns all models that ‘model_name` has_many of



77
78
79
# File 'lib/katapult/application_model.rb', line 77

def get_has_manys_for(model_name)
  associations.select { |a| a.belongs_to == model_name }.map(&:model)
end

#get_model!(name) ⇒ Object



62
63
64
65
# File 'lib/katapult/application_model.rb', line 62

def get_model!(name)
  models.find { |m| m.name == name } or raise NotFound,
    "Could not find a model named #{ name }"
end

#get_web_ui(name) ⇒ Object



67
68
69
# File 'lib/katapult/application_model.rb', line 67

def get_web_ui(name)
  web_uis.find { |w| w.name == name }
end

#model(name, &block) ⇒ Object

DSL



29
30
31
# File 'lib/katapult/application_model.rb', line 29

def model(name, &block)
  models << Model.new(name, application_model: self, &block)
end

DSL



40
41
42
# File 'lib/katapult/application_model.rb', line 40

def navigation(name = :main)
  @nav = Navigation.new(name, application_model: self)
end

#render(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/katapult/application_model.rb', line 81

def render(options = {})
  prepare_render

  models.each { |m| m.render(options) }
  web_uis.each { |w| w.render(options) }
  nav.render(options) if nav
  authentication.render(options) if authentication
end

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

DSL



34
35
36
37
# File 'lib/katapult/application_model.rb', line 34

def web_ui(name, options = {}, &block)
  options[:application_model] = self
  web_uis << WebUI.new(name, options, &block)
end