Class: Portrayal::Schema
- Inherits:
-
Object
- Object
- Portrayal::Schema
- Defined in:
- lib/portrayal/schema.rb
Constant Summary collapse
- DEFINITION_OF_OBJECT_ENHANCEMENTS =
<<~RUBY.freeze def eql?(other); self.class == other.class && self == other end def hash; [self.class, self.class.portrayal.attributes(self)].hash end def deconstruct; self.class.portrayal.attributes(self).values end def deconstruct_keys(keys) keys ||= self.class.portrayal.keywords keys &= self.class.portrayal.keywords Hash[keys.map { |k| [k, send(k)] }] end def ==(other) return super unless other.class.is_a?(Portrayal) self.class.portrayal.attributes(self) == self.class.portrayal.attributes(other) end def freeze self.class.portrayal.attributes(self).values.each(&:freeze) super end def initialize_dup(source) self.class.portrayal.attributes(source).each do |key, value| instance_variable_set("@\#{key}", value.dup) end super end def initialize_clone(source) self.class.portrayal.attributes(source).each do |key, value| instance_variable_set("@\#{key}", value.clone) end super end RUBY
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_keyword(name, default) ⇒ Object
- #attributes(object) ⇒ Object
- #camelize(string) ⇒ Object
- #definition_of_initialize ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
- #initialize_dup(other) ⇒ Object
- #keywords ⇒ Object
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
45 |
# File 'lib/portrayal/schema.rb', line 45 def initialize; @schema = {} end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
5 6 7 |
# File 'lib/portrayal/schema.rb', line 5 def schema @schema end |
Instance Method Details
#[](name) ⇒ Object
47 |
# File 'lib/portrayal/schema.rb', line 47 def [](name); @schema[name] end |
#add_keyword(name, default) ⇒ Object
55 56 57 58 59 |
# File 'lib/portrayal/schema.rb', line 55 def add_keyword(name, default) name = name.to_sym @schema.delete(name) # Forcing keyword to be added at the end of the hash. @schema[name] = default.equal?(NULL) ? nil : Default.new(default) end |
#attributes(object) ⇒ Object
49 50 51 |
# File 'lib/portrayal/schema.rb', line 49 def attributes(object) Hash[object.class.portrayal.keywords.map { |k| [k, object.send(k)] }] end |
#camelize(string) ⇒ Object
53 |
# File 'lib/portrayal/schema.rb', line 53 def camelize(string); string.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase } end |
#definition_of_initialize ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/portrayal/schema.rb', line 65 def definition_of_initialize init_args = @schema.map { |name, default| "#{name}:#{default && " self.class.portrayal[:#{name}]"}" }.join(', ') init_assigns = @schema.keys.map { |name| "@#{name} = #{name}.is_a?(::Portrayal::Default) ? " \ "(#{name}.call? ? instance_exec(&#{name}.value) : #{name}.value) : " \ "#{name}" }.join('; ') "def initialize(#{init_args}); #{init_assigns} end" end |
#initialize_dup(other) ⇒ Object
61 62 63 |
# File 'lib/portrayal/schema.rb', line 61 def initialize_dup(other) super; @schema = other.schema.transform_values(&:dup) end |
#keywords ⇒ Object
46 |
# File 'lib/portrayal/schema.rb', line 46 def keywords; @schema.keys end |