Class: Reindeer::Meta
- Inherits:
-
Object
- Object
- Reindeer::Meta
- Defined in:
- lib/reindeer/meta.rb,
lib/reindeer/meta/attribute.rb
Defined Under Namespace
Classes: Attribute
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#required_methods ⇒ Object
readonly
Returns the value of attribute required_methods.
Instance Method Summary collapse
- #add_attribute(name, opts) ⇒ Object
-
#add_required_method(method) ⇒ Object
Not sure if this is the best place for it.
- #add_role(role) ⇒ Object
- #all_roles ⇒ Object
- #build_all(obj, args) ⇒ Object
- #compose! ⇒ Object
- #get_all_attributes ⇒ Object
- #get_attribute(sym) ⇒ Object
- #get_attributes ⇒ Object
- #has_attribute?(name) ⇒ Boolean
-
#initialize(klass) ⇒ Meta
constructor
A new instance of Meta.
- #setup_attributes(obj, args) ⇒ Object
Constructor Details
#initialize(klass) ⇒ Meta
Returns a new instance of Meta.
9 10 11 12 13 14 15 |
# File 'lib/reindeer/meta.rb', line 9 def initialize(klass) @klass = klass # Hrm, circular? Not a problem with constants? # TODO Use a hash @attributes = [] @roles = [] @required_methods = [] end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/reindeer/meta.rb', line 6 def klass @klass end |
#required_methods ⇒ Object (readonly)
Returns the value of attribute required_methods.
7 8 9 |
# File 'lib/reindeer/meta.rb', line 7 def required_methods @required_methods end |
Instance Method Details
#add_attribute(name, opts) ⇒ Object
66 67 68 69 70 |
# File 'lib/reindeer/meta.rb', line 66 def add_attribute(name, opts) attr = Reindeer::Meta::Attribute.new(name, opts) get_attributes << attr return attr end |
#add_required_method(method) ⇒ Object
Not sure if this is the best place for it.
51 52 53 |
# File 'lib/reindeer/meta.rb', line 51 def add_required_method(method) @required_methods << method end |
#add_role(role) ⇒ Object
42 43 44 |
# File 'lib/reindeer/meta.rb', line 42 def add_role(role) @roles << role end |
#all_roles ⇒ Object
46 47 48 |
# File 'lib/reindeer/meta.rb', line 46 def all_roles @roles end |
#build_all(obj, args) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/reindeer/meta.rb', line 17 def build_all(obj, args) to_build = obj.class.ancestors.take_while { |klass| klass != Reindeer }.reverse (to_build - obj.class.included_modules).each { |klass| # TODO assume build is private. build = klass.instance_method(:build) build.bind(obj).call(args) } end |
#compose! ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/reindeer/meta.rb', line 29 def compose! all_roles.each do |role| role.assert_requires klass # role.compose_methods! klass klass.__send__ :include, role # Blech role..get_attributes.each do |attr| attr.install_methods_in klass end get_attributes.push(*role..get_attributes) end end |
#get_all_attributes ⇒ Object
59 60 61 62 63 64 |
# File 'lib/reindeer/meta.rb', line 59 def get_all_attributes all_classes = klass.ancestors.take_while{|k| k!=Reindeer}.select{|c| c.class == Class }.reverse all_classes.collect{|c| c..get_attributes}.flatten end |
#get_attribute(sym) ⇒ Object
94 95 96 97 |
# File 'lib/reindeer/meta.rb', line 94 def get_attribute(sym) sym = sym.sub(/^@/, '').to_sym if sym.is_a?(String) get_all_attributes.select{|a| a.name == sym}.first end |
#get_attributes ⇒ Object
55 56 57 |
# File 'lib/reindeer/meta.rb', line 55 def get_attributes @attributes end |
#has_attribute?(name) ⇒ Boolean
90 91 92 |
# File 'lib/reindeer/meta.rb', line 90 def has_attribute?(name) get_all_attributes.any? {|a| a.name == name } end |
#setup_attributes(obj, args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/reindeer/meta.rb', line 72 def setup_attributes(obj, args) for attr in get_all_attributes name = attr.name if attr.required? and not args.has_key? name raise Meta::Attribute::AttributeError, "Did not specify required argument '#{name}'" end obj.instance_eval do if args.has_key?(name) attr.set_value_for self, args[name] elsif attr.has_default? and not attr.is_lazy? attr.set_value_for self, attr.get_default_value end end end end |