Class: Dao::Extractor
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Dao::Extractor
- Defined in:
- lib/dao/extractor.rb
Instance Attribute Summary collapse
-
#extracted ⇒ Object
readonly
Returns the value of attribute extracted.
-
#strategies ⇒ Object
readonly
Returns the value of attribute strategies.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #extract(attribute, &block) ⇒ Object
- #extracts(*args, &block) ⇒ Object
-
#initialize(target, *args, &block) ⇒ Extractor
constructor
A new instance of Extractor.
- #inspect ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
Constructor Details
#initialize(target, *args, &block) ⇒ Extractor
Returns a new instance of Extractor.
8 9 10 11 12 13 |
# File 'lib/dao/extractor.rb', line 8 def initialize(target, *args, &block) @target = target @strategies = Map.new @extracted = Map.new extracts(*args) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
42 43 44 45 |
# File 'lib/dao/extractor.rb', line 42 def method_missing(method, *args, &block) super unless @strategies.has_key?(method) extract(method, &@strategies[method]) end |
Instance Attribute Details
#extracted ⇒ Object (readonly)
Returns the value of attribute extracted.
5 6 7 |
# File 'lib/dao/extractor.rb', line 5 def extracted @extracted end |
#strategies ⇒ Object (readonly)
Returns the value of attribute strategies.
6 7 8 |
# File 'lib/dao/extractor.rb', line 6 def strategies @strategies end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
4 5 6 |
# File 'lib/dao/extractor.rb', line 4 def target @target end |
Instance Method Details
#extract(attribute, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dao/extractor.rb', line 47 def extract(attribute, &block) return @extracted[attribute] if @extracted.has_key?(attribute) if @target.respond_to?(attribute) value = @target.send(attribute) @extracted[attribute] = value return @extracted[attribute] end ivar = "@#{ attribute }" if @target.instance_variable_defined?(ivar) value = @target.instance_variable_get(ivar) @extracted[attribute] = value return @extracted[attribute] end if block @extracted[attribute] = block.call return @extracted[attribute] end end |
#extracts(*args, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dao/extractor.rb', line 19 def extracts(*args, &block) hashes = [] args.each do |arg| if arg.is_a?(Hash) hashes.push(arg) else if block hashes.push(arg => block) end end end hashes.each do |hash| hash.each do |key, val| next unless val.respond_to?(:call) @strategies[key] = val end end self end |
#inspect ⇒ Object
15 16 17 |
# File 'lib/dao/extractor.rb', line 15 def inspect @extracted.inspect end |