Class: Sinja::Config

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ConfigUtils
Defined in:
lib/sinja/config.rb

Constant Summary collapse

DEFAULT_SERIALIZER_OPTS =
{
  :jsonapi=>{ :version=>'1.0' }.freeze
}.freeze
DEFAULT_OPTS =
{
  :json_generator=>(Sinatra::Base.development? ? :pretty_generate : :generate),
  :json_error_generator=>(Sinatra::Base.development? ? :pretty_generate : :generate)
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigUtils

#deep_copy, #deep_freeze

Constructor Details

#initializeConfig

Returns a new instance of Config.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sinja/config.rb', line 61

def initialize
  @query_params = {
    :include=>[], # passthru to JAS
    :fields=>{}, # passthru to JAS
    :filter=>{},
    :page=>{},
    :sort=>[]
  }

  @error_logger = ->(h) { logger.error('sinja') { h } }

  @default_roles = {
    :resource=>RolesConfig.new(%i[show show_many index create update destroy]),
    :has_many=>RolesConfig.new(%i[fetch clear replace merge subtract]),
    :has_one=>RolesConfig.new(%i[pluck prune graft])
  }

  action_proc = proc { |type, hash, action| hash[action] = {
    :roles=>@default_roles[type][action].dup,
    :sideload_on=>Set.new,
    :filter_by=>Set.new,
    :sort_by=>Set.new
  }}.curry

  @resource_config = Hash.new { |h, k| h[k] = {
    :resource=>Hash.new(&action_proc[:resource]),
    :has_many=>Hash.new { |rh, rk| rh[rk] = Hash.new(&action_proc[:has_many]) },
    :has_one=>Hash.new { |rh, rk| rh[rk] = Hash.new(&action_proc[:has_one]) }
  }}

  @conflict_exceptions = Set.new
  @not_found_exceptions = Set.new
  @validation_exceptions = Set.new
  @validation_formatter = ->{ Array.new }

  @opts = deep_copy(DEFAULT_OPTS)
  @page_using = Hash.new
  @serializer_opts = deep_copy(DEFAULT_SERIALIZER_OPTS)
end

Instance Attribute Details

#conflict_exceptionsObject

Returns the value of attribute conflict_exceptions.



50
51
52
# File 'lib/sinja/config.rb', line 50

def conflict_exceptions
  @conflict_exceptions
end

#error_loggerObject

Returns the value of attribute error_logger.



50
51
52
# File 'lib/sinja/config.rb', line 50

def error_logger
  @error_logger
end

#not_found_exceptionsObject

Returns the value of attribute not_found_exceptions.



50
51
52
# File 'lib/sinja/config.rb', line 50

def not_found_exceptions
  @not_found_exceptions
end

#page_usingObject

Returns the value of attribute page_using.



50
51
52
# File 'lib/sinja/config.rb', line 50

def page_using
  @page_using
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



50
51
52
# File 'lib/sinja/config.rb', line 50

def query_params
  @query_params
end

#resource_configObject (readonly)

Returns the value of attribute resource_config.



50
51
52
# File 'lib/sinja/config.rb', line 50

def resource_config
  @resource_config
end

#serializer_optsObject

Returns the value of attribute serializer_opts.



50
51
52
# File 'lib/sinja/config.rb', line 50

def serializer_opts
  @serializer_opts
end

#validation_exceptionsObject

Returns the value of attribute validation_exceptions.



50
51
52
# File 'lib/sinja/config.rb', line 50

def validation_exceptions
  @validation_exceptions
end

#validation_formatterObject

Returns the value of attribute validation_formatter.



50
51
52
# File 'lib/sinja/config.rb', line 50

def validation_formatter
  @validation_formatter
end

Instance Method Details

#default_has_many_rolesObject



141
142
143
# File 'lib/sinja/config.rb', line 141

def default_has_many_roles
  @default_roles[:has_many]
end

#default_has_many_roles=(other = {}) ⇒ Object



145
146
147
# File 'lib/sinja/config.rb', line 145

def default_has_many_roles=(other={})
  @default_roles[:has_many].merge!(other)
end

#default_has_one_rolesObject



149
150
151
# File 'lib/sinja/config.rb', line 149

def default_has_one_roles
  @default_roles[:has_one]
end

#default_has_one_roles=(other = {}) ⇒ Object



153
154
155
# File 'lib/sinja/config.rb', line 153

def default_has_one_roles=(other={})
  @default_roles[:has_one].merge!(other)
end

#default_rolesObject



133
134
135
# File 'lib/sinja/config.rb', line 133

def default_roles
  @default_roles[:resource]
end

#default_roles=(other = {}) ⇒ Object



137
138
139
# File 'lib/sinja/config.rb', line 137

def default_roles=(other={})
  @default_roles[:resource].merge!(other)
end

#freezeObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/sinja/config.rb', line 170

def freeze
  @query_params.freeze
  @error_logger.freeze

  deep_freeze(@default_roles)
  deep_freeze(@resource_config)

  @conflict_exceptions.freeze
  @not_found_exceptions.freeze
  @validation_exceptions.freeze
  @validation_formatter.freeze

  @opts.freeze
  @page_using.freeze
  deep_freeze(@serializer_opts)

  super
end