Class: BetterSeo::DSL::Base
- Inherits:
-
Object
- Object
- BetterSeo::DSL::Base
- Defined in:
- lib/better_seo/dsl/base.rb
Direct Known Subclasses
MetaTags, OpenGraph, OpenGraph::ArticleBuilder, TwitterCards
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#build ⇒ Object
Build final configuration.
-
#evaluate(&block) ⇒ Object
Block evaluation.
-
#get(key) ⇒ Object
Generic getter method.
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#merge!(other) ⇒ Object
Merge another config.
-
#set(key, value) ⇒ Object
Generic setter method.
-
#to_h ⇒ Object
Convert to hash.
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
8 9 10 |
# File 'lib/better_seo/dsl/base.rb', line 8 def initialize @config = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (protected)
Dynamic method handling
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/better_seo/dsl/base.rb', line 60 def method_missing(method_name, *args, &block) method_str = method_name.to_s if method_str.end_with?("=") # Setter: title = "value" key = method_str.chomp("=").to_sym set(key, args.first) elsif block_given? # Nested block: open_graph do ... end nested_builder = self.class.new nested_builder.evaluate(&block) set(method_name, nested_builder.build) elsif args.any? # Setter without =: title "value" set(method_name, args.first) else # Getter get(method_name) end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/better_seo/dsl/base.rb', line 6 def config @config end |
Instance Method Details
#build ⇒ Object
Build final configuration
30 31 32 33 |
# File 'lib/better_seo/dsl/base.rb', line 30 def build validate! @config.dup end |
#evaluate(&block) ⇒ Object
Block evaluation
24 25 26 27 |
# File 'lib/better_seo/dsl/base.rb', line 24 def evaluate(&block) instance_eval(&block) if block_given? self end |
#get(key) ⇒ Object
Generic getter method
19 20 21 |
# File 'lib/better_seo/dsl/base.rb', line 19 def get(key) @config[key] end |
#merge!(other) ⇒ Object
Merge another config
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/better_seo/dsl/base.rb', line 41 def merge!(other) if other.is_a?(Hash) @config.merge!(other) elsif other.respond_to?(:to_h) @config.merge!(other.to_h) else raise DSLError, "Cannot merge #{other.class}" end self end |
#set(key, value) ⇒ Object
Generic setter method
13 14 15 16 |
# File 'lib/better_seo/dsl/base.rb', line 13 def set(key, value) @config[key] = value self end |
#to_h ⇒ Object
Convert to hash
36 37 38 |
# File 'lib/better_seo/dsl/base.rb', line 36 def to_h @config.dup end |