Skip to content

Commit dfa8155

Browse files
committed
[refactor] lazily instantiating entries
1 parent ce0873b commit dfa8155

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/contentstack/entries.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ class Entries
88

99
def initialize(body)
1010
@body = body
11-
@entries = body.map { |entry| Entry.new(entry) }
1211
end
1312

1413
def each(&block)
15-
entries.each(&block)
14+
@body.each do |entry|
15+
block.call(Entry.new(entry))
16+
end
1617
end
1718
end
1819
end

lib/contentstack/entry.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
module Contentstack
44
class Entry
5+
6+
attr_reader :properties, :attributes
57

68
def initialize(attributes)
79
@attributes = attributes
10+
@properties = attributes.keys
11+
12+
properties.each do |prop|
13+
define_singleton_method prop do
14+
@attributes[prop]
15+
end
16+
end
817
end
918

10-
def properties
11-
@attributes.keys
19+
def to_s
20+
"#{attributes[:title]} created on #{attributes[:created_at]}"
1221
end
1322

1423
def is_entry?

0 commit comments

Comments
 (0)