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.
- #merge!(options = {}) ⇒ Object
- #method_missing(m, *args, &block) ⇒ Object
- #to_str ⇒ Object (also: #to_s, #inspect)
Constructor Details
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
#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 @options 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
22 23 24 |
# File 'lib/almicube/key.rb', line 22 def [](name) [name.to_sym] end |
#[]=(name, value) ⇒ Object
26 27 28 |
# File 'lib/almicube/key.rb', line 26 def []=(name, value) [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 = select @options.merge() self end |
#to_str ⇒ Object 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 = .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 |