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
100
# File 'lib/sinja/config.rb', line 61

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

  @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] = {
    :route_opts=>{ :pkre=>/\d+/ },
    :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 = DEFAULT_OPTS.dup
  @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

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Sinja::Config)

    the object that the method was called on



171
172
173
# File 'lib/sinja/config.rb', line 171

def configure
  yield self
end

#default_has_many_rolesObject



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

def default_has_many_roles
  @default_roles[:has_many]
end

#default_has_many_roles=(other = {}) ⇒ Object



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

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

#default_has_one_rolesObject



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

def default_has_one_roles
  @default_roles[:has_one]
end

#default_has_one_roles=(other = {}) ⇒ Object



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

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

#default_rolesObject



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

def default_roles
  @default_roles[:resource]
end

#default_roles=(other = {}) ⇒ Object



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

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

#freezeObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/sinja/config.rb', line 175

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