Class: Maglev::KnowledgeConfig::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/maglev/knowledge_config.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Builder

Returns a new instance of Builder.



63
64
65
66
67
68
69
70
71
# File 'lib/maglev/knowledge_config.rb', line 63

def initialize(model_class)
  @model_class = model_class
  @exposed_attributes = []
  @hidden_attributes = []
  @tags = []
  @relations = []
  @attached_sources = []
  @rich_text_sources = []
end

Instance Method Details

#build(&block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/maglev/knowledge_config.rb', line 73

def build(&block)
  instance_eval(&block) if block
  validate!

  KnowledgeConfig.new(
    model_class: @model_class,
    exposed_attributes: normalize(@exposed_attributes),
    hidden_attributes: normalize(@hidden_attributes),
    tags: normalize(@tags),
    relations: @relations.uniq { |relation| relation.name },
    attached_sources: @attached_sources.uniq(&:name),
    rich_text_sources: @rich_text_sources.uniq(&:name)
  )
end

#expose(*attributes) ⇒ Object



88
89
90
# File 'lib/maglev/knowledge_config.rb', line 88

def expose(*attributes)
  @exposed_attributes.concat(attributes)
end

#expose_attached(*names) ⇒ Object



104
105
106
# File 'lib/maglev/knowledge_config.rb', line 104

def expose_attached(*names)
  @attached_sources.concat(names.map { |name| ContentSource.new(name) })
end

#expose_rich_text(*names) ⇒ Object



108
109
110
# File 'lib/maglev/knowledge_config.rb', line 108

def expose_rich_text(*names)
  @rich_text_sources.concat(names.map { |name| ContentSource.new(name) })
end

#hide(*attributes) ⇒ Object



92
93
94
# File 'lib/maglev/knowledge_config.rb', line 92

def hide(*attributes)
  @hidden_attributes.concat(attributes)
end


100
101
102
# File 'lib/maglev/knowledge_config.rb', line 100

def include_related(association, depth:, limit:, inverse: nil)
  @relations << Relation.new(name: association, depth: depth, limit: limit, inverse: inverse)
end

#tags(*tags) ⇒ Object



96
97
98
# File 'lib/maglev/knowledge_config.rb', line 96

def tags(*tags)
  @tags.concat(tags)
end