Module: Awestruct::AStructMixin

Included in:
AStruct
Defined in:
lib/awestruct/astruct_mixin.rb

Constant Summary collapse

UNTRACKED_KEYS =
[
  :url,
]

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/awestruct/astruct_mixin.rb', line 29

def method_missing(sym, *args, &blk)
  type = sym.to_s[-1,1]
  name = sym.to_s.gsub(/[=!?]$/, '').to_sym
  case type
  when '='
    self[name] = args.first
  when '!'
    __send__(name, *args, &blk)
  when '?'
    self[name]
  else
    if key?(name)
      self[name]
    elsif @cascade_for_nils
      self[name] = AStruct.new.cascade_for_nils!
      self[name]
    else
      unless args.size.zero?
        $LOG.warn "Call to unknown method: #{name}"
      end
      nil
    end
  end
end

Class Method Details

.extended(o) ⇒ Object



8
9
10
11
12
13
# File 'lib/awestruct/astruct_mixin.rb', line 8

def self.extended(o)
  class << o
    alias_method :original_entries, :entries
    undef entries
  end
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
27
# File 'lib/awestruct/astruct_mixin.rb', line 24

def [](key)
  r = [key, key.to_sym, key.to_s].find { |fk| !super(fk).nil? }
  transform_entry( key, super(r) )
end

#cascade_for_nils!Object



15
16
17
18
# File 'lib/awestruct/astruct_mixin.rb', line 15

def cascade_for_nils!
  @cascade_for_nils = true
  self
end

#inspectObject



78
79
80
# File 'lib/awestruct/astruct_mixin.rb', line 78

def inspect
  "AStruct<#{super.to_s}>"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/awestruct/astruct_mixin.rb', line 20

def key?(key)
  super( key ) || super( key.to_sym ) || super( key.to_s )
end

#transform_entry(for_key, entry) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/awestruct/astruct_mixin.rb', line 58

def transform_entry(for_key, entry)
  r = case(entry)
    when AStructMixin
      entry
    when Hash
      #AStruct.new( entry )
      entry.extend( AStructMixin )
    when Array
      entry.map!{|i| transform_entry( for_key, i)}
    else
      entry
  end
  if ( self.is_a? Awestruct::Page )
    unless UNTRACKED_KEYS.include? for_key.to_sym
      Awestruct::Dependencies.track_key_dependency( self, for_key )
    end
  end
  r
end