Method: MetaRuby::Attributes#inherited_single_value_attribute

Defined in:
lib/metaruby/attributes.rb

#inherited_single_value_attribute(name, &default_value) ⇒ InheritedAttribute

Defines an attribute that holds at most a single value

The value returned by the defined accessor will be the one set at the lowest level in the model hierarchy (i.e. self, then superclass, …)

Parameters:

  • name (String)

    the attribute name

Returns:

  • (InheritedAttribute)

    the attribute definition

Raises:

  • (ArgumentError)

    if no attribute with that name exists



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/metaruby/attributes.rb', line 51

def inherited_single_value_attribute(name, &default_value)
    dsl_attribute_name = "__dsl_attribute__#{name}"
    ivar = "@#{dsl_attribute_name}"
    dsl_attribute(dsl_attribute_name)
    if default_value
        define_method("#{dsl_attribute_name}_get_default") { default_value }
    end

    promotion_method = "promote_#{name}"
    if method_defined?(promotion_method)
        define_single_value_with_promotion("#{dsl_attribute_name}_get", promotion_method, ivar)
    else
        define_single_value_without_promotion("#{dsl_attribute_name}_get", ivar)
    end
    define_method(name) do |*args|
        if args.empty? # Getter call
            send("#{dsl_attribute_name}_get")
        else # Setter call, delegate to the dsl_attribute implementation
            send(dsl_attribute_name, *args)
        end
    end
    nil
end