Class: Almicube::Key
- Inherits:
-
Object
- Object
- Almicube::Key
- 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
-
#config ⇒ Object
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#ranking ⇒ Object
readonly
Returns the value of attribute ranking.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
-
#initialize(ranking, options = {}) ⇒ Key
constructor
A new instance of Key.
- #method_missing(m, *args, &block) ⇒ Object
- #to_str ⇒ Object (also: #to_s)
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, ={}) @ranking = ranking = ranking..merge().select { |k| self.class::ALLOWED_OPTIONS.include? k.to_sym } @config ||= Config.config end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/almicube/key.rb', line 25 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
#config ⇒ Object
Returns the value of attribute config.
9 10 11 |
# File 'lib/almicube/key.rb', line 9 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/almicube/key.rb', line 7 def end |
#ranking ⇒ Object (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
17 18 19 |
# File 'lib/almicube/key.rb', line 17 def [](name) [name.to_sym] end |
#[]=(name, value) ⇒ Object
21 22 23 |
# File 'lib/almicube/key.rb', line 21 def []=(name, value) [name.to_sym] = value if self.class::ALLOWED_OPTIONS.include? name.to_sym end |
#to_str ⇒ Object Also known as: to_s
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/almicube/key.rb', line 33 def to_str key = config.key_format.clone key.match( self.class::KEY_PATTERN ) do |m| value = .fetch(m[1].to_sym, '') value = value.strftime(.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 |