Module: Fortitude::Widget::Needs::ClassMethods

Defined in:
lib/fortitude/widget/needs.rb

Constant Summary collapse

STANDARD_INSTANCE_VARIABLE_PREFIX =

INTERNAL USE ONLY

"_fortitude_assign_"

Instance Method Summary collapse

Instance Method Details

#ensure_needs_methods_are_valid!(originating_class = self) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fortitude/widget/needs.rb', line 110

def ensure_needs_methods_are_valid!(originating_class = self)
  out = false
  out ||= superclass.ensure_needs_methods_are_valid!(originating_class) if superclass.respond_to?(:ensure_needs_methods_are_valid!)

  unless @_fortitude_my_needs_methods_valid
    rebuilding(:needs, :invalid, originating_class) do
      rebuild_my_needs_methods!
      @_fortitude_my_needs_methods_valid = true
    end

    out = true
  end

  out
end

#extract_needed_assigns_from(input) ⇒ Object

PUBLIC API



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fortitude/widget/needs.rb', line 82

def extract_needed_assigns_from(input)
  out = { }
  needs_as_hash.keys.each do |name|
    if input.has_key?(name)
      out[name] = input[name]
    elsif input.has_key?(name.to_s)
      out[name] = input[name.to_s]
    end
  end
  out
end

#instance_variable_name_for_need(need_name) ⇒ Object

INTERNAL USE ONLY



98
99
100
101
102
103
# File 'lib/fortitude/widget/needs.rb', line 98

def instance_variable_name_for_need(need_name)
  effective_name = need_name.to_s
  effective_name.gsub!("!", "_fortitude_bang")
  effective_name.gsub!("?", "_fortitude_question")
  "@" + (use_instance_variables_for_assigns ? "" : STANDARD_INSTANCE_VARIABLE_PREFIX) + effective_name
end

#invalidate_my_needs_methods!Object

INTERNAL USE ONLY



106
107
108
# File 'lib/fortitude/widget/needs.rb', line 106

def invalidate_my_needs_methods!
  @_fortitude_my_needs_methods_valid = false
end

#invalidate_needs!(why, klass = self) ⇒ Object

INTERNAL USE ONLY



73
74
75
76
77
78
79
# File 'lib/fortitude/widget/needs.rb', line 73

def invalidate_needs!(why, klass = self)
  invalidating(:needs, why, klass) do
    @_fortitude_needs_as_hash = nil
    invalidate_my_needs_methods!
    direct_subclasses.each { |s| s.invalidate_needs!(why, klass) }
  end
end

#is_valid_ruby_method_name?(s) ⇒ Boolean

INTERNAL USE ONLY

Returns:

  • (Boolean)


54
55
56
# File 'lib/fortitude/widget/needs.rb', line 54

def is_valid_ruby_method_name?(s)
  s.to_s =~ /^[A-Za-z_][A-Za-z0-9_]*[\?\!]?$/
end

#my_needs_as_hashObject

INTERNAL USE ONLY



68
69
70
# File 'lib/fortitude/widget/needs.rb', line 68

def my_needs_as_hash
  @this_class_needs ||= { }
end

#needs(*names) ⇒ Object

PUBLIC API

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fortitude/widget/needs.rb', line 24

def needs(*names)
  previous_needs = needs_as_hash
  return previous_needs if names.length == 0

  @this_class_needs ||= { }

  with_defaults_raw = { }
  with_defaults_raw = names.pop if names[-1] && names[-1].kind_of?(Hash)

  names = names.map { |n| n.to_s.strip.downcase.to_sym }
  with_defaults = { }
  with_defaults_raw.each { |k,v| with_defaults[k.to_s.strip.downcase.to_sym] = v }

  bad_names = names.select { |n| ! is_valid_ruby_method_name?(n.to_s) }
  raise ArgumentError, "Needs in a Fortitude widget class must be valid Ruby method names; these are not: #{bad_names.inspect}" if bad_names.length > 0

  names.each do |name|
    @this_class_needs[name] = REQUIRED_NEED
  end

  with_defaults.each do |name, default_value|
    @this_class_needs[name] = default_value.freeze
  end

  invalidate_needs!(:need_declared)

  needs_as_hash
end

#needs_as_hashObject

INTERNAL USE ONLY



59
60
61
62
63
64
65
# File 'lib/fortitude/widget/needs.rb', line 59

def needs_as_hash
  @_fortitude_needs_as_hash ||= begin
    out = { }
    out = superclass.needs_as_hash if superclass.respond_to?(:needs_as_hash)
    out.merge(@this_class_needs || { })
  end
end