Class: Almicube::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/almicube/key.rb

Constant Summary collapse

ALLOWED_OPTIONS =
%i[prefix suffix class_name attribute_name type distinction date selector]
KEY_PATTERN =

Example) %sample_key

/%{([a-z_]+)}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ranking, options = {}) ⇒ Key

Returns a new instance of Key.



11
12
13
14
15
# File 'lib/almicube/key.rb', line 11

def initialize(ranking, options={})
  @ranking = ranking
  @options = select ranking.options.merge(options)
  @config ||= Config.config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/almicube/key.rb', line 30

def method_missing(m, *args, &block)
  if to_str.respond_to? m
    to_str.send(m, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/almicube/key.rb', line 9

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/almicube/key.rb', line 7

def options
  @options
end

#rankingObject (readonly)

Returns the value of attribute ranking.



7
8
9
# File 'lib/almicube/key.rb', line 7

def ranking
  @ranking
end

Instance Method Details

#[](name) ⇒ Object



22
23
24
# File 'lib/almicube/key.rb', line 22

def [](name)
  options[name.to_sym]
end

#[]=(name, value) ⇒ Object



26
27
28
# File 'lib/almicube/key.rb', line 26

def []=(name, value)
  options[name.to_sym] = value if self.class::ALLOWED_OPTIONS.include? name.to_sym
end

#merge!(options = {}) ⇒ Object



17
18
19
20
# File 'lib/almicube/key.rb', line 17

def merge!(options={})
  @options = select @options.merge(options)
  self
end

#to_strObject Also known as: to_s, inspect



38
39
40
41
42
43
44
45
46
47
# File 'lib/almicube/key.rb', line 38

def to_str
  key = config.key_format.clone
  key.match( self.class::KEY_PATTERN ) do |m|
    value = options.fetch(m[1].to_sym, '')
    value = value.strftime(options.fetch(:date_format, '%Y%m%d')) if value.is_a? Date
    value = value.label if value.kind_of? Selector::Base
    key.gsub! m[0], value.to_s
  end while key =~ self.class::KEY_PATTERN
  key
end