Class: Casbin::Config::Config
- Inherits:
-
Object
- Object
- Casbin::Config::Config
- Defined in:
- lib/casbin-ruby/config/config.rb
Constant Summary collapse
- DEFAULT_SECTION =
DEFAULT_SECTION specifies the name of a section if no name provided
'default'
- DEFAULT_COMMENT =
DEFAULT_COMMENT defines what character(s) indicate a comment ‘#`
'#'
- DEFAULT_COMMENT_SEM =
DEFAULT_COMMENT_SEM defines what alternate character(s) indicate a comment ‘;`
';'
- DEFAULT_MULTI_LINE_SEPARATOR =
DEFAULT_MULTI_LINE_SEPARATOR defines what character indicates a multi-line content
'\\'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
- #add_config(section, option, value) ⇒ Object
-
#get(key) ⇒ Object
section.key or key.
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #parse(conf_name) ⇒ Object
- #parse_from_io(io) ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
18 19 20 |
# File 'lib/casbin-ruby/config/config.rb', line 18 def initialize @data = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
16 17 18 |
# File 'lib/casbin-ruby/config/config.rb', line 16 def data @data end |
Class Method Details
.new_config(conf_name) ⇒ Object
22 23 24 |
# File 'lib/casbin-ruby/config/config.rb', line 22 def self.new_config(conf_name) new.tap { |config| config.parse(conf_name) } end |
.new_config_from_text(text) ⇒ Object
26 27 28 |
# File 'lib/casbin-ruby/config/config.rb', line 26 def self.new_config_from_text(text) new.tap { |config| config.parse_from_io StringIO.new(text) } end |
Instance Method Details
#add_config(section, option, value) ⇒ Object
96 97 98 99 100 |
# File 'lib/casbin-ruby/config/config.rb', line 96 def add_config(section, option, value) section = DEFAULT_SECTION if section == '' data[section] ||= {} data[section][option] = value end |
#get(key) ⇒ Object
section.key or key
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/casbin-ruby/config/config.rb', line 46 def get(key) keys = key.to_s.downcase.split('::') if keys.size >= 2 section = keys[0] option = keys[1] else section = DEFAULT_SECTION option = keys[0] end return '' unless data.key?(section) data[section][option] || '' end |
#parse(conf_name) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/casbin-ruby/config/config.rb', line 61 def parse(conf_name) return unless File.exist?(conf_name) # 'r:UTF-8' required for Windows File.open(conf_name, 'r:UTF-8') { |f| parse_from_io f } end |
#parse_from_io(io) ⇒ Object
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 |
# File 'lib/casbin-ruby/config/config.rb', line 68 def parse_from_io(io) line_number = 0 section = '' multi_line = '' io.each_line do |raw| line = raw.chomp line_number += 1 next if line == '' || line[0] == DEFAULT_COMMENT || line[0] == DEFAULT_COMMENT_SEM next section = line[1...-1] if line[0] == '[' && line[-1] == ']' if line[-1] == DEFAULT_MULTI_LINE_SEPARATOR && line.strip.size > 1 part = line[0...-1].strip multi_line = multi_line == '' ? part : "#{multi_line} #{part}" next end if multi_line == '' write(section, line, line_number) else multi_line += " #{line.strip}" unless line[-1] == DEFAULT_MULTI_LINE_SEPARATOR write(section, multi_line, line_number) multi_line = '' end end end |
#set(key, value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/casbin-ruby/config/config.rb', line 30 def set(key, value) raise 'key is empty' if key.to_s.size.zero? keys = key.downcase.split('::') if keys.size >= 2 section = keys[0] option = keys[1] else section = '' option = keys[0] end add_config(section, option, value) end |