Class: Reindeer::Meta::Attribute
- Inherits:
-
Object
- Object
- Reindeer::Meta::Attribute
- Defined in:
- lib/reindeer/meta/attribute.rb
Defined Under Namespace
Classes: AttributeError
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#handles ⇒ Object
readonly
Returns the value of attribute handles.
-
#is_a ⇒ Object
readonly
Returns the value of attribute is_a.
-
#is_bare ⇒ Object
readonly
Returns the value of attribute is_bare.
-
#is_ro ⇒ Object
readonly
Returns the value of attribute is_ro.
-
#is_rw ⇒ Object
readonly
Returns the value of attribute is_rw.
-
#lazy_build ⇒ Object
readonly
Returns the value of attribute lazy_build.
-
#lazy_builder ⇒ Object
readonly
Returns the value of attribute lazy_builder.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type_of ⇒ Object
readonly
Returns the value of attribute type_of.
Instance Method Summary collapse
- #get_default_value ⇒ Object
-
#has_default? ⇒ Boolean
There must be a cleaner way to get a boolean.
- #has_handles? ⇒ Boolean
-
#initialize(name, opts) ⇒ Attribute
constructor
A new instance of Attribute.
- #install_accessors_in(klass) ⇒ Object
- #install_clearer_in(klass) ⇒ Object
- #install_delegators(klass) ⇒ Object
- #install_methods_in(klass) ⇒ Object
- #install_predicate_in(klass) ⇒ Object
- #is_lazy? ⇒ Boolean
-
#required? ⇒ Boolean
Predicates.
- #set_value_for(instance, value) ⇒ Object
- #to_var ⇒ Object
Constructor Details
#initialize(name, opts) ⇒ Attribute
Returns a new instance of Attribute.
18 19 20 21 |
# File 'lib/reindeer/meta/attribute.rb', line 18 def initialize(name, opts) @name = name process_opts opts end |
Instance Attribute Details
#default_value ⇒ Object (readonly)
Returns the value of attribute default_value.
13 14 15 |
# File 'lib/reindeer/meta/attribute.rb', line 13 def default_value @default_value end |
#handles ⇒ Object (readonly)
Returns the value of attribute handles.
16 17 18 |
# File 'lib/reindeer/meta/attribute.rb', line 16 def handles @handles end |
#is_a ⇒ Object (readonly)
Returns the value of attribute is_a.
11 12 13 |
# File 'lib/reindeer/meta/attribute.rb', line 11 def is_a @is_a end |
#is_bare ⇒ Object (readonly)
Returns the value of attribute is_bare.
9 10 11 |
# File 'lib/reindeer/meta/attribute.rb', line 9 def @is_bare end |
#is_ro ⇒ Object (readonly)
Returns the value of attribute is_ro.
9 10 11 |
# File 'lib/reindeer/meta/attribute.rb', line 9 def is_ro @is_ro end |
#is_rw ⇒ Object (readonly)
Returns the value of attribute is_rw.
9 10 11 |
# File 'lib/reindeer/meta/attribute.rb', line 9 def is_rw @is_rw end |
#lazy_build ⇒ Object (readonly)
Returns the value of attribute lazy_build.
14 15 16 |
# File 'lib/reindeer/meta/attribute.rb', line 14 def lazy_build @lazy_build end |
#lazy_builder ⇒ Object (readonly)
Returns the value of attribute lazy_builder.
14 15 16 |
# File 'lib/reindeer/meta/attribute.rb', line 14 def lazy_builder @lazy_builder end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/reindeer/meta/attribute.rb', line 7 def name @name end |
#type_of ⇒ Object (readonly)
Returns the value of attribute type_of.
11 12 13 |
# File 'lib/reindeer/meta/attribute.rb', line 11 def type_of @type_of end |
Instance Method Details
#get_default_value ⇒ Object
95 96 97 |
# File 'lib/reindeer/meta/attribute.rb', line 95 def get_default_value default_value.call end |
#has_default? ⇒ Boolean
There must be a cleaner way to get a boolean.
104 105 106 |
# File 'lib/reindeer/meta/attribute.rb', line 104 def has_default? not default_value.nil? end |
#has_handles? ⇒ Boolean
110 111 112 |
# File 'lib/reindeer/meta/attribute.rb', line 110 def has_handles? not handles.nil? end |
#install_accessors_in(klass) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/reindeer/meta/attribute.rb', line 36 def install_accessors_in(klass) return if if is_lazy? attr_name = to_var builder = lazy_builder klass.__send__ :define_method, name, Proc.new { if instance_variable_defined? attr_name instance_variable_get attr_name else .get_attribute(attr_name).set_value_for( self, builder.is_a?(Symbol) ? __send__(builder) : builder.call() ) end } else name_sym = name klass.class_eval { self.__send__ :attr_reader, name_sym } if is_rw klass.__send__ :define_method, "#{name}=", Proc.new {|v| .get_attribute(name_sym).set_value_for(self, v) } end end end |
#install_clearer_in(klass) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/reindeer/meta/attribute.rb', line 63 def install_clearer_in(klass) attr_name = to_var klass.__send__ :define_method, "clear_#{name}!", Proc.new { remove_instance_variable attr_name } end |
#install_delegators(klass) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/reindeer/meta/attribute.rb', line 77 def install_delegators(klass) attr_name = to_var handles.each do |method| klass.__send__ :define_method, method, Proc.new {|*args| instance_variable_get(attr_name).__send__(method, *args) } end end |
#install_methods_in(klass) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/reindeer/meta/attribute.rb', line 27 def install_methods_in(klass) install_accessors_in klass if lazy_build install_clearer_in klass install_predicate_in klass end install_delegators klass if has_handles? end |
#install_predicate_in(klass) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/reindeer/meta/attribute.rb', line 70 def install_predicate_in(klass) attr_name = to_var klass.__send__ :define_method, "has_#{name}?", Proc.new { instance_variable_defined? attr_name } end |
#is_lazy? ⇒ Boolean
107 108 109 |
# File 'lib/reindeer/meta/attribute.rb', line 107 def is_lazy? not @lazy_builder.nil? end |
#required? ⇒ Boolean
Predicates
100 101 102 |
# File 'lib/reindeer/meta/attribute.rb', line 100 def required? @required end |
#set_value_for(instance, value) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/reindeer/meta/attribute.rb', line 86 def set_value_for(instance, value) if is_a and not value.is_a? is_a raise Meta::Attribute::AttributeError, "The value for '#{name}' of type '#{value.class}' is not a '#{is_a}'" end type_of.check_constraint(value) if type_of instance.instance_variable_set to_var, value end |
#to_var ⇒ Object
23 24 25 |
# File 'lib/reindeer/meta/attribute.rb', line 23 def to_var "@#{name.to_s}" end |